mmci.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070
  1. /*
  2. * linux/drivers/mmc/host/mmci.c - ARM PrimeCell MMCI PL180/1 driver
  3. *
  4. * Copyright (C) 2003 Deep Blue Solutions, Ltd, All Rights Reserved.
  5. * Copyright (C) 2010 ST-Ericsson SA
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/moduleparam.h>
  13. #include <linux/init.h>
  14. #include <linux/ioport.h>
  15. #include <linux/device.h>
  16. #include <linux/io.h>
  17. #include <linux/interrupt.h>
  18. #include <linux/kernel.h>
  19. #include <linux/slab.h>
  20. #include <linux/delay.h>
  21. #include <linux/err.h>
  22. #include <linux/highmem.h>
  23. #include <linux/log2.h>
  24. #include <linux/mmc/pm.h>
  25. #include <linux/mmc/host.h>
  26. #include <linux/mmc/card.h>
  27. #include <linux/mmc/slot-gpio.h>
  28. #include <linux/amba/bus.h>
  29. #include <linux/clk.h>
  30. #include <linux/scatterlist.h>
  31. #include <linux/gpio.h>
  32. #include <linux/of_gpio.h>
  33. #include <linux/regulator/consumer.h>
  34. #include <linux/dmaengine.h>
  35. #include <linux/dma-mapping.h>
  36. #include <linux/amba/mmci.h>
  37. #include <linux/pm_runtime.h>
  38. #include <linux/types.h>
  39. #include <linux/pinctrl/consumer.h>
  40. #include <asm/div64.h>
  41. #include <asm/io.h>
  42. #include "mmci.h"
  43. #include "mmci_qcom_dml.h"
  44. #define DRIVER_NAME "mmci-pl18x"
  45. static unsigned int fmax = 515633;
  46. /**
  47. * struct variant_data - MMCI variant-specific quirks
  48. * @clkreg: default value for MCICLOCK register
  49. * @clkreg_enable: enable value for MMCICLOCK register
  50. * @clkreg_8bit_bus_enable: enable value for 8 bit bus
  51. * @clkreg_neg_edge_enable: enable value for inverted data/cmd output
  52. * @datalength_bits: number of bits in the MMCIDATALENGTH register
  53. * @fifosize: number of bytes that can be written when MMCI_TXFIFOEMPTY
  54. * is asserted (likewise for RX)
  55. * @fifohalfsize: number of bytes that can be written when MCI_TXFIFOHALFEMPTY
  56. * is asserted (likewise for RX)
  57. * @data_cmd_enable: enable value for data commands.
  58. * @st_sdio: enable ST specific SDIO logic
  59. * @st_clkdiv: true if using a ST-specific clock divider algorithm
  60. * @datactrl_mask_ddrmode: ddr mode mask in datactrl register.
  61. * @blksz_datactrl16: true if Block size is at b16..b30 position in datactrl register
  62. * @blksz_datactrl4: true if Block size is at b4..b16 position in datactrl
  63. * register
  64. * @datactrl_mask_sdio: SDIO enable mask in datactrl register
  65. * @pwrreg_powerup: power up value for MMCIPOWER register
  66. * @f_max: maximum clk frequency supported by the controller.
  67. * @signal_direction: input/out direction of bus signals can be indicated
  68. * @pwrreg_clkgate: MMCIPOWER register must be used to gate the clock
  69. * @busy_detect: true if the variant supports busy detection on DAT0.
  70. * @busy_dpsm_flag: bitmask enabling busy detection in the DPSM
  71. * @busy_detect_flag: bitmask identifying the bit in the MMCISTATUS register
  72. * indicating that the card is busy
  73. * @busy_detect_mask: bitmask identifying the bit in the MMCIMASK0 to mask for
  74. * getting busy end detection interrupts
  75. * @pwrreg_nopower: bits in MMCIPOWER don't controls ext. power supply
  76. * @explicit_mclk_control: enable explicit mclk control in driver.
  77. * @qcom_fifo: enables qcom specific fifo pio read logic.
  78. * @qcom_dml: enables qcom specific dma glue for dma transfers.
  79. * @reversed_irq_handling: handle data irq before cmd irq.
  80. * @mmcimask1: true if variant have a MMCIMASK1 register.
  81. * @start_err: bitmask identifying the STARTBITERR bit inside MMCISTATUS
  82. * register.
  83. * @opendrain: bitmask identifying the OPENDRAIN bit inside MMCIPOWER register
  84. */
  85. struct variant_data {
  86. unsigned int clkreg;
  87. unsigned int clkreg_enable;
  88. unsigned int clkreg_8bit_bus_enable;
  89. unsigned int clkreg_neg_edge_enable;
  90. unsigned int datalength_bits;
  91. unsigned int fifosize;
  92. unsigned int fifohalfsize;
  93. unsigned int data_cmd_enable;
  94. unsigned int datactrl_mask_ddrmode;
  95. unsigned int datactrl_mask_sdio;
  96. bool st_sdio;
  97. bool st_clkdiv;
  98. bool blksz_datactrl16;
  99. bool blksz_datactrl4;
  100. u32 pwrreg_powerup;
  101. u32 f_max;
  102. bool signal_direction;
  103. bool pwrreg_clkgate;
  104. bool busy_detect;
  105. u32 busy_dpsm_flag;
  106. u32 busy_detect_flag;
  107. u32 busy_detect_mask;
  108. bool pwrreg_nopower;
  109. bool explicit_mclk_control;
  110. bool qcom_fifo;
  111. bool qcom_dml;
  112. bool reversed_irq_handling;
  113. bool mmcimask1;
  114. u32 start_err;
  115. u32 opendrain;
  116. };
  117. static struct variant_data variant_arm = {
  118. .fifosize = 16 * 4,
  119. .fifohalfsize = 8 * 4,
  120. .datalength_bits = 16,
  121. .pwrreg_powerup = MCI_PWR_UP,
  122. .f_max = 100000000,
  123. .reversed_irq_handling = true,
  124. .mmcimask1 = true,
  125. .start_err = MCI_STARTBITERR,
  126. .opendrain = MCI_ROD,
  127. };
  128. static struct variant_data variant_arm_extended_fifo = {
  129. .fifosize = 128 * 4,
  130. .fifohalfsize = 64 * 4,
  131. .datalength_bits = 16,
  132. .pwrreg_powerup = MCI_PWR_UP,
  133. .f_max = 100000000,
  134. .mmcimask1 = true,
  135. .start_err = MCI_STARTBITERR,
  136. .opendrain = MCI_ROD,
  137. };
  138. static struct variant_data variant_arm_extended_fifo_hwfc = {
  139. .fifosize = 128 * 4,
  140. .fifohalfsize = 64 * 4,
  141. .clkreg_enable = MCI_ARM_HWFCEN,
  142. .datalength_bits = 16,
  143. .pwrreg_powerup = MCI_PWR_UP,
  144. .f_max = 100000000,
  145. .mmcimask1 = true,
  146. .start_err = MCI_STARTBITERR,
  147. .opendrain = MCI_ROD,
  148. };
  149. static struct variant_data variant_u300 = {
  150. .fifosize = 16 * 4,
  151. .fifohalfsize = 8 * 4,
  152. .clkreg_enable = MCI_ST_U300_HWFCEN,
  153. .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
  154. .datalength_bits = 16,
  155. .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
  156. .st_sdio = true,
  157. .pwrreg_powerup = MCI_PWR_ON,
  158. .f_max = 100000000,
  159. .signal_direction = true,
  160. .pwrreg_clkgate = true,
  161. .pwrreg_nopower = true,
  162. .mmcimask1 = true,
  163. .start_err = MCI_STARTBITERR,
  164. .opendrain = MCI_OD,
  165. };
  166. static struct variant_data variant_nomadik = {
  167. .fifosize = 16 * 4,
  168. .fifohalfsize = 8 * 4,
  169. .clkreg = MCI_CLK_ENABLE,
  170. .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
  171. .datalength_bits = 24,
  172. .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
  173. .st_sdio = true,
  174. .st_clkdiv = true,
  175. .pwrreg_powerup = MCI_PWR_ON,
  176. .f_max = 100000000,
  177. .signal_direction = true,
  178. .pwrreg_clkgate = true,
  179. .pwrreg_nopower = true,
  180. .mmcimask1 = true,
  181. .start_err = MCI_STARTBITERR,
  182. .opendrain = MCI_OD,
  183. };
  184. static struct variant_data variant_ux500 = {
  185. .fifosize = 30 * 4,
  186. .fifohalfsize = 8 * 4,
  187. .clkreg = MCI_CLK_ENABLE,
  188. .clkreg_enable = MCI_ST_UX500_HWFCEN,
  189. .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
  190. .clkreg_neg_edge_enable = MCI_ST_UX500_NEG_EDGE,
  191. .datalength_bits = 24,
  192. .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
  193. .st_sdio = true,
  194. .st_clkdiv = true,
  195. .pwrreg_powerup = MCI_PWR_ON,
  196. .f_max = 100000000,
  197. .signal_direction = true,
  198. .pwrreg_clkgate = true,
  199. .busy_detect = true,
  200. .busy_dpsm_flag = MCI_DPSM_ST_BUSYMODE,
  201. .busy_detect_flag = MCI_ST_CARDBUSY,
  202. .busy_detect_mask = MCI_ST_BUSYENDMASK,
  203. .pwrreg_nopower = true,
  204. .mmcimask1 = true,
  205. .start_err = MCI_STARTBITERR,
  206. .opendrain = MCI_OD,
  207. };
  208. static struct variant_data variant_ux500v2 = {
  209. .fifosize = 30 * 4,
  210. .fifohalfsize = 8 * 4,
  211. .clkreg = MCI_CLK_ENABLE,
  212. .clkreg_enable = MCI_ST_UX500_HWFCEN,
  213. .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
  214. .clkreg_neg_edge_enable = MCI_ST_UX500_NEG_EDGE,
  215. .datactrl_mask_ddrmode = MCI_DPSM_ST_DDRMODE,
  216. .datalength_bits = 24,
  217. .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
  218. .st_sdio = true,
  219. .st_clkdiv = true,
  220. .blksz_datactrl16 = true,
  221. .pwrreg_powerup = MCI_PWR_ON,
  222. .f_max = 100000000,
  223. .signal_direction = true,
  224. .pwrreg_clkgate = true,
  225. .busy_detect = true,
  226. .busy_dpsm_flag = MCI_DPSM_ST_BUSYMODE,
  227. .busy_detect_flag = MCI_ST_CARDBUSY,
  228. .busy_detect_mask = MCI_ST_BUSYENDMASK,
  229. .pwrreg_nopower = true,
  230. .mmcimask1 = true,
  231. .start_err = MCI_STARTBITERR,
  232. .opendrain = MCI_OD,
  233. };
  234. static struct variant_data variant_stm32 = {
  235. .fifosize = 32 * 4,
  236. .fifohalfsize = 8 * 4,
  237. .clkreg = MCI_CLK_ENABLE,
  238. .clkreg_enable = MCI_ST_UX500_HWFCEN,
  239. .clkreg_8bit_bus_enable = MCI_ST_8BIT_BUS,
  240. .clkreg_neg_edge_enable = MCI_ST_UX500_NEG_EDGE,
  241. .datalength_bits = 24,
  242. .datactrl_mask_sdio = MCI_DPSM_ST_SDIOEN,
  243. .st_sdio = true,
  244. .st_clkdiv = true,
  245. .pwrreg_powerup = MCI_PWR_ON,
  246. .f_max = 48000000,
  247. .pwrreg_clkgate = true,
  248. .pwrreg_nopower = true,
  249. };
  250. static struct variant_data variant_qcom = {
  251. .fifosize = 16 * 4,
  252. .fifohalfsize = 8 * 4,
  253. .clkreg = MCI_CLK_ENABLE,
  254. .clkreg_enable = MCI_QCOM_CLK_FLOWENA |
  255. MCI_QCOM_CLK_SELECT_IN_FBCLK,
  256. .clkreg_8bit_bus_enable = MCI_QCOM_CLK_WIDEBUS_8,
  257. .datactrl_mask_ddrmode = MCI_QCOM_CLK_SELECT_IN_DDR_MODE,
  258. .data_cmd_enable = MCI_CPSM_QCOM_DATCMD,
  259. .blksz_datactrl4 = true,
  260. .datalength_bits = 24,
  261. .pwrreg_powerup = MCI_PWR_UP,
  262. .f_max = 208000000,
  263. .explicit_mclk_control = true,
  264. .qcom_fifo = true,
  265. .qcom_dml = true,
  266. .mmcimask1 = true,
  267. .start_err = MCI_STARTBITERR,
  268. .opendrain = MCI_ROD,
  269. };
  270. /* Busy detection for the ST Micro variant */
  271. static int mmci_card_busy(struct mmc_host *mmc)
  272. {
  273. struct mmci_host *host = mmc_priv(mmc);
  274. unsigned long flags;
  275. int busy = 0;
  276. spin_lock_irqsave(&host->lock, flags);
  277. if (readl(host->base + MMCISTATUS) & host->variant->busy_detect_flag)
  278. busy = 1;
  279. spin_unlock_irqrestore(&host->lock, flags);
  280. return busy;
  281. }
  282. /*
  283. * Validate mmc prerequisites
  284. */
  285. static int mmci_validate_data(struct mmci_host *host,
  286. struct mmc_data *data)
  287. {
  288. if (!data)
  289. return 0;
  290. if (!is_power_of_2(data->blksz)) {
  291. dev_err(mmc_dev(host->mmc),
  292. "unsupported block size (%d bytes)\n", data->blksz);
  293. return -EINVAL;
  294. }
  295. return 0;
  296. }
  297. static void mmci_reg_delay(struct mmci_host *host)
  298. {
  299. /*
  300. * According to the spec, at least three feedback clock cycles
  301. * of max 52 MHz must pass between two writes to the MMCICLOCK reg.
  302. * Three MCLK clock cycles must pass between two MMCIPOWER reg writes.
  303. * Worst delay time during card init is at 100 kHz => 30 us.
  304. * Worst delay time when up and running is at 25 MHz => 120 ns.
  305. */
  306. if (host->cclk < 25000000)
  307. udelay(30);
  308. else
  309. ndelay(120);
  310. }
  311. /*
  312. * This must be called with host->lock held
  313. */
  314. static void mmci_write_clkreg(struct mmci_host *host, u32 clk)
  315. {
  316. if (host->clk_reg != clk) {
  317. host->clk_reg = clk;
  318. writel(clk, host->base + MMCICLOCK);
  319. }
  320. }
  321. /*
  322. * This must be called with host->lock held
  323. */
  324. static void mmci_write_pwrreg(struct mmci_host *host, u32 pwr)
  325. {
  326. if (host->pwr_reg != pwr) {
  327. host->pwr_reg = pwr;
  328. writel(pwr, host->base + MMCIPOWER);
  329. }
  330. }
  331. /*
  332. * This must be called with host->lock held
  333. */
  334. static void mmci_write_datactrlreg(struct mmci_host *host, u32 datactrl)
  335. {
  336. /* Keep busy mode in DPSM if enabled */
  337. datactrl |= host->datactrl_reg & host->variant->busy_dpsm_flag;
  338. if (host->datactrl_reg != datactrl) {
  339. host->datactrl_reg = datactrl;
  340. writel(datactrl, host->base + MMCIDATACTRL);
  341. }
  342. }
  343. /*
  344. * This must be called with host->lock held
  345. */
  346. static void mmci_set_clkreg(struct mmci_host *host, unsigned int desired)
  347. {
  348. struct variant_data *variant = host->variant;
  349. u32 clk = variant->clkreg;
  350. /* Make sure cclk reflects the current calculated clock */
  351. host->cclk = 0;
  352. if (desired) {
  353. if (variant->explicit_mclk_control) {
  354. host->cclk = host->mclk;
  355. } else if (desired >= host->mclk) {
  356. clk = MCI_CLK_BYPASS;
  357. if (variant->st_clkdiv)
  358. clk |= MCI_ST_UX500_NEG_EDGE;
  359. host->cclk = host->mclk;
  360. } else if (variant->st_clkdiv) {
  361. /*
  362. * DB8500 TRM says f = mclk / (clkdiv + 2)
  363. * => clkdiv = (mclk / f) - 2
  364. * Round the divider up so we don't exceed the max
  365. * frequency
  366. */
  367. clk = DIV_ROUND_UP(host->mclk, desired) - 2;
  368. if (clk >= 256)
  369. clk = 255;
  370. host->cclk = host->mclk / (clk + 2);
  371. } else {
  372. /*
  373. * PL180 TRM says f = mclk / (2 * (clkdiv + 1))
  374. * => clkdiv = mclk / (2 * f) - 1
  375. */
  376. clk = host->mclk / (2 * desired) - 1;
  377. if (clk >= 256)
  378. clk = 255;
  379. host->cclk = host->mclk / (2 * (clk + 1));
  380. }
  381. clk |= variant->clkreg_enable;
  382. clk |= MCI_CLK_ENABLE;
  383. /* This hasn't proven to be worthwhile */
  384. /* clk |= MCI_CLK_PWRSAVE; */
  385. }
  386. /* Set actual clock for debug */
  387. host->mmc->actual_clock = host->cclk;
  388. if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4)
  389. clk |= MCI_4BIT_BUS;
  390. if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_8)
  391. clk |= variant->clkreg_8bit_bus_enable;
  392. if (host->mmc->ios.timing == MMC_TIMING_UHS_DDR50 ||
  393. host->mmc->ios.timing == MMC_TIMING_MMC_DDR52)
  394. clk |= variant->clkreg_neg_edge_enable;
  395. mmci_write_clkreg(host, clk);
  396. }
  397. static void
  398. mmci_request_end(struct mmci_host *host, struct mmc_request *mrq)
  399. {
  400. writel(0, host->base + MMCICOMMAND);
  401. BUG_ON(host->data);
  402. host->mrq = NULL;
  403. host->cmd = NULL;
  404. mmc_request_done(host->mmc, mrq);
  405. }
  406. static void mmci_set_mask1(struct mmci_host *host, unsigned int mask)
  407. {
  408. void __iomem *base = host->base;
  409. struct variant_data *variant = host->variant;
  410. if (host->singleirq) {
  411. unsigned int mask0 = readl(base + MMCIMASK0);
  412. mask0 &= ~MCI_IRQ1MASK;
  413. mask0 |= mask;
  414. writel(mask0, base + MMCIMASK0);
  415. }
  416. if (variant->mmcimask1)
  417. writel(mask, base + MMCIMASK1);
  418. host->mask1_reg = mask;
  419. }
  420. static void mmci_stop_data(struct mmci_host *host)
  421. {
  422. mmci_write_datactrlreg(host, 0);
  423. mmci_set_mask1(host, 0);
  424. host->data = NULL;
  425. }
  426. static void mmci_init_sg(struct mmci_host *host, struct mmc_data *data)
  427. {
  428. unsigned int flags = SG_MITER_ATOMIC;
  429. if (data->flags & MMC_DATA_READ)
  430. flags |= SG_MITER_TO_SG;
  431. else
  432. flags |= SG_MITER_FROM_SG;
  433. sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
  434. }
  435. /*
  436. * All the DMA operation mode stuff goes inside this ifdef.
  437. * This assumes that you have a generic DMA device interface,
  438. * no custom DMA interfaces are supported.
  439. */
  440. #ifdef CONFIG_DMA_ENGINE
  441. static void mmci_dma_setup(struct mmci_host *host)
  442. {
  443. const char *rxname, *txname;
  444. struct variant_data *variant = host->variant;
  445. host->dma_rx_channel = dma_request_slave_channel(mmc_dev(host->mmc), "rx");
  446. host->dma_tx_channel = dma_request_slave_channel(mmc_dev(host->mmc), "tx");
  447. /* initialize pre request cookie */
  448. host->next_data.cookie = 1;
  449. /*
  450. * If only an RX channel is specified, the driver will
  451. * attempt to use it bidirectionally, however if it is
  452. * is specified but cannot be located, DMA will be disabled.
  453. */
  454. if (host->dma_rx_channel && !host->dma_tx_channel)
  455. host->dma_tx_channel = host->dma_rx_channel;
  456. if (host->dma_rx_channel)
  457. rxname = dma_chan_name(host->dma_rx_channel);
  458. else
  459. rxname = "none";
  460. if (host->dma_tx_channel)
  461. txname = dma_chan_name(host->dma_tx_channel);
  462. else
  463. txname = "none";
  464. dev_info(mmc_dev(host->mmc), "DMA channels RX %s, TX %s\n",
  465. rxname, txname);
  466. /*
  467. * Limit the maximum segment size in any SG entry according to
  468. * the parameters of the DMA engine device.
  469. */
  470. if (host->dma_tx_channel) {
  471. struct device *dev = host->dma_tx_channel->device->dev;
  472. unsigned int max_seg_size = dma_get_max_seg_size(dev);
  473. if (max_seg_size < host->mmc->max_seg_size)
  474. host->mmc->max_seg_size = max_seg_size;
  475. }
  476. if (host->dma_rx_channel) {
  477. struct device *dev = host->dma_rx_channel->device->dev;
  478. unsigned int max_seg_size = dma_get_max_seg_size(dev);
  479. if (max_seg_size < host->mmc->max_seg_size)
  480. host->mmc->max_seg_size = max_seg_size;
  481. }
  482. if (variant->qcom_dml && host->dma_rx_channel && host->dma_tx_channel)
  483. if (dml_hw_init(host, host->mmc->parent->of_node))
  484. variant->qcom_dml = false;
  485. }
  486. /*
  487. * This is used in or so inline it
  488. * so it can be discarded.
  489. */
  490. static inline void mmci_dma_release(struct mmci_host *host)
  491. {
  492. if (host->dma_rx_channel)
  493. dma_release_channel(host->dma_rx_channel);
  494. if (host->dma_tx_channel)
  495. dma_release_channel(host->dma_tx_channel);
  496. host->dma_rx_channel = host->dma_tx_channel = NULL;
  497. }
  498. static void mmci_dma_data_error(struct mmci_host *host)
  499. {
  500. dev_err(mmc_dev(host->mmc), "error during DMA transfer!\n");
  501. dmaengine_terminate_all(host->dma_current);
  502. host->dma_in_progress = false;
  503. host->dma_current = NULL;
  504. host->dma_desc_current = NULL;
  505. host->data->host_cookie = 0;
  506. }
  507. static void mmci_dma_unmap(struct mmci_host *host, struct mmc_data *data)
  508. {
  509. struct dma_chan *chan;
  510. if (data->flags & MMC_DATA_READ)
  511. chan = host->dma_rx_channel;
  512. else
  513. chan = host->dma_tx_channel;
  514. dma_unmap_sg(chan->device->dev, data->sg, data->sg_len,
  515. mmc_get_dma_dir(data));
  516. }
  517. static void mmci_dma_finalize(struct mmci_host *host, struct mmc_data *data)
  518. {
  519. u32 status;
  520. int i;
  521. /* Wait up to 1ms for the DMA to complete */
  522. for (i = 0; ; i++) {
  523. status = readl(host->base + MMCISTATUS);
  524. if (!(status & MCI_RXDATAAVLBLMASK) || i >= 100)
  525. break;
  526. udelay(10);
  527. }
  528. /*
  529. * Check to see whether we still have some data left in the FIFO -
  530. * this catches DMA controllers which are unable to monitor the
  531. * DMALBREQ and DMALSREQ signals while allowing us to DMA to non-
  532. * contiguous buffers. On TX, we'll get a FIFO underrun error.
  533. */
  534. if (status & MCI_RXDATAAVLBLMASK) {
  535. mmci_dma_data_error(host);
  536. if (!data->error)
  537. data->error = -EIO;
  538. }
  539. if (!data->host_cookie)
  540. mmci_dma_unmap(host, data);
  541. /*
  542. * Use of DMA with scatter-gather is impossible.
  543. * Give up with DMA and switch back to PIO mode.
  544. */
  545. if (status & MCI_RXDATAAVLBLMASK) {
  546. dev_err(mmc_dev(host->mmc), "buggy DMA detected. Taking evasive action.\n");
  547. mmci_dma_release(host);
  548. }
  549. host->dma_in_progress = false;
  550. host->dma_current = NULL;
  551. host->dma_desc_current = NULL;
  552. }
  553. /* prepares DMA channel and DMA descriptor, returns non-zero on failure */
  554. static int __mmci_dma_prep_data(struct mmci_host *host, struct mmc_data *data,
  555. struct dma_chan **dma_chan,
  556. struct dma_async_tx_descriptor **dma_desc)
  557. {
  558. struct variant_data *variant = host->variant;
  559. struct dma_slave_config conf = {
  560. .src_addr = host->phybase + MMCIFIFO,
  561. .dst_addr = host->phybase + MMCIFIFO,
  562. .src_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
  563. .dst_addr_width = DMA_SLAVE_BUSWIDTH_4_BYTES,
  564. .src_maxburst = variant->fifohalfsize >> 2, /* # of words */
  565. .dst_maxburst = variant->fifohalfsize >> 2, /* # of words */
  566. .device_fc = false,
  567. };
  568. struct dma_chan *chan;
  569. struct dma_device *device;
  570. struct dma_async_tx_descriptor *desc;
  571. int nr_sg;
  572. unsigned long flags = DMA_CTRL_ACK;
  573. if (data->flags & MMC_DATA_READ) {
  574. conf.direction = DMA_DEV_TO_MEM;
  575. chan = host->dma_rx_channel;
  576. } else {
  577. conf.direction = DMA_MEM_TO_DEV;
  578. chan = host->dma_tx_channel;
  579. }
  580. /* If there's no DMA channel, fall back to PIO */
  581. if (!chan)
  582. return -EINVAL;
  583. /* If less than or equal to the fifo size, don't bother with DMA */
  584. if (data->blksz * data->blocks <= variant->fifosize)
  585. return -EINVAL;
  586. device = chan->device;
  587. nr_sg = dma_map_sg(device->dev, data->sg, data->sg_len,
  588. mmc_get_dma_dir(data));
  589. if (nr_sg == 0)
  590. return -EINVAL;
  591. if (host->variant->qcom_dml)
  592. flags |= DMA_PREP_INTERRUPT;
  593. dmaengine_slave_config(chan, &conf);
  594. desc = dmaengine_prep_slave_sg(chan, data->sg, nr_sg,
  595. conf.direction, flags);
  596. if (!desc)
  597. goto unmap_exit;
  598. *dma_chan = chan;
  599. *dma_desc = desc;
  600. return 0;
  601. unmap_exit:
  602. dma_unmap_sg(device->dev, data->sg, data->sg_len,
  603. mmc_get_dma_dir(data));
  604. return -ENOMEM;
  605. }
  606. static inline int mmci_dma_prep_data(struct mmci_host *host,
  607. struct mmc_data *data)
  608. {
  609. /* Check if next job is already prepared. */
  610. if (host->dma_current && host->dma_desc_current)
  611. return 0;
  612. /* No job were prepared thus do it now. */
  613. return __mmci_dma_prep_data(host, data, &host->dma_current,
  614. &host->dma_desc_current);
  615. }
  616. static inline int mmci_dma_prep_next(struct mmci_host *host,
  617. struct mmc_data *data)
  618. {
  619. struct mmci_host_next *nd = &host->next_data;
  620. return __mmci_dma_prep_data(host, data, &nd->dma_chan, &nd->dma_desc);
  621. }
  622. static int mmci_dma_start_data(struct mmci_host *host, unsigned int datactrl)
  623. {
  624. int ret;
  625. struct mmc_data *data = host->data;
  626. ret = mmci_dma_prep_data(host, host->data);
  627. if (ret)
  628. return ret;
  629. /* Okay, go for it. */
  630. dev_vdbg(mmc_dev(host->mmc),
  631. "Submit MMCI DMA job, sglen %d blksz %04x blks %04x flags %08x\n",
  632. data->sg_len, data->blksz, data->blocks, data->flags);
  633. host->dma_in_progress = true;
  634. dmaengine_submit(host->dma_desc_current);
  635. dma_async_issue_pending(host->dma_current);
  636. if (host->variant->qcom_dml)
  637. dml_start_xfer(host, data);
  638. datactrl |= MCI_DPSM_DMAENABLE;
  639. /* Trigger the DMA transfer */
  640. mmci_write_datactrlreg(host, datactrl);
  641. /*
  642. * Let the MMCI say when the data is ended and it's time
  643. * to fire next DMA request. When that happens, MMCI will
  644. * call mmci_data_end()
  645. */
  646. writel(readl(host->base + MMCIMASK0) | MCI_DATAENDMASK,
  647. host->base + MMCIMASK0);
  648. return 0;
  649. }
  650. static void mmci_get_next_data(struct mmci_host *host, struct mmc_data *data)
  651. {
  652. struct mmci_host_next *next = &host->next_data;
  653. WARN_ON(data->host_cookie && data->host_cookie != next->cookie);
  654. WARN_ON(!data->host_cookie && (next->dma_desc || next->dma_chan));
  655. host->dma_desc_current = next->dma_desc;
  656. host->dma_current = next->dma_chan;
  657. next->dma_desc = NULL;
  658. next->dma_chan = NULL;
  659. }
  660. static void mmci_pre_request(struct mmc_host *mmc, struct mmc_request *mrq)
  661. {
  662. struct mmci_host *host = mmc_priv(mmc);
  663. struct mmc_data *data = mrq->data;
  664. struct mmci_host_next *nd = &host->next_data;
  665. if (!data)
  666. return;
  667. BUG_ON(data->host_cookie);
  668. if (mmci_validate_data(host, data))
  669. return;
  670. if (!mmci_dma_prep_next(host, data))
  671. data->host_cookie = ++nd->cookie < 0 ? 1 : nd->cookie;
  672. }
  673. static void mmci_post_request(struct mmc_host *mmc, struct mmc_request *mrq,
  674. int err)
  675. {
  676. struct mmci_host *host = mmc_priv(mmc);
  677. struct mmc_data *data = mrq->data;
  678. if (!data || !data->host_cookie)
  679. return;
  680. mmci_dma_unmap(host, data);
  681. if (err) {
  682. struct mmci_host_next *next = &host->next_data;
  683. struct dma_chan *chan;
  684. if (data->flags & MMC_DATA_READ)
  685. chan = host->dma_rx_channel;
  686. else
  687. chan = host->dma_tx_channel;
  688. dmaengine_terminate_all(chan);
  689. if (host->dma_desc_current == next->dma_desc)
  690. host->dma_desc_current = NULL;
  691. if (host->dma_current == next->dma_chan) {
  692. host->dma_in_progress = false;
  693. host->dma_current = NULL;
  694. }
  695. next->dma_desc = NULL;
  696. next->dma_chan = NULL;
  697. data->host_cookie = 0;
  698. }
  699. }
  700. #else
  701. /* Blank functions if the DMA engine is not available */
  702. static void mmci_get_next_data(struct mmci_host *host, struct mmc_data *data)
  703. {
  704. }
  705. static inline void mmci_dma_setup(struct mmci_host *host)
  706. {
  707. }
  708. static inline void mmci_dma_release(struct mmci_host *host)
  709. {
  710. }
  711. static inline void mmci_dma_unmap(struct mmci_host *host, struct mmc_data *data)
  712. {
  713. }
  714. static inline void mmci_dma_finalize(struct mmci_host *host,
  715. struct mmc_data *data)
  716. {
  717. }
  718. static inline void mmci_dma_data_error(struct mmci_host *host)
  719. {
  720. }
  721. static inline int mmci_dma_start_data(struct mmci_host *host, unsigned int datactrl)
  722. {
  723. return -ENOSYS;
  724. }
  725. #define mmci_pre_request NULL
  726. #define mmci_post_request NULL
  727. #endif
  728. static void mmci_start_data(struct mmci_host *host, struct mmc_data *data)
  729. {
  730. struct variant_data *variant = host->variant;
  731. unsigned int datactrl, timeout, irqmask;
  732. unsigned long long clks;
  733. void __iomem *base;
  734. int blksz_bits;
  735. dev_dbg(mmc_dev(host->mmc), "blksz %04x blks %04x flags %08x\n",
  736. data->blksz, data->blocks, data->flags);
  737. host->data = data;
  738. host->size = data->blksz * data->blocks;
  739. data->bytes_xfered = 0;
  740. clks = (unsigned long long)data->timeout_ns * host->cclk;
  741. do_div(clks, NSEC_PER_SEC);
  742. timeout = data->timeout_clks + (unsigned int)clks;
  743. base = host->base;
  744. writel(timeout, base + MMCIDATATIMER);
  745. writel(host->size, base + MMCIDATALENGTH);
  746. blksz_bits = ffs(data->blksz) - 1;
  747. BUG_ON(1 << blksz_bits != data->blksz);
  748. if (variant->blksz_datactrl16)
  749. datactrl = MCI_DPSM_ENABLE | (data->blksz << 16);
  750. else if (variant->blksz_datactrl4)
  751. datactrl = MCI_DPSM_ENABLE | (data->blksz << 4);
  752. else
  753. datactrl = MCI_DPSM_ENABLE | blksz_bits << 4;
  754. if (data->flags & MMC_DATA_READ)
  755. datactrl |= MCI_DPSM_DIRECTION;
  756. if (host->mmc->card && mmc_card_sdio(host->mmc->card)) {
  757. u32 clk;
  758. datactrl |= variant->datactrl_mask_sdio;
  759. /*
  760. * The ST Micro variant for SDIO small write transfers
  761. * needs to have clock H/W flow control disabled,
  762. * otherwise the transfer will not start. The threshold
  763. * depends on the rate of MCLK.
  764. */
  765. if (variant->st_sdio && data->flags & MMC_DATA_WRITE &&
  766. (host->size < 8 ||
  767. (host->size <= 8 && host->mclk > 50000000)))
  768. clk = host->clk_reg & ~variant->clkreg_enable;
  769. else
  770. clk = host->clk_reg | variant->clkreg_enable;
  771. mmci_write_clkreg(host, clk);
  772. }
  773. if (host->mmc->ios.timing == MMC_TIMING_UHS_DDR50 ||
  774. host->mmc->ios.timing == MMC_TIMING_MMC_DDR52)
  775. datactrl |= variant->datactrl_mask_ddrmode;
  776. /*
  777. * Attempt to use DMA operation mode, if this
  778. * should fail, fall back to PIO mode
  779. */
  780. if (!mmci_dma_start_data(host, datactrl))
  781. return;
  782. /* IRQ mode, map the SG list for CPU reading/writing */
  783. mmci_init_sg(host, data);
  784. if (data->flags & MMC_DATA_READ) {
  785. irqmask = MCI_RXFIFOHALFFULLMASK;
  786. /*
  787. * If we have less than the fifo 'half-full' threshold to
  788. * transfer, trigger a PIO interrupt as soon as any data
  789. * is available.
  790. */
  791. if (host->size < variant->fifohalfsize)
  792. irqmask |= MCI_RXDATAAVLBLMASK;
  793. } else {
  794. /*
  795. * We don't actually need to include "FIFO empty" here
  796. * since its implicit in "FIFO half empty".
  797. */
  798. irqmask = MCI_TXFIFOHALFEMPTYMASK;
  799. }
  800. mmci_write_datactrlreg(host, datactrl);
  801. writel(readl(base + MMCIMASK0) & ~MCI_DATAENDMASK, base + MMCIMASK0);
  802. mmci_set_mask1(host, irqmask);
  803. }
  804. static void
  805. mmci_start_command(struct mmci_host *host, struct mmc_command *cmd, u32 c)
  806. {
  807. void __iomem *base = host->base;
  808. dev_dbg(mmc_dev(host->mmc), "op %02x arg %08x flags %08x\n",
  809. cmd->opcode, cmd->arg, cmd->flags);
  810. if (readl(base + MMCICOMMAND) & MCI_CPSM_ENABLE) {
  811. writel(0, base + MMCICOMMAND);
  812. mmci_reg_delay(host);
  813. }
  814. c |= cmd->opcode | MCI_CPSM_ENABLE;
  815. if (cmd->flags & MMC_RSP_PRESENT) {
  816. if (cmd->flags & MMC_RSP_136)
  817. c |= MCI_CPSM_LONGRSP;
  818. c |= MCI_CPSM_RESPONSE;
  819. }
  820. if (/*interrupt*/0)
  821. c |= MCI_CPSM_INTERRUPT;
  822. if (mmc_cmd_type(cmd) == MMC_CMD_ADTC)
  823. c |= host->variant->data_cmd_enable;
  824. host->cmd = cmd;
  825. writel(cmd->arg, base + MMCIARGUMENT);
  826. writel(c, base + MMCICOMMAND);
  827. }
  828. static void
  829. mmci_data_irq(struct mmci_host *host, struct mmc_data *data,
  830. unsigned int status)
  831. {
  832. /* Make sure we have data to handle */
  833. if (!data)
  834. return;
  835. /* First check for errors */
  836. if (status & (MCI_DATACRCFAIL | MCI_DATATIMEOUT |
  837. host->variant->start_err |
  838. MCI_TXUNDERRUN | MCI_RXOVERRUN)) {
  839. u32 remain, success;
  840. /* Terminate the DMA transfer */
  841. if (dma_inprogress(host)) {
  842. mmci_dma_data_error(host);
  843. mmci_dma_unmap(host, data);
  844. }
  845. /*
  846. * Calculate how far we are into the transfer. Note that
  847. * the data counter gives the number of bytes transferred
  848. * on the MMC bus, not on the host side. On reads, this
  849. * can be as much as a FIFO-worth of data ahead. This
  850. * matters for FIFO overruns only.
  851. */
  852. remain = readl(host->base + MMCIDATACNT);
  853. success = data->blksz * data->blocks - remain;
  854. dev_dbg(mmc_dev(host->mmc), "MCI ERROR IRQ, status 0x%08x at 0x%08x\n",
  855. status, success);
  856. if (status & MCI_DATACRCFAIL) {
  857. /* Last block was not successful */
  858. success -= 1;
  859. data->error = -EILSEQ;
  860. } else if (status & MCI_DATATIMEOUT) {
  861. data->error = -ETIMEDOUT;
  862. } else if (status & MCI_STARTBITERR) {
  863. data->error = -ECOMM;
  864. } else if (status & MCI_TXUNDERRUN) {
  865. data->error = -EIO;
  866. } else if (status & MCI_RXOVERRUN) {
  867. if (success > host->variant->fifosize)
  868. success -= host->variant->fifosize;
  869. else
  870. success = 0;
  871. data->error = -EIO;
  872. }
  873. data->bytes_xfered = round_down(success, data->blksz);
  874. }
  875. if (status & MCI_DATABLOCKEND)
  876. dev_err(mmc_dev(host->mmc), "stray MCI_DATABLOCKEND interrupt\n");
  877. if (status & MCI_DATAEND || data->error) {
  878. if (dma_inprogress(host))
  879. mmci_dma_finalize(host, data);
  880. mmci_stop_data(host);
  881. if (!data->error)
  882. /* The error clause is handled above, success! */
  883. data->bytes_xfered = data->blksz * data->blocks;
  884. if (!data->stop || host->mrq->sbc) {
  885. mmci_request_end(host, data->mrq);
  886. } else {
  887. mmci_start_command(host, data->stop, 0);
  888. }
  889. }
  890. }
  891. static void
  892. mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd,
  893. unsigned int status)
  894. {
  895. void __iomem *base = host->base;
  896. bool sbc;
  897. if (!cmd)
  898. return;
  899. sbc = (cmd == host->mrq->sbc);
  900. /*
  901. * We need to be one of these interrupts to be considered worth
  902. * handling. Note that we tag on any latent IRQs postponed
  903. * due to waiting for busy status.
  904. */
  905. if (!((status|host->busy_status) &
  906. (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT|MCI_CMDSENT|MCI_CMDRESPEND)))
  907. return;
  908. /*
  909. * ST Micro variant: handle busy detection.
  910. */
  911. if (host->variant->busy_detect) {
  912. bool busy_resp = !!(cmd->flags & MMC_RSP_BUSY);
  913. /* We are busy with a command, return */
  914. if (host->busy_status &&
  915. (status & host->variant->busy_detect_flag))
  916. return;
  917. /*
  918. * We were not busy, but we now got a busy response on
  919. * something that was not an error, and we double-check
  920. * that the special busy status bit is still set before
  921. * proceeding.
  922. */
  923. if (!host->busy_status && busy_resp &&
  924. !(status & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT)) &&
  925. (readl(base + MMCISTATUS) & host->variant->busy_detect_flag)) {
  926. /* Clear the busy start IRQ */
  927. writel(host->variant->busy_detect_mask,
  928. host->base + MMCICLEAR);
  929. /* Unmask the busy end IRQ */
  930. writel(readl(base + MMCIMASK0) |
  931. host->variant->busy_detect_mask,
  932. base + MMCIMASK0);
  933. /*
  934. * Now cache the last response status code (until
  935. * the busy bit goes low), and return.
  936. */
  937. host->busy_status =
  938. status & (MCI_CMDSENT|MCI_CMDRESPEND);
  939. return;
  940. }
  941. /*
  942. * At this point we are not busy with a command, we have
  943. * not received a new busy request, clear and mask the busy
  944. * end IRQ and fall through to process the IRQ.
  945. */
  946. if (host->busy_status) {
  947. writel(host->variant->busy_detect_mask,
  948. host->base + MMCICLEAR);
  949. writel(readl(base + MMCIMASK0) &
  950. ~host->variant->busy_detect_mask,
  951. base + MMCIMASK0);
  952. host->busy_status = 0;
  953. }
  954. }
  955. host->cmd = NULL;
  956. if (status & MCI_CMDTIMEOUT) {
  957. cmd->error = -ETIMEDOUT;
  958. } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) {
  959. cmd->error = -EILSEQ;
  960. } else {
  961. cmd->resp[0] = readl(base + MMCIRESPONSE0);
  962. cmd->resp[1] = readl(base + MMCIRESPONSE1);
  963. cmd->resp[2] = readl(base + MMCIRESPONSE2);
  964. cmd->resp[3] = readl(base + MMCIRESPONSE3);
  965. }
  966. if ((!sbc && !cmd->data) || cmd->error) {
  967. if (host->data) {
  968. /* Terminate the DMA transfer */
  969. if (dma_inprogress(host)) {
  970. mmci_dma_data_error(host);
  971. mmci_dma_unmap(host, host->data);
  972. }
  973. mmci_stop_data(host);
  974. }
  975. mmci_request_end(host, host->mrq);
  976. } else if (sbc) {
  977. mmci_start_command(host, host->mrq->cmd, 0);
  978. } else if (!(cmd->data->flags & MMC_DATA_READ)) {
  979. mmci_start_data(host, cmd->data);
  980. }
  981. }
  982. static int mmci_get_rx_fifocnt(struct mmci_host *host, u32 status, int remain)
  983. {
  984. return remain - (readl(host->base + MMCIFIFOCNT) << 2);
  985. }
  986. static int mmci_qcom_get_rx_fifocnt(struct mmci_host *host, u32 status, int r)
  987. {
  988. /*
  989. * on qcom SDCC4 only 8 words are used in each burst so only 8 addresses
  990. * from the fifo range should be used
  991. */
  992. if (status & MCI_RXFIFOHALFFULL)
  993. return host->variant->fifohalfsize;
  994. else if (status & MCI_RXDATAAVLBL)
  995. return 4;
  996. return 0;
  997. }
  998. static int mmci_pio_read(struct mmci_host *host, char *buffer, unsigned int remain)
  999. {
  1000. void __iomem *base = host->base;
  1001. char *ptr = buffer;
  1002. u32 status = readl(host->base + MMCISTATUS);
  1003. int host_remain = host->size;
  1004. do {
  1005. int count = host->get_rx_fifocnt(host, status, host_remain);
  1006. if (count > remain)
  1007. count = remain;
  1008. if (count <= 0)
  1009. break;
  1010. /*
  1011. * SDIO especially may want to send something that is
  1012. * not divisible by 4 (as opposed to card sectors
  1013. * etc). Therefore make sure to always read the last bytes
  1014. * while only doing full 32-bit reads towards the FIFO.
  1015. */
  1016. if (unlikely(count & 0x3)) {
  1017. if (count < 4) {
  1018. unsigned char buf[4];
  1019. ioread32_rep(base + MMCIFIFO, buf, 1);
  1020. memcpy(ptr, buf, count);
  1021. } else {
  1022. ioread32_rep(base + MMCIFIFO, ptr, count >> 2);
  1023. count &= ~0x3;
  1024. }
  1025. } else {
  1026. ioread32_rep(base + MMCIFIFO, ptr, count >> 2);
  1027. }
  1028. ptr += count;
  1029. remain -= count;
  1030. host_remain -= count;
  1031. if (remain == 0)
  1032. break;
  1033. status = readl(base + MMCISTATUS);
  1034. } while (status & MCI_RXDATAAVLBL);
  1035. return ptr - buffer;
  1036. }
  1037. static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int remain, u32 status)
  1038. {
  1039. struct variant_data *variant = host->variant;
  1040. void __iomem *base = host->base;
  1041. char *ptr = buffer;
  1042. do {
  1043. unsigned int count, maxcnt;
  1044. maxcnt = status & MCI_TXFIFOEMPTY ?
  1045. variant->fifosize : variant->fifohalfsize;
  1046. count = min(remain, maxcnt);
  1047. /*
  1048. * SDIO especially may want to send something that is
  1049. * not divisible by 4 (as opposed to card sectors
  1050. * etc), and the FIFO only accept full 32-bit writes.
  1051. * So compensate by adding +3 on the count, a single
  1052. * byte become a 32bit write, 7 bytes will be two
  1053. * 32bit writes etc.
  1054. */
  1055. iowrite32_rep(base + MMCIFIFO, ptr, (count + 3) >> 2);
  1056. ptr += count;
  1057. remain -= count;
  1058. if (remain == 0)
  1059. break;
  1060. status = readl(base + MMCISTATUS);
  1061. } while (status & MCI_TXFIFOHALFEMPTY);
  1062. return ptr - buffer;
  1063. }
  1064. /*
  1065. * PIO data transfer IRQ handler.
  1066. */
  1067. static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
  1068. {
  1069. struct mmci_host *host = dev_id;
  1070. struct sg_mapping_iter *sg_miter = &host->sg_miter;
  1071. struct variant_data *variant = host->variant;
  1072. void __iomem *base = host->base;
  1073. u32 status;
  1074. status = readl(base + MMCISTATUS);
  1075. dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status);
  1076. do {
  1077. unsigned int remain, len;
  1078. char *buffer;
  1079. /*
  1080. * For write, we only need to test the half-empty flag
  1081. * here - if the FIFO is completely empty, then by
  1082. * definition it is more than half empty.
  1083. *
  1084. * For read, check for data available.
  1085. */
  1086. if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL)))
  1087. break;
  1088. if (!sg_miter_next(sg_miter))
  1089. break;
  1090. buffer = sg_miter->addr;
  1091. remain = sg_miter->length;
  1092. len = 0;
  1093. if (status & MCI_RXACTIVE)
  1094. len = mmci_pio_read(host, buffer, remain);
  1095. if (status & MCI_TXACTIVE)
  1096. len = mmci_pio_write(host, buffer, remain, status);
  1097. sg_miter->consumed = len;
  1098. host->size -= len;
  1099. remain -= len;
  1100. if (remain)
  1101. break;
  1102. status = readl(base + MMCISTATUS);
  1103. } while (1);
  1104. sg_miter_stop(sg_miter);
  1105. /*
  1106. * If we have less than the fifo 'half-full' threshold to transfer,
  1107. * trigger a PIO interrupt as soon as any data is available.
  1108. */
  1109. if (status & MCI_RXACTIVE && host->size < variant->fifohalfsize)
  1110. mmci_set_mask1(host, MCI_RXDATAAVLBLMASK);
  1111. /*
  1112. * If we run out of data, disable the data IRQs; this
  1113. * prevents a race where the FIFO becomes empty before
  1114. * the chip itself has disabled the data path, and
  1115. * stops us racing with our data end IRQ.
  1116. */
  1117. if (host->size == 0) {
  1118. mmci_set_mask1(host, 0);
  1119. writel(readl(base + MMCIMASK0) | MCI_DATAENDMASK, base + MMCIMASK0);
  1120. }
  1121. return IRQ_HANDLED;
  1122. }
  1123. /*
  1124. * Handle completion of command and data transfers.
  1125. */
  1126. static irqreturn_t mmci_irq(int irq, void *dev_id)
  1127. {
  1128. struct mmci_host *host = dev_id;
  1129. u32 status;
  1130. int ret = 0;
  1131. spin_lock(&host->lock);
  1132. do {
  1133. status = readl(host->base + MMCISTATUS);
  1134. if (host->singleirq) {
  1135. if (status & host->mask1_reg)
  1136. mmci_pio_irq(irq, dev_id);
  1137. status &= ~MCI_IRQ1MASK;
  1138. }
  1139. /*
  1140. * We intentionally clear the MCI_ST_CARDBUSY IRQ (if it's
  1141. * enabled) in mmci_cmd_irq() function where ST Micro busy
  1142. * detection variant is handled. Considering the HW seems to be
  1143. * triggering the IRQ on both edges while monitoring DAT0 for
  1144. * busy completion and that same status bit is used to monitor
  1145. * start and end of busy detection, special care must be taken
  1146. * to make sure that both start and end interrupts are always
  1147. * cleared one after the other.
  1148. */
  1149. status &= readl(host->base + MMCIMASK0);
  1150. if (host->variant->busy_detect)
  1151. writel(status & ~host->variant->busy_detect_mask,
  1152. host->base + MMCICLEAR);
  1153. else
  1154. writel(status, host->base + MMCICLEAR);
  1155. dev_dbg(mmc_dev(host->mmc), "irq0 (data+cmd) %08x\n", status);
  1156. if (host->variant->reversed_irq_handling) {
  1157. mmci_data_irq(host, host->data, status);
  1158. mmci_cmd_irq(host, host->cmd, status);
  1159. } else {
  1160. mmci_cmd_irq(host, host->cmd, status);
  1161. mmci_data_irq(host, host->data, status);
  1162. }
  1163. /*
  1164. * Don't poll for busy completion in irq context.
  1165. */
  1166. if (host->variant->busy_detect && host->busy_status)
  1167. status &= ~host->variant->busy_detect_flag;
  1168. ret = 1;
  1169. } while (status);
  1170. spin_unlock(&host->lock);
  1171. return IRQ_RETVAL(ret);
  1172. }
  1173. static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
  1174. {
  1175. struct mmci_host *host = mmc_priv(mmc);
  1176. unsigned long flags;
  1177. WARN_ON(host->mrq != NULL);
  1178. mrq->cmd->error = mmci_validate_data(host, mrq->data);
  1179. if (mrq->cmd->error) {
  1180. mmc_request_done(mmc, mrq);
  1181. return;
  1182. }
  1183. spin_lock_irqsave(&host->lock, flags);
  1184. host->mrq = mrq;
  1185. if (mrq->data)
  1186. mmci_get_next_data(host, mrq->data);
  1187. if (mrq->data && mrq->data->flags & MMC_DATA_READ)
  1188. mmci_start_data(host, mrq->data);
  1189. if (mrq->sbc)
  1190. mmci_start_command(host, mrq->sbc, 0);
  1191. else
  1192. mmci_start_command(host, mrq->cmd, 0);
  1193. spin_unlock_irqrestore(&host->lock, flags);
  1194. }
  1195. static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  1196. {
  1197. struct mmci_host *host = mmc_priv(mmc);
  1198. struct variant_data *variant = host->variant;
  1199. u32 pwr = 0;
  1200. unsigned long flags;
  1201. int ret;
  1202. if (host->plat->ios_handler &&
  1203. host->plat->ios_handler(mmc_dev(mmc), ios))
  1204. dev_err(mmc_dev(mmc), "platform ios_handler failed\n");
  1205. switch (ios->power_mode) {
  1206. case MMC_POWER_OFF:
  1207. if (!IS_ERR(mmc->supply.vmmc))
  1208. mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
  1209. if (!IS_ERR(mmc->supply.vqmmc) && host->vqmmc_enabled) {
  1210. regulator_disable(mmc->supply.vqmmc);
  1211. host->vqmmc_enabled = false;
  1212. }
  1213. break;
  1214. case MMC_POWER_UP:
  1215. if (!IS_ERR(mmc->supply.vmmc))
  1216. mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, ios->vdd);
  1217. /*
  1218. * The ST Micro variant doesn't have the PL180s MCI_PWR_UP
  1219. * and instead uses MCI_PWR_ON so apply whatever value is
  1220. * configured in the variant data.
  1221. */
  1222. pwr |= variant->pwrreg_powerup;
  1223. break;
  1224. case MMC_POWER_ON:
  1225. if (!IS_ERR(mmc->supply.vqmmc) && !host->vqmmc_enabled) {
  1226. ret = regulator_enable(mmc->supply.vqmmc);
  1227. if (ret < 0)
  1228. dev_err(mmc_dev(mmc),
  1229. "failed to enable vqmmc regulator\n");
  1230. else
  1231. host->vqmmc_enabled = true;
  1232. }
  1233. pwr |= MCI_PWR_ON;
  1234. break;
  1235. }
  1236. if (variant->signal_direction && ios->power_mode != MMC_POWER_OFF) {
  1237. /*
  1238. * The ST Micro variant has some additional bits
  1239. * indicating signal direction for the signals in
  1240. * the SD/MMC bus and feedback-clock usage.
  1241. */
  1242. pwr |= host->pwr_reg_add;
  1243. if (ios->bus_width == MMC_BUS_WIDTH_4)
  1244. pwr &= ~MCI_ST_DATA74DIREN;
  1245. else if (ios->bus_width == MMC_BUS_WIDTH_1)
  1246. pwr &= (~MCI_ST_DATA74DIREN &
  1247. ~MCI_ST_DATA31DIREN &
  1248. ~MCI_ST_DATA2DIREN);
  1249. }
  1250. if (variant->opendrain) {
  1251. if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
  1252. pwr |= variant->opendrain;
  1253. } else {
  1254. /*
  1255. * If the variant cannot configure the pads by its own, then we
  1256. * expect the pinctrl to be able to do that for us
  1257. */
  1258. if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
  1259. pinctrl_select_state(host->pinctrl, host->pins_opendrain);
  1260. else
  1261. pinctrl_select_state(host->pinctrl, host->pins_default);
  1262. }
  1263. /*
  1264. * If clock = 0 and the variant requires the MMCIPOWER to be used for
  1265. * gating the clock, the MCI_PWR_ON bit is cleared.
  1266. */
  1267. if (!ios->clock && variant->pwrreg_clkgate)
  1268. pwr &= ~MCI_PWR_ON;
  1269. if (host->variant->explicit_mclk_control &&
  1270. ios->clock != host->clock_cache) {
  1271. ret = clk_set_rate(host->clk, ios->clock);
  1272. if (ret < 0)
  1273. dev_err(mmc_dev(host->mmc),
  1274. "Error setting clock rate (%d)\n", ret);
  1275. else
  1276. host->mclk = clk_get_rate(host->clk);
  1277. }
  1278. host->clock_cache = ios->clock;
  1279. spin_lock_irqsave(&host->lock, flags);
  1280. mmci_set_clkreg(host, ios->clock);
  1281. mmci_write_pwrreg(host, pwr);
  1282. mmci_reg_delay(host);
  1283. spin_unlock_irqrestore(&host->lock, flags);
  1284. }
  1285. static int mmci_get_cd(struct mmc_host *mmc)
  1286. {
  1287. struct mmci_host *host = mmc_priv(mmc);
  1288. struct mmci_platform_data *plat = host->plat;
  1289. unsigned int status = mmc_gpio_get_cd(mmc);
  1290. if (status == -ENOSYS) {
  1291. if (!plat->status)
  1292. return 1; /* Assume always present */
  1293. status = plat->status(mmc_dev(host->mmc));
  1294. }
  1295. return status;
  1296. }
  1297. static int mmci_sig_volt_switch(struct mmc_host *mmc, struct mmc_ios *ios)
  1298. {
  1299. int ret = 0;
  1300. if (!IS_ERR(mmc->supply.vqmmc)) {
  1301. switch (ios->signal_voltage) {
  1302. case MMC_SIGNAL_VOLTAGE_330:
  1303. ret = regulator_set_voltage(mmc->supply.vqmmc,
  1304. 2700000, 3600000);
  1305. break;
  1306. case MMC_SIGNAL_VOLTAGE_180:
  1307. ret = regulator_set_voltage(mmc->supply.vqmmc,
  1308. 1700000, 1950000);
  1309. break;
  1310. case MMC_SIGNAL_VOLTAGE_120:
  1311. ret = regulator_set_voltage(mmc->supply.vqmmc,
  1312. 1100000, 1300000);
  1313. break;
  1314. }
  1315. if (ret)
  1316. dev_warn(mmc_dev(mmc), "Voltage switch failed\n");
  1317. }
  1318. return ret;
  1319. }
  1320. static struct mmc_host_ops mmci_ops = {
  1321. .request = mmci_request,
  1322. .pre_req = mmci_pre_request,
  1323. .post_req = mmci_post_request,
  1324. .set_ios = mmci_set_ios,
  1325. .get_ro = mmc_gpio_get_ro,
  1326. .get_cd = mmci_get_cd,
  1327. .start_signal_voltage_switch = mmci_sig_volt_switch,
  1328. };
  1329. static int mmci_of_parse(struct device_node *np, struct mmc_host *mmc)
  1330. {
  1331. struct mmci_host *host = mmc_priv(mmc);
  1332. int ret = mmc_of_parse(mmc);
  1333. if (ret)
  1334. return ret;
  1335. if (of_get_property(np, "st,sig-dir-dat0", NULL))
  1336. host->pwr_reg_add |= MCI_ST_DATA0DIREN;
  1337. if (of_get_property(np, "st,sig-dir-dat2", NULL))
  1338. host->pwr_reg_add |= MCI_ST_DATA2DIREN;
  1339. if (of_get_property(np, "st,sig-dir-dat31", NULL))
  1340. host->pwr_reg_add |= MCI_ST_DATA31DIREN;
  1341. if (of_get_property(np, "st,sig-dir-dat74", NULL))
  1342. host->pwr_reg_add |= MCI_ST_DATA74DIREN;
  1343. if (of_get_property(np, "st,sig-dir-cmd", NULL))
  1344. host->pwr_reg_add |= MCI_ST_CMDDIREN;
  1345. if (of_get_property(np, "st,sig-pin-fbclk", NULL))
  1346. host->pwr_reg_add |= MCI_ST_FBCLKEN;
  1347. if (of_get_property(np, "mmc-cap-mmc-highspeed", NULL))
  1348. mmc->caps |= MMC_CAP_MMC_HIGHSPEED;
  1349. if (of_get_property(np, "mmc-cap-sd-highspeed", NULL))
  1350. mmc->caps |= MMC_CAP_SD_HIGHSPEED;
  1351. return 0;
  1352. }
  1353. static int mmci_probe(struct amba_device *dev,
  1354. const struct amba_id *id)
  1355. {
  1356. struct mmci_platform_data *plat = dev->dev.platform_data;
  1357. struct device_node *np = dev->dev.of_node;
  1358. struct variant_data *variant = id->data;
  1359. struct mmci_host *host;
  1360. struct mmc_host *mmc;
  1361. int ret;
  1362. /* Must have platform data or Device Tree. */
  1363. if (!plat && !np) {
  1364. dev_err(&dev->dev, "No plat data or DT found\n");
  1365. return -EINVAL;
  1366. }
  1367. if (!plat) {
  1368. plat = devm_kzalloc(&dev->dev, sizeof(*plat), GFP_KERNEL);
  1369. if (!plat)
  1370. return -ENOMEM;
  1371. }
  1372. mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
  1373. if (!mmc)
  1374. return -ENOMEM;
  1375. ret = mmci_of_parse(np, mmc);
  1376. if (ret)
  1377. goto host_free;
  1378. host = mmc_priv(mmc);
  1379. host->mmc = mmc;
  1380. /*
  1381. * Some variant (STM32) doesn't have opendrain bit, nevertheless
  1382. * pins can be set accordingly using pinctrl
  1383. */
  1384. if (!variant->opendrain) {
  1385. host->pinctrl = devm_pinctrl_get(&dev->dev);
  1386. if (IS_ERR(host->pinctrl)) {
  1387. dev_err(&dev->dev, "failed to get pinctrl");
  1388. ret = PTR_ERR(host->pinctrl);
  1389. goto host_free;
  1390. }
  1391. host->pins_default = pinctrl_lookup_state(host->pinctrl,
  1392. PINCTRL_STATE_DEFAULT);
  1393. if (IS_ERR(host->pins_default)) {
  1394. dev_err(mmc_dev(mmc), "Can't select default pins\n");
  1395. ret = PTR_ERR(host->pins_default);
  1396. goto host_free;
  1397. }
  1398. host->pins_opendrain = pinctrl_lookup_state(host->pinctrl,
  1399. MMCI_PINCTRL_STATE_OPENDRAIN);
  1400. if (IS_ERR(host->pins_opendrain)) {
  1401. dev_err(mmc_dev(mmc), "Can't select opendrain pins\n");
  1402. ret = PTR_ERR(host->pins_opendrain);
  1403. goto host_free;
  1404. }
  1405. }
  1406. host->hw_designer = amba_manf(dev);
  1407. host->hw_revision = amba_rev(dev);
  1408. dev_dbg(mmc_dev(mmc), "designer ID = 0x%02x\n", host->hw_designer);
  1409. dev_dbg(mmc_dev(mmc), "revision = 0x%01x\n", host->hw_revision);
  1410. host->clk = devm_clk_get(&dev->dev, NULL);
  1411. if (IS_ERR(host->clk)) {
  1412. ret = PTR_ERR(host->clk);
  1413. goto host_free;
  1414. }
  1415. ret = clk_prepare_enable(host->clk);
  1416. if (ret)
  1417. goto host_free;
  1418. if (variant->qcom_fifo)
  1419. host->get_rx_fifocnt = mmci_qcom_get_rx_fifocnt;
  1420. else
  1421. host->get_rx_fifocnt = mmci_get_rx_fifocnt;
  1422. host->plat = plat;
  1423. host->variant = variant;
  1424. host->mclk = clk_get_rate(host->clk);
  1425. /*
  1426. * According to the spec, mclk is max 100 MHz,
  1427. * so we try to adjust the clock down to this,
  1428. * (if possible).
  1429. */
  1430. if (host->mclk > variant->f_max) {
  1431. ret = clk_set_rate(host->clk, variant->f_max);
  1432. if (ret < 0)
  1433. goto clk_disable;
  1434. host->mclk = clk_get_rate(host->clk);
  1435. dev_dbg(mmc_dev(mmc), "eventual mclk rate: %u Hz\n",
  1436. host->mclk);
  1437. }
  1438. host->phybase = dev->res.start;
  1439. host->base = devm_ioremap_resource(&dev->dev, &dev->res);
  1440. if (IS_ERR(host->base)) {
  1441. ret = PTR_ERR(host->base);
  1442. goto clk_disable;
  1443. }
  1444. /*
  1445. * The ARM and ST versions of the block have slightly different
  1446. * clock divider equations which means that the minimum divider
  1447. * differs too.
  1448. * on Qualcomm like controllers get the nearest minimum clock to 100Khz
  1449. */
  1450. if (variant->st_clkdiv)
  1451. mmc->f_min = DIV_ROUND_UP(host->mclk, 257);
  1452. else if (variant->explicit_mclk_control)
  1453. mmc->f_min = clk_round_rate(host->clk, 100000);
  1454. else
  1455. mmc->f_min = DIV_ROUND_UP(host->mclk, 512);
  1456. /*
  1457. * If no maximum operating frequency is supplied, fall back to use
  1458. * the module parameter, which has a (low) default value in case it
  1459. * is not specified. Either value must not exceed the clock rate into
  1460. * the block, of course.
  1461. */
  1462. if (mmc->f_max)
  1463. mmc->f_max = variant->explicit_mclk_control ?
  1464. min(variant->f_max, mmc->f_max) :
  1465. min(host->mclk, mmc->f_max);
  1466. else
  1467. mmc->f_max = variant->explicit_mclk_control ?
  1468. fmax : min(host->mclk, fmax);
  1469. dev_dbg(mmc_dev(mmc), "clocking block at %u Hz\n", mmc->f_max);
  1470. /* Get regulators and the supported OCR mask */
  1471. ret = mmc_regulator_get_supply(mmc);
  1472. if (ret)
  1473. goto clk_disable;
  1474. if (!mmc->ocr_avail)
  1475. mmc->ocr_avail = plat->ocr_mask;
  1476. else if (plat->ocr_mask)
  1477. dev_warn(mmc_dev(mmc), "Platform OCR mask is ignored\n");
  1478. /* DT takes precedence over platform data. */
  1479. if (!np) {
  1480. if (!plat->cd_invert)
  1481. mmc->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
  1482. mmc->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
  1483. }
  1484. /* We support these capabilities. */
  1485. mmc->caps |= MMC_CAP_CMD23;
  1486. /*
  1487. * Enable busy detection.
  1488. */
  1489. if (variant->busy_detect) {
  1490. mmci_ops.card_busy = mmci_card_busy;
  1491. /*
  1492. * Not all variants have a flag to enable busy detection
  1493. * in the DPSM, but if they do, set it here.
  1494. */
  1495. if (variant->busy_dpsm_flag)
  1496. mmci_write_datactrlreg(host,
  1497. host->variant->busy_dpsm_flag);
  1498. mmc->caps |= MMC_CAP_WAIT_WHILE_BUSY;
  1499. mmc->max_busy_timeout = 0;
  1500. }
  1501. mmc->ops = &mmci_ops;
  1502. /* We support these PM capabilities. */
  1503. mmc->pm_caps |= MMC_PM_KEEP_POWER;
  1504. /*
  1505. * We can do SGIO
  1506. */
  1507. mmc->max_segs = NR_SG;
  1508. /*
  1509. * Since only a certain number of bits are valid in the data length
  1510. * register, we must ensure that we don't exceed 2^num-1 bytes in a
  1511. * single request.
  1512. */
  1513. mmc->max_req_size = (1 << variant->datalength_bits) - 1;
  1514. /*
  1515. * Set the maximum segment size. Since we aren't doing DMA
  1516. * (yet) we are only limited by the data length register.
  1517. */
  1518. mmc->max_seg_size = mmc->max_req_size;
  1519. /*
  1520. * Block size can be up to 2048 bytes, but must be a power of two.
  1521. */
  1522. mmc->max_blk_size = 1 << 11;
  1523. /*
  1524. * Limit the number of blocks transferred so that we don't overflow
  1525. * the maximum request size.
  1526. */
  1527. mmc->max_blk_count = mmc->max_req_size >> 11;
  1528. spin_lock_init(&host->lock);
  1529. writel(0, host->base + MMCIMASK0);
  1530. if (variant->mmcimask1)
  1531. writel(0, host->base + MMCIMASK1);
  1532. writel(0xfff, host->base + MMCICLEAR);
  1533. /*
  1534. * If:
  1535. * - not using DT but using a descriptor table, or
  1536. * - using a table of descriptors ALONGSIDE DT, or
  1537. * look up these descriptors named "cd" and "wp" right here, fail
  1538. * silently of these do not exist and proceed to try platform data
  1539. */
  1540. if (!np) {
  1541. ret = mmc_gpiod_request_cd(mmc, "cd", 0, false, 0, NULL);
  1542. if (ret < 0) {
  1543. if (ret == -EPROBE_DEFER)
  1544. goto clk_disable;
  1545. else if (gpio_is_valid(plat->gpio_cd)) {
  1546. ret = mmc_gpio_request_cd(mmc, plat->gpio_cd, 0);
  1547. if (ret)
  1548. goto clk_disable;
  1549. }
  1550. }
  1551. ret = mmc_gpiod_request_ro(mmc, "wp", 0, false, 0, NULL);
  1552. if (ret < 0) {
  1553. if (ret == -EPROBE_DEFER)
  1554. goto clk_disable;
  1555. else if (gpio_is_valid(plat->gpio_wp)) {
  1556. ret = mmc_gpio_request_ro(mmc, plat->gpio_wp);
  1557. if (ret)
  1558. goto clk_disable;
  1559. }
  1560. }
  1561. }
  1562. ret = devm_request_irq(&dev->dev, dev->irq[0], mmci_irq, IRQF_SHARED,
  1563. DRIVER_NAME " (cmd)", host);
  1564. if (ret)
  1565. goto clk_disable;
  1566. if (!dev->irq[1])
  1567. host->singleirq = true;
  1568. else {
  1569. ret = devm_request_irq(&dev->dev, dev->irq[1], mmci_pio_irq,
  1570. IRQF_SHARED, DRIVER_NAME " (pio)", host);
  1571. if (ret)
  1572. goto clk_disable;
  1573. }
  1574. writel(MCI_IRQENABLE, host->base + MMCIMASK0);
  1575. amba_set_drvdata(dev, mmc);
  1576. dev_info(&dev->dev, "%s: PL%03x manf %x rev%u at 0x%08llx irq %d,%d (pio)\n",
  1577. mmc_hostname(mmc), amba_part(dev), amba_manf(dev),
  1578. amba_rev(dev), (unsigned long long)dev->res.start,
  1579. dev->irq[0], dev->irq[1]);
  1580. mmci_dma_setup(host);
  1581. pm_runtime_set_autosuspend_delay(&dev->dev, 50);
  1582. pm_runtime_use_autosuspend(&dev->dev);
  1583. mmc_add_host(mmc);
  1584. pm_runtime_put(&dev->dev);
  1585. return 0;
  1586. clk_disable:
  1587. clk_disable_unprepare(host->clk);
  1588. host_free:
  1589. mmc_free_host(mmc);
  1590. return ret;
  1591. }
  1592. static int mmci_remove(struct amba_device *dev)
  1593. {
  1594. struct mmc_host *mmc = amba_get_drvdata(dev);
  1595. if (mmc) {
  1596. struct mmci_host *host = mmc_priv(mmc);
  1597. struct variant_data *variant = host->variant;
  1598. /*
  1599. * Undo pm_runtime_put() in probe. We use the _sync
  1600. * version here so that we can access the primecell.
  1601. */
  1602. pm_runtime_get_sync(&dev->dev);
  1603. mmc_remove_host(mmc);
  1604. writel(0, host->base + MMCIMASK0);
  1605. if (variant->mmcimask1)
  1606. writel(0, host->base + MMCIMASK1);
  1607. writel(0, host->base + MMCICOMMAND);
  1608. writel(0, host->base + MMCIDATACTRL);
  1609. mmci_dma_release(host);
  1610. clk_disable_unprepare(host->clk);
  1611. mmc_free_host(mmc);
  1612. }
  1613. return 0;
  1614. }
  1615. #ifdef CONFIG_PM
  1616. static void mmci_save(struct mmci_host *host)
  1617. {
  1618. unsigned long flags;
  1619. spin_lock_irqsave(&host->lock, flags);
  1620. writel(0, host->base + MMCIMASK0);
  1621. if (host->variant->pwrreg_nopower) {
  1622. writel(0, host->base + MMCIDATACTRL);
  1623. writel(0, host->base + MMCIPOWER);
  1624. writel(0, host->base + MMCICLOCK);
  1625. }
  1626. mmci_reg_delay(host);
  1627. spin_unlock_irqrestore(&host->lock, flags);
  1628. }
  1629. static void mmci_restore(struct mmci_host *host)
  1630. {
  1631. unsigned long flags;
  1632. spin_lock_irqsave(&host->lock, flags);
  1633. if (host->variant->pwrreg_nopower) {
  1634. writel(host->clk_reg, host->base + MMCICLOCK);
  1635. writel(host->datactrl_reg, host->base + MMCIDATACTRL);
  1636. writel(host->pwr_reg, host->base + MMCIPOWER);
  1637. }
  1638. writel(MCI_IRQENABLE, host->base + MMCIMASK0);
  1639. mmci_reg_delay(host);
  1640. spin_unlock_irqrestore(&host->lock, flags);
  1641. }
  1642. static int mmci_runtime_suspend(struct device *dev)
  1643. {
  1644. struct amba_device *adev = to_amba_device(dev);
  1645. struct mmc_host *mmc = amba_get_drvdata(adev);
  1646. if (mmc) {
  1647. struct mmci_host *host = mmc_priv(mmc);
  1648. pinctrl_pm_select_sleep_state(dev);
  1649. mmci_save(host);
  1650. clk_disable_unprepare(host->clk);
  1651. }
  1652. return 0;
  1653. }
  1654. static int mmci_runtime_resume(struct device *dev)
  1655. {
  1656. struct amba_device *adev = to_amba_device(dev);
  1657. struct mmc_host *mmc = amba_get_drvdata(adev);
  1658. if (mmc) {
  1659. struct mmci_host *host = mmc_priv(mmc);
  1660. clk_prepare_enable(host->clk);
  1661. mmci_restore(host);
  1662. pinctrl_pm_select_default_state(dev);
  1663. }
  1664. return 0;
  1665. }
  1666. #endif
  1667. static const struct dev_pm_ops mmci_dev_pm_ops = {
  1668. SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
  1669. pm_runtime_force_resume)
  1670. SET_RUNTIME_PM_OPS(mmci_runtime_suspend, mmci_runtime_resume, NULL)
  1671. };
  1672. static const struct amba_id mmci_ids[] = {
  1673. {
  1674. .id = 0x00041180,
  1675. .mask = 0xff0fffff,
  1676. .data = &variant_arm,
  1677. },
  1678. {
  1679. .id = 0x01041180,
  1680. .mask = 0xff0fffff,
  1681. .data = &variant_arm_extended_fifo,
  1682. },
  1683. {
  1684. .id = 0x02041180,
  1685. .mask = 0xff0fffff,
  1686. .data = &variant_arm_extended_fifo_hwfc,
  1687. },
  1688. {
  1689. .id = 0x00041181,
  1690. .mask = 0x000fffff,
  1691. .data = &variant_arm,
  1692. },
  1693. /* ST Micro variants */
  1694. {
  1695. .id = 0x00180180,
  1696. .mask = 0x00ffffff,
  1697. .data = &variant_u300,
  1698. },
  1699. {
  1700. .id = 0x10180180,
  1701. .mask = 0xf0ffffff,
  1702. .data = &variant_nomadik,
  1703. },
  1704. {
  1705. .id = 0x00280180,
  1706. .mask = 0x00ffffff,
  1707. .data = &variant_nomadik,
  1708. },
  1709. {
  1710. .id = 0x00480180,
  1711. .mask = 0xf0ffffff,
  1712. .data = &variant_ux500,
  1713. },
  1714. {
  1715. .id = 0x10480180,
  1716. .mask = 0xf0ffffff,
  1717. .data = &variant_ux500v2,
  1718. },
  1719. {
  1720. .id = 0x00880180,
  1721. .mask = 0x00ffffff,
  1722. .data = &variant_stm32,
  1723. },
  1724. /* Qualcomm variants */
  1725. {
  1726. .id = 0x00051180,
  1727. .mask = 0x000fffff,
  1728. .data = &variant_qcom,
  1729. },
  1730. { 0, 0 },
  1731. };
  1732. MODULE_DEVICE_TABLE(amba, mmci_ids);
  1733. static struct amba_driver mmci_driver = {
  1734. .drv = {
  1735. .name = DRIVER_NAME,
  1736. .pm = &mmci_dev_pm_ops,
  1737. },
  1738. .probe = mmci_probe,
  1739. .remove = mmci_remove,
  1740. .id_table = mmci_ids,
  1741. };
  1742. module_amba_driver(mmci_driver);
  1743. module_param(fmax, uint, 0444);
  1744. MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
  1745. MODULE_LICENSE("GPL");