meson-gx-mmc.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340
  1. /*
  2. * Amlogic SD/eMMC driver for the GX/S905 family SoCs
  3. *
  4. * Copyright (c) 2016 BayLibre, SAS.
  5. * Author: Kevin Hilman <khilman@baylibre.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of version 2 of the GNU General Public License as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  18. * The full GNU General Public License is included in this distribution
  19. * in the file called COPYING.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/init.h>
  24. #include <linux/device.h>
  25. #include <linux/of_device.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/ioport.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/dma-mapping.h>
  30. #include <linux/mmc/host.h>
  31. #include <linux/mmc/mmc.h>
  32. #include <linux/mmc/sdio.h>
  33. #include <linux/mmc/slot-gpio.h>
  34. #include <linux/io.h>
  35. #include <linux/clk.h>
  36. #include <linux/clk-provider.h>
  37. #include <linux/regulator/consumer.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/bitfield.h>
  40. #include <linux/pinctrl/consumer.h>
  41. #define DRIVER_NAME "meson-gx-mmc"
  42. #define SD_EMMC_CLOCK 0x0
  43. #define CLK_DIV_MASK GENMASK(5, 0)
  44. #define CLK_SRC_MASK GENMASK(7, 6)
  45. #define CLK_CORE_PHASE_MASK GENMASK(9, 8)
  46. #define CLK_TX_PHASE_MASK GENMASK(11, 10)
  47. #define CLK_RX_PHASE_MASK GENMASK(13, 12)
  48. #define CLK_TX_DELAY_MASK GENMASK(19, 16)
  49. #define CLK_RX_DELAY_MASK GENMASK(23, 20)
  50. #define CLK_DELAY_STEP_PS 200
  51. #define CLK_PHASE_STEP 30
  52. #define CLK_PHASE_POINT_NUM (360 / CLK_PHASE_STEP)
  53. #define CLK_ALWAYS_ON BIT(24)
  54. #define SD_EMMC_DELAY 0x4
  55. #define SD_EMMC_ADJUST 0x8
  56. #define SD_EMMC_CALOUT 0x10
  57. #define SD_EMMC_START 0x40
  58. #define START_DESC_INIT BIT(0)
  59. #define START_DESC_BUSY BIT(1)
  60. #define START_DESC_ADDR_MASK GENMASK(31, 2)
  61. #define SD_EMMC_CFG 0x44
  62. #define CFG_BUS_WIDTH_MASK GENMASK(1, 0)
  63. #define CFG_BUS_WIDTH_1 0x0
  64. #define CFG_BUS_WIDTH_4 0x1
  65. #define CFG_BUS_WIDTH_8 0x2
  66. #define CFG_DDR BIT(2)
  67. #define CFG_BLK_LEN_MASK GENMASK(7, 4)
  68. #define CFG_RESP_TIMEOUT_MASK GENMASK(11, 8)
  69. #define CFG_RC_CC_MASK GENMASK(15, 12)
  70. #define CFG_STOP_CLOCK BIT(22)
  71. #define CFG_CLK_ALWAYS_ON BIT(18)
  72. #define CFG_CHK_DS BIT(20)
  73. #define CFG_AUTO_CLK BIT(23)
  74. #define SD_EMMC_STATUS 0x48
  75. #define STATUS_BUSY BIT(31)
  76. #define STATUS_DATI GENMASK(23, 16)
  77. #define SD_EMMC_IRQ_EN 0x4c
  78. #define IRQ_RXD_ERR_MASK GENMASK(7, 0)
  79. #define IRQ_TXD_ERR BIT(8)
  80. #define IRQ_DESC_ERR BIT(9)
  81. #define IRQ_RESP_ERR BIT(10)
  82. #define IRQ_CRC_ERR \
  83. (IRQ_RXD_ERR_MASK | IRQ_TXD_ERR | IRQ_DESC_ERR | IRQ_RESP_ERR)
  84. #define IRQ_RESP_TIMEOUT BIT(11)
  85. #define IRQ_DESC_TIMEOUT BIT(12)
  86. #define IRQ_TIMEOUTS \
  87. (IRQ_RESP_TIMEOUT | IRQ_DESC_TIMEOUT)
  88. #define IRQ_END_OF_CHAIN BIT(13)
  89. #define IRQ_RESP_STATUS BIT(14)
  90. #define IRQ_SDIO BIT(15)
  91. #define IRQ_EN_MASK \
  92. (IRQ_CRC_ERR | IRQ_TIMEOUTS | IRQ_END_OF_CHAIN | IRQ_RESP_STATUS |\
  93. IRQ_SDIO)
  94. #define SD_EMMC_CMD_CFG 0x50
  95. #define SD_EMMC_CMD_ARG 0x54
  96. #define SD_EMMC_CMD_DAT 0x58
  97. #define SD_EMMC_CMD_RSP 0x5c
  98. #define SD_EMMC_CMD_RSP1 0x60
  99. #define SD_EMMC_CMD_RSP2 0x64
  100. #define SD_EMMC_CMD_RSP3 0x68
  101. #define SD_EMMC_RXD 0x94
  102. #define SD_EMMC_TXD 0x94
  103. #define SD_EMMC_LAST_REG SD_EMMC_TXD
  104. #define SD_EMMC_CFG_BLK_SIZE 512 /* internal buffer max: 512 bytes */
  105. #define SD_EMMC_CFG_RESP_TIMEOUT 256 /* in clock cycles */
  106. #define SD_EMMC_CMD_TIMEOUT 1024 /* in ms */
  107. #define SD_EMMC_CMD_TIMEOUT_DATA 4096 /* in ms */
  108. #define SD_EMMC_CFG_CMD_GAP 16 /* in clock cycles */
  109. #define SD_EMMC_DESC_BUF_LEN PAGE_SIZE
  110. #define SD_EMMC_PRE_REQ_DONE BIT(0)
  111. #define SD_EMMC_DESC_CHAIN_MODE BIT(1)
  112. #define MUX_CLK_NUM_PARENTS 2
  113. struct sd_emmc_desc {
  114. u32 cmd_cfg;
  115. u32 cmd_arg;
  116. u32 cmd_data;
  117. u32 cmd_resp;
  118. };
  119. struct meson_host {
  120. struct device *dev;
  121. struct mmc_host *mmc;
  122. struct mmc_command *cmd;
  123. spinlock_t lock;
  124. void __iomem *regs;
  125. struct clk *core_clk;
  126. struct clk *mmc_clk;
  127. struct clk *rx_clk;
  128. struct clk *tx_clk;
  129. unsigned long req_rate;
  130. struct pinctrl *pinctrl;
  131. struct pinctrl_state *pins_default;
  132. struct pinctrl_state *pins_clk_gate;
  133. unsigned int bounce_buf_size;
  134. void *bounce_buf;
  135. dma_addr_t bounce_dma_addr;
  136. struct sd_emmc_desc *descs;
  137. dma_addr_t descs_dma_addr;
  138. bool vqmmc_enabled;
  139. };
  140. #define CMD_CFG_LENGTH_MASK GENMASK(8, 0)
  141. #define CMD_CFG_BLOCK_MODE BIT(9)
  142. #define CMD_CFG_R1B BIT(10)
  143. #define CMD_CFG_END_OF_CHAIN BIT(11)
  144. #define CMD_CFG_TIMEOUT_MASK GENMASK(15, 12)
  145. #define CMD_CFG_NO_RESP BIT(16)
  146. #define CMD_CFG_NO_CMD BIT(17)
  147. #define CMD_CFG_DATA_IO BIT(18)
  148. #define CMD_CFG_DATA_WR BIT(19)
  149. #define CMD_CFG_RESP_NOCRC BIT(20)
  150. #define CMD_CFG_RESP_128 BIT(21)
  151. #define CMD_CFG_RESP_NUM BIT(22)
  152. #define CMD_CFG_DATA_NUM BIT(23)
  153. #define CMD_CFG_CMD_INDEX_MASK GENMASK(29, 24)
  154. #define CMD_CFG_ERROR BIT(30)
  155. #define CMD_CFG_OWNER BIT(31)
  156. #define CMD_DATA_MASK GENMASK(31, 2)
  157. #define CMD_DATA_BIG_ENDIAN BIT(1)
  158. #define CMD_DATA_SRAM BIT(0)
  159. #define CMD_RESP_MASK GENMASK(31, 1)
  160. #define CMD_RESP_SRAM BIT(0)
  161. struct meson_mmc_phase {
  162. struct clk_hw hw;
  163. void __iomem *reg;
  164. unsigned long phase_mask;
  165. unsigned long delay_mask;
  166. unsigned int delay_step_ps;
  167. };
  168. #define to_meson_mmc_phase(_hw) container_of(_hw, struct meson_mmc_phase, hw)
  169. static int meson_mmc_clk_get_phase(struct clk_hw *hw)
  170. {
  171. struct meson_mmc_phase *mmc = to_meson_mmc_phase(hw);
  172. unsigned int phase_num = 1 << hweight_long(mmc->phase_mask);
  173. unsigned long period_ps, p, d;
  174. int degrees;
  175. u32 val;
  176. val = readl(mmc->reg);
  177. p = (val & mmc->phase_mask) >> __ffs(mmc->phase_mask);
  178. degrees = p * 360 / phase_num;
  179. if (mmc->delay_mask) {
  180. period_ps = DIV_ROUND_UP((unsigned long)NSEC_PER_SEC * 1000,
  181. clk_get_rate(hw->clk));
  182. d = (val & mmc->delay_mask) >> __ffs(mmc->delay_mask);
  183. degrees += d * mmc->delay_step_ps * 360 / period_ps;
  184. degrees %= 360;
  185. }
  186. return degrees;
  187. }
  188. static void meson_mmc_apply_phase_delay(struct meson_mmc_phase *mmc,
  189. unsigned int phase,
  190. unsigned int delay)
  191. {
  192. u32 val;
  193. val = readl(mmc->reg);
  194. val &= ~mmc->phase_mask;
  195. val |= phase << __ffs(mmc->phase_mask);
  196. if (mmc->delay_mask) {
  197. val &= ~mmc->delay_mask;
  198. val |= delay << __ffs(mmc->delay_mask);
  199. }
  200. writel(val, mmc->reg);
  201. }
  202. static int meson_mmc_clk_set_phase(struct clk_hw *hw, int degrees)
  203. {
  204. struct meson_mmc_phase *mmc = to_meson_mmc_phase(hw);
  205. unsigned int phase_num = 1 << hweight_long(mmc->phase_mask);
  206. unsigned long period_ps, d = 0, r;
  207. uint64_t p;
  208. p = degrees % 360;
  209. if (!mmc->delay_mask) {
  210. p = DIV_ROUND_CLOSEST_ULL(p, 360 / phase_num);
  211. } else {
  212. period_ps = DIV_ROUND_UP((unsigned long)NSEC_PER_SEC * 1000,
  213. clk_get_rate(hw->clk));
  214. /* First compute the phase index (p), the remainder (r) is the
  215. * part we'll try to acheive using the delays (d).
  216. */
  217. r = do_div(p, 360 / phase_num);
  218. d = DIV_ROUND_CLOSEST(r * period_ps,
  219. 360 * mmc->delay_step_ps);
  220. d = min(d, mmc->delay_mask >> __ffs(mmc->delay_mask));
  221. }
  222. meson_mmc_apply_phase_delay(mmc, p, d);
  223. return 0;
  224. }
  225. static const struct clk_ops meson_mmc_clk_phase_ops = {
  226. .get_phase = meson_mmc_clk_get_phase,
  227. .set_phase = meson_mmc_clk_set_phase,
  228. };
  229. static unsigned int meson_mmc_get_timeout_msecs(struct mmc_data *data)
  230. {
  231. unsigned int timeout = data->timeout_ns / NSEC_PER_MSEC;
  232. if (!timeout)
  233. return SD_EMMC_CMD_TIMEOUT_DATA;
  234. timeout = roundup_pow_of_two(timeout);
  235. return min(timeout, 32768U); /* max. 2^15 ms */
  236. }
  237. static struct mmc_command *meson_mmc_get_next_command(struct mmc_command *cmd)
  238. {
  239. if (cmd->opcode == MMC_SET_BLOCK_COUNT && !cmd->error)
  240. return cmd->mrq->cmd;
  241. else if (mmc_op_multi(cmd->opcode) &&
  242. (!cmd->mrq->sbc || cmd->error || cmd->data->error))
  243. return cmd->mrq->stop;
  244. else
  245. return NULL;
  246. }
  247. static void meson_mmc_get_transfer_mode(struct mmc_host *mmc,
  248. struct mmc_request *mrq)
  249. {
  250. struct mmc_data *data = mrq->data;
  251. struct scatterlist *sg;
  252. int i;
  253. bool use_desc_chain_mode = true;
  254. /*
  255. * Broken SDIO with AP6255-based WiFi on Khadas VIM Pro has been
  256. * reported. For some strange reason this occurs in descriptor
  257. * chain mode only. So let's fall back to bounce buffer mode
  258. * for command SD_IO_RW_EXTENDED.
  259. */
  260. if (mrq->cmd->opcode == SD_IO_RW_EXTENDED)
  261. return;
  262. for_each_sg(data->sg, sg, data->sg_len, i)
  263. /* check for 8 byte alignment */
  264. if (sg->offset & 7) {
  265. WARN_ONCE(1, "unaligned scatterlist buffer\n");
  266. use_desc_chain_mode = false;
  267. break;
  268. }
  269. if (use_desc_chain_mode)
  270. data->host_cookie |= SD_EMMC_DESC_CHAIN_MODE;
  271. }
  272. static inline bool meson_mmc_desc_chain_mode(const struct mmc_data *data)
  273. {
  274. return data->host_cookie & SD_EMMC_DESC_CHAIN_MODE;
  275. }
  276. static inline bool meson_mmc_bounce_buf_read(const struct mmc_data *data)
  277. {
  278. return data && data->flags & MMC_DATA_READ &&
  279. !meson_mmc_desc_chain_mode(data);
  280. }
  281. static void meson_mmc_pre_req(struct mmc_host *mmc, struct mmc_request *mrq)
  282. {
  283. struct mmc_data *data = mrq->data;
  284. if (!data)
  285. return;
  286. meson_mmc_get_transfer_mode(mmc, mrq);
  287. data->host_cookie |= SD_EMMC_PRE_REQ_DONE;
  288. if (!meson_mmc_desc_chain_mode(data))
  289. return;
  290. data->sg_count = dma_map_sg(mmc_dev(mmc), data->sg, data->sg_len,
  291. mmc_get_dma_dir(data));
  292. if (!data->sg_count)
  293. dev_err(mmc_dev(mmc), "dma_map_sg failed");
  294. }
  295. static void meson_mmc_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
  296. int err)
  297. {
  298. struct mmc_data *data = mrq->data;
  299. if (data && meson_mmc_desc_chain_mode(data) && data->sg_count)
  300. dma_unmap_sg(mmc_dev(mmc), data->sg, data->sg_len,
  301. mmc_get_dma_dir(data));
  302. }
  303. static bool meson_mmc_timing_is_ddr(struct mmc_ios *ios)
  304. {
  305. if (ios->timing == MMC_TIMING_MMC_DDR52 ||
  306. ios->timing == MMC_TIMING_UHS_DDR50 ||
  307. ios->timing == MMC_TIMING_MMC_HS400)
  308. return true;
  309. return false;
  310. }
  311. /*
  312. * Gating the clock on this controller is tricky. It seems the mmc clock
  313. * is also used by the controller. It may crash during some operation if the
  314. * clock is stopped. The safest thing to do, whenever possible, is to keep
  315. * clock running at stop it at the pad using the pinmux.
  316. */
  317. static void meson_mmc_clk_gate(struct meson_host *host)
  318. {
  319. u32 cfg;
  320. if (host->pins_clk_gate) {
  321. pinctrl_select_state(host->pinctrl, host->pins_clk_gate);
  322. } else {
  323. /*
  324. * If the pinmux is not provided - default to the classic and
  325. * unsafe method
  326. */
  327. cfg = readl(host->regs + SD_EMMC_CFG);
  328. cfg |= CFG_STOP_CLOCK;
  329. writel(cfg, host->regs + SD_EMMC_CFG);
  330. }
  331. }
  332. static void meson_mmc_clk_ungate(struct meson_host *host)
  333. {
  334. u32 cfg;
  335. if (host->pins_clk_gate)
  336. pinctrl_select_state(host->pinctrl, host->pins_default);
  337. /* Make sure the clock is not stopped in the controller */
  338. cfg = readl(host->regs + SD_EMMC_CFG);
  339. cfg &= ~CFG_STOP_CLOCK;
  340. writel(cfg, host->regs + SD_EMMC_CFG);
  341. }
  342. static int meson_mmc_clk_set(struct meson_host *host, struct mmc_ios *ios)
  343. {
  344. struct mmc_host *mmc = host->mmc;
  345. unsigned long rate = ios->clock;
  346. int ret;
  347. u32 cfg;
  348. /* DDR modes require higher module clock */
  349. if (meson_mmc_timing_is_ddr(ios))
  350. rate <<= 1;
  351. /* Same request - bail-out */
  352. if (host->req_rate == rate)
  353. return 0;
  354. /* stop clock */
  355. meson_mmc_clk_gate(host);
  356. host->req_rate = 0;
  357. if (!rate) {
  358. mmc->actual_clock = 0;
  359. /* return with clock being stopped */
  360. return 0;
  361. }
  362. /* Stop the clock during rate change to avoid glitches */
  363. cfg = readl(host->regs + SD_EMMC_CFG);
  364. cfg |= CFG_STOP_CLOCK;
  365. writel(cfg, host->regs + SD_EMMC_CFG);
  366. ret = clk_set_rate(host->mmc_clk, rate);
  367. if (ret) {
  368. dev_err(host->dev, "Unable to set cfg_div_clk to %lu. ret=%d\n",
  369. rate, ret);
  370. return ret;
  371. }
  372. host->req_rate = rate;
  373. mmc->actual_clock = clk_get_rate(host->mmc_clk);
  374. /* We should report the real output frequency of the controller */
  375. if (meson_mmc_timing_is_ddr(ios))
  376. mmc->actual_clock >>= 1;
  377. dev_dbg(host->dev, "clk rate: %u Hz\n", mmc->actual_clock);
  378. if (ios->clock != mmc->actual_clock)
  379. dev_dbg(host->dev, "requested rate was %u\n", ios->clock);
  380. /* (re)start clock */
  381. meson_mmc_clk_ungate(host);
  382. return 0;
  383. }
  384. /*
  385. * The SD/eMMC IP block has an internal mux and divider used for
  386. * generating the MMC clock. Use the clock framework to create and
  387. * manage these clocks.
  388. */
  389. static int meson_mmc_clk_init(struct meson_host *host)
  390. {
  391. struct clk_init_data init;
  392. struct clk_mux *mux;
  393. struct clk_divider *div;
  394. struct meson_mmc_phase *core, *tx, *rx;
  395. struct clk *clk;
  396. char clk_name[32];
  397. int i, ret = 0;
  398. const char *mux_parent_names[MUX_CLK_NUM_PARENTS];
  399. const char *clk_parent[1];
  400. u32 clk_reg;
  401. /* init SD_EMMC_CLOCK to sane defaults w/min clock rate */
  402. clk_reg = 0;
  403. clk_reg |= CLK_ALWAYS_ON;
  404. clk_reg |= CLK_DIV_MASK;
  405. writel(clk_reg, host->regs + SD_EMMC_CLOCK);
  406. /* get the mux parents */
  407. for (i = 0; i < MUX_CLK_NUM_PARENTS; i++) {
  408. struct clk *clk;
  409. char name[16];
  410. snprintf(name, sizeof(name), "clkin%d", i);
  411. clk = devm_clk_get(host->dev, name);
  412. if (IS_ERR(clk)) {
  413. if (clk != ERR_PTR(-EPROBE_DEFER))
  414. dev_err(host->dev, "Missing clock %s\n", name);
  415. return PTR_ERR(clk);
  416. }
  417. mux_parent_names[i] = __clk_get_name(clk);
  418. }
  419. /* create the mux */
  420. mux = devm_kzalloc(host->dev, sizeof(*mux), GFP_KERNEL);
  421. if (!mux)
  422. return -ENOMEM;
  423. snprintf(clk_name, sizeof(clk_name), "%s#mux", dev_name(host->dev));
  424. init.name = clk_name;
  425. init.ops = &clk_mux_ops;
  426. init.flags = 0;
  427. init.parent_names = mux_parent_names;
  428. init.num_parents = MUX_CLK_NUM_PARENTS;
  429. mux->reg = host->regs + SD_EMMC_CLOCK;
  430. mux->shift = __ffs(CLK_SRC_MASK);
  431. mux->mask = CLK_SRC_MASK >> mux->shift;
  432. mux->hw.init = &init;
  433. clk = devm_clk_register(host->dev, &mux->hw);
  434. if (WARN_ON(IS_ERR(clk)))
  435. return PTR_ERR(clk);
  436. /* create the divider */
  437. div = devm_kzalloc(host->dev, sizeof(*div), GFP_KERNEL);
  438. if (!div)
  439. return -ENOMEM;
  440. snprintf(clk_name, sizeof(clk_name), "%s#div", dev_name(host->dev));
  441. init.name = clk_name;
  442. init.ops = &clk_divider_ops;
  443. init.flags = CLK_SET_RATE_PARENT;
  444. clk_parent[0] = __clk_get_name(clk);
  445. init.parent_names = clk_parent;
  446. init.num_parents = 1;
  447. div->reg = host->regs + SD_EMMC_CLOCK;
  448. div->shift = __ffs(CLK_DIV_MASK);
  449. div->width = __builtin_popcountl(CLK_DIV_MASK);
  450. div->hw.init = &init;
  451. div->flags = CLK_DIVIDER_ONE_BASED;
  452. clk = devm_clk_register(host->dev, &div->hw);
  453. if (WARN_ON(IS_ERR(clk)))
  454. return PTR_ERR(clk);
  455. /* create the mmc core clock */
  456. core = devm_kzalloc(host->dev, sizeof(*core), GFP_KERNEL);
  457. if (!core)
  458. return -ENOMEM;
  459. snprintf(clk_name, sizeof(clk_name), "%s#core", dev_name(host->dev));
  460. init.name = clk_name;
  461. init.ops = &meson_mmc_clk_phase_ops;
  462. init.flags = CLK_SET_RATE_PARENT;
  463. clk_parent[0] = __clk_get_name(clk);
  464. init.parent_names = clk_parent;
  465. init.num_parents = 1;
  466. core->reg = host->regs + SD_EMMC_CLOCK;
  467. core->phase_mask = CLK_CORE_PHASE_MASK;
  468. core->hw.init = &init;
  469. host->mmc_clk = devm_clk_register(host->dev, &core->hw);
  470. if (WARN_ON(PTR_ERR_OR_ZERO(host->mmc_clk)))
  471. return PTR_ERR(host->mmc_clk);
  472. /* create the mmc tx clock */
  473. tx = devm_kzalloc(host->dev, sizeof(*tx), GFP_KERNEL);
  474. if (!tx)
  475. return -ENOMEM;
  476. snprintf(clk_name, sizeof(clk_name), "%s#tx", dev_name(host->dev));
  477. init.name = clk_name;
  478. init.ops = &meson_mmc_clk_phase_ops;
  479. init.flags = 0;
  480. clk_parent[0] = __clk_get_name(host->mmc_clk);
  481. init.parent_names = clk_parent;
  482. init.num_parents = 1;
  483. tx->reg = host->regs + SD_EMMC_CLOCK;
  484. tx->phase_mask = CLK_TX_PHASE_MASK;
  485. tx->delay_mask = CLK_TX_DELAY_MASK;
  486. tx->delay_step_ps = CLK_DELAY_STEP_PS;
  487. tx->hw.init = &init;
  488. host->tx_clk = devm_clk_register(host->dev, &tx->hw);
  489. if (WARN_ON(PTR_ERR_OR_ZERO(host->tx_clk)))
  490. return PTR_ERR(host->tx_clk);
  491. /* create the mmc rx clock */
  492. rx = devm_kzalloc(host->dev, sizeof(*rx), GFP_KERNEL);
  493. if (!rx)
  494. return -ENOMEM;
  495. snprintf(clk_name, sizeof(clk_name), "%s#rx", dev_name(host->dev));
  496. init.name = clk_name;
  497. init.ops = &meson_mmc_clk_phase_ops;
  498. init.flags = 0;
  499. clk_parent[0] = __clk_get_name(host->mmc_clk);
  500. init.parent_names = clk_parent;
  501. init.num_parents = 1;
  502. rx->reg = host->regs + SD_EMMC_CLOCK;
  503. rx->phase_mask = CLK_RX_PHASE_MASK;
  504. rx->delay_mask = CLK_RX_DELAY_MASK;
  505. rx->delay_step_ps = CLK_DELAY_STEP_PS;
  506. rx->hw.init = &init;
  507. host->rx_clk = devm_clk_register(host->dev, &rx->hw);
  508. if (WARN_ON(PTR_ERR_OR_ZERO(host->rx_clk)))
  509. return PTR_ERR(host->rx_clk);
  510. /* init SD_EMMC_CLOCK to sane defaults w/min clock rate */
  511. host->mmc->f_min = clk_round_rate(host->mmc_clk, 400000);
  512. ret = clk_set_rate(host->mmc_clk, host->mmc->f_min);
  513. if (ret)
  514. return ret;
  515. /*
  516. * Set phases : These values are mostly the datasheet recommended ones
  517. * except for the Tx phase. Datasheet recommends 180 but some cards
  518. * fail at initialisation with it. 270 works just fine, it fixes these
  519. * initialisation issues and enable eMMC DDR52 mode.
  520. */
  521. clk_set_phase(host->mmc_clk, 180);
  522. clk_set_phase(host->tx_clk, 270);
  523. clk_set_phase(host->rx_clk, 0);
  524. return clk_prepare_enable(host->mmc_clk);
  525. }
  526. static void meson_mmc_shift_map(unsigned long *map, unsigned long shift)
  527. {
  528. DECLARE_BITMAP(left, CLK_PHASE_POINT_NUM);
  529. DECLARE_BITMAP(right, CLK_PHASE_POINT_NUM);
  530. /*
  531. * shift the bitmap right and reintroduce the dropped bits on the left
  532. * of the bitmap
  533. */
  534. bitmap_shift_right(right, map, shift, CLK_PHASE_POINT_NUM);
  535. bitmap_shift_left(left, map, CLK_PHASE_POINT_NUM - shift,
  536. CLK_PHASE_POINT_NUM);
  537. bitmap_or(map, left, right, CLK_PHASE_POINT_NUM);
  538. }
  539. static void meson_mmc_find_next_region(unsigned long *map,
  540. unsigned long *start,
  541. unsigned long *stop)
  542. {
  543. *start = find_next_bit(map, CLK_PHASE_POINT_NUM, *start);
  544. *stop = find_next_zero_bit(map, CLK_PHASE_POINT_NUM, *start);
  545. }
  546. static int meson_mmc_find_tuning_point(unsigned long *test)
  547. {
  548. unsigned long shift, stop, offset = 0, start = 0, size = 0;
  549. /* Get the all good/all bad situation out the way */
  550. if (bitmap_full(test, CLK_PHASE_POINT_NUM))
  551. return 0; /* All points are good so point 0 will do */
  552. else if (bitmap_empty(test, CLK_PHASE_POINT_NUM))
  553. return -EIO; /* No successful tuning point */
  554. /*
  555. * Now we know there is a least one region find. Make sure it does
  556. * not wrap by the shifting the bitmap if necessary
  557. */
  558. shift = find_first_zero_bit(test, CLK_PHASE_POINT_NUM);
  559. if (shift != 0)
  560. meson_mmc_shift_map(test, shift);
  561. while (start < CLK_PHASE_POINT_NUM) {
  562. meson_mmc_find_next_region(test, &start, &stop);
  563. if ((stop - start) > size) {
  564. offset = start;
  565. size = stop - start;
  566. }
  567. start = stop;
  568. }
  569. /* Get the center point of the region */
  570. offset += (size / 2);
  571. /* Shift the result back */
  572. offset = (offset + shift) % CLK_PHASE_POINT_NUM;
  573. return offset;
  574. }
  575. static int meson_mmc_clk_phase_tuning(struct mmc_host *mmc, u32 opcode,
  576. struct clk *clk)
  577. {
  578. int point, ret;
  579. DECLARE_BITMAP(test, CLK_PHASE_POINT_NUM);
  580. dev_dbg(mmc_dev(mmc), "%s phase/delay tunning...\n",
  581. __clk_get_name(clk));
  582. bitmap_zero(test, CLK_PHASE_POINT_NUM);
  583. /* Explore tuning points */
  584. for (point = 0; point < CLK_PHASE_POINT_NUM; point++) {
  585. clk_set_phase(clk, point * CLK_PHASE_STEP);
  586. ret = mmc_send_tuning(mmc, opcode, NULL);
  587. if (!ret)
  588. set_bit(point, test);
  589. }
  590. /* Find the optimal tuning point and apply it */
  591. point = meson_mmc_find_tuning_point(test);
  592. if (point < 0)
  593. return point; /* tuning failed */
  594. clk_set_phase(clk, point * CLK_PHASE_STEP);
  595. dev_dbg(mmc_dev(mmc), "success with phase: %d\n",
  596. clk_get_phase(clk));
  597. return 0;
  598. }
  599. static int meson_mmc_execute_tuning(struct mmc_host *mmc, u32 opcode)
  600. {
  601. struct meson_host *host = mmc_priv(mmc);
  602. return meson_mmc_clk_phase_tuning(mmc, opcode, host->rx_clk);
  603. }
  604. static void meson_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  605. {
  606. struct meson_host *host = mmc_priv(mmc);
  607. u32 bus_width, val;
  608. int err;
  609. /*
  610. * GPIO regulator, only controls switching between 1v8 and
  611. * 3v3, doesn't support MMC_POWER_OFF, MMC_POWER_ON.
  612. */
  613. switch (ios->power_mode) {
  614. case MMC_POWER_OFF:
  615. if (!IS_ERR(mmc->supply.vmmc))
  616. mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
  617. if (!IS_ERR(mmc->supply.vqmmc) && host->vqmmc_enabled) {
  618. regulator_disable(mmc->supply.vqmmc);
  619. host->vqmmc_enabled = false;
  620. }
  621. break;
  622. case MMC_POWER_UP:
  623. if (!IS_ERR(mmc->supply.vmmc))
  624. mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, ios->vdd);
  625. /* Reset rx phase */
  626. clk_set_phase(host->rx_clk, 0);
  627. break;
  628. case MMC_POWER_ON:
  629. if (!IS_ERR(mmc->supply.vqmmc) && !host->vqmmc_enabled) {
  630. int ret = regulator_enable(mmc->supply.vqmmc);
  631. if (ret < 0)
  632. dev_err(host->dev,
  633. "failed to enable vqmmc regulator\n");
  634. else
  635. host->vqmmc_enabled = true;
  636. }
  637. break;
  638. }
  639. /* Bus width */
  640. switch (ios->bus_width) {
  641. case MMC_BUS_WIDTH_1:
  642. bus_width = CFG_BUS_WIDTH_1;
  643. break;
  644. case MMC_BUS_WIDTH_4:
  645. bus_width = CFG_BUS_WIDTH_4;
  646. break;
  647. case MMC_BUS_WIDTH_8:
  648. bus_width = CFG_BUS_WIDTH_8;
  649. break;
  650. default:
  651. dev_err(host->dev, "Invalid ios->bus_width: %u. Setting to 4.\n",
  652. ios->bus_width);
  653. bus_width = CFG_BUS_WIDTH_4;
  654. }
  655. val = readl(host->regs + SD_EMMC_CFG);
  656. val &= ~CFG_BUS_WIDTH_MASK;
  657. val |= FIELD_PREP(CFG_BUS_WIDTH_MASK, bus_width);
  658. val &= ~CFG_DDR;
  659. if (meson_mmc_timing_is_ddr(ios))
  660. val |= CFG_DDR;
  661. val &= ~CFG_CHK_DS;
  662. if (ios->timing == MMC_TIMING_MMC_HS400)
  663. val |= CFG_CHK_DS;
  664. err = meson_mmc_clk_set(host, ios);
  665. if (err)
  666. dev_err(host->dev, "Failed to set clock: %d\n,", err);
  667. writel(val, host->regs + SD_EMMC_CFG);
  668. dev_dbg(host->dev, "SD_EMMC_CFG: 0x%08x\n", val);
  669. }
  670. static void meson_mmc_request_done(struct mmc_host *mmc,
  671. struct mmc_request *mrq)
  672. {
  673. struct meson_host *host = mmc_priv(mmc);
  674. host->cmd = NULL;
  675. mmc_request_done(host->mmc, mrq);
  676. }
  677. static void meson_mmc_set_blksz(struct mmc_host *mmc, unsigned int blksz)
  678. {
  679. struct meson_host *host = mmc_priv(mmc);
  680. u32 cfg, blksz_old;
  681. cfg = readl(host->regs + SD_EMMC_CFG);
  682. blksz_old = FIELD_GET(CFG_BLK_LEN_MASK, cfg);
  683. if (!is_power_of_2(blksz))
  684. dev_err(host->dev, "blksz %u is not a power of 2\n", blksz);
  685. blksz = ilog2(blksz);
  686. /* check if block-size matches, if not update */
  687. if (blksz == blksz_old)
  688. return;
  689. dev_dbg(host->dev, "%s: update blk_len %d -> %d\n", __func__,
  690. blksz_old, blksz);
  691. cfg &= ~CFG_BLK_LEN_MASK;
  692. cfg |= FIELD_PREP(CFG_BLK_LEN_MASK, blksz);
  693. writel(cfg, host->regs + SD_EMMC_CFG);
  694. }
  695. static void meson_mmc_set_response_bits(struct mmc_command *cmd, u32 *cmd_cfg)
  696. {
  697. if (cmd->flags & MMC_RSP_PRESENT) {
  698. if (cmd->flags & MMC_RSP_136)
  699. *cmd_cfg |= CMD_CFG_RESP_128;
  700. *cmd_cfg |= CMD_CFG_RESP_NUM;
  701. if (!(cmd->flags & MMC_RSP_CRC))
  702. *cmd_cfg |= CMD_CFG_RESP_NOCRC;
  703. if (cmd->flags & MMC_RSP_BUSY)
  704. *cmd_cfg |= CMD_CFG_R1B;
  705. } else {
  706. *cmd_cfg |= CMD_CFG_NO_RESP;
  707. }
  708. }
  709. static void meson_mmc_desc_chain_transfer(struct mmc_host *mmc, u32 cmd_cfg)
  710. {
  711. struct meson_host *host = mmc_priv(mmc);
  712. struct sd_emmc_desc *desc = host->descs;
  713. struct mmc_data *data = host->cmd->data;
  714. struct scatterlist *sg;
  715. u32 start;
  716. int i;
  717. if (data->flags & MMC_DATA_WRITE)
  718. cmd_cfg |= CMD_CFG_DATA_WR;
  719. if (data->blocks > 1) {
  720. cmd_cfg |= CMD_CFG_BLOCK_MODE;
  721. meson_mmc_set_blksz(mmc, data->blksz);
  722. }
  723. for_each_sg(data->sg, sg, data->sg_count, i) {
  724. unsigned int len = sg_dma_len(sg);
  725. if (data->blocks > 1)
  726. len /= data->blksz;
  727. desc[i].cmd_cfg = cmd_cfg;
  728. desc[i].cmd_cfg |= FIELD_PREP(CMD_CFG_LENGTH_MASK, len);
  729. if (i > 0)
  730. desc[i].cmd_cfg |= CMD_CFG_NO_CMD;
  731. desc[i].cmd_arg = host->cmd->arg;
  732. desc[i].cmd_resp = 0;
  733. desc[i].cmd_data = sg_dma_address(sg);
  734. }
  735. desc[data->sg_count - 1].cmd_cfg |= CMD_CFG_END_OF_CHAIN;
  736. dma_wmb(); /* ensure descriptor is written before kicked */
  737. start = host->descs_dma_addr | START_DESC_BUSY;
  738. writel(start, host->regs + SD_EMMC_START);
  739. }
  740. static void meson_mmc_start_cmd(struct mmc_host *mmc, struct mmc_command *cmd)
  741. {
  742. struct meson_host *host = mmc_priv(mmc);
  743. struct mmc_data *data = cmd->data;
  744. u32 cmd_cfg = 0, cmd_data = 0;
  745. unsigned int xfer_bytes = 0;
  746. /* Setup descriptors */
  747. dma_rmb();
  748. host->cmd = cmd;
  749. cmd_cfg |= FIELD_PREP(CMD_CFG_CMD_INDEX_MASK, cmd->opcode);
  750. cmd_cfg |= CMD_CFG_OWNER; /* owned by CPU */
  751. meson_mmc_set_response_bits(cmd, &cmd_cfg);
  752. /* data? */
  753. if (data) {
  754. data->bytes_xfered = 0;
  755. cmd_cfg |= CMD_CFG_DATA_IO;
  756. cmd_cfg |= FIELD_PREP(CMD_CFG_TIMEOUT_MASK,
  757. ilog2(meson_mmc_get_timeout_msecs(data)));
  758. if (meson_mmc_desc_chain_mode(data)) {
  759. meson_mmc_desc_chain_transfer(mmc, cmd_cfg);
  760. return;
  761. }
  762. if (data->blocks > 1) {
  763. cmd_cfg |= CMD_CFG_BLOCK_MODE;
  764. cmd_cfg |= FIELD_PREP(CMD_CFG_LENGTH_MASK,
  765. data->blocks);
  766. meson_mmc_set_blksz(mmc, data->blksz);
  767. } else {
  768. cmd_cfg |= FIELD_PREP(CMD_CFG_LENGTH_MASK, data->blksz);
  769. }
  770. xfer_bytes = data->blksz * data->blocks;
  771. if (data->flags & MMC_DATA_WRITE) {
  772. cmd_cfg |= CMD_CFG_DATA_WR;
  773. WARN_ON(xfer_bytes > host->bounce_buf_size);
  774. sg_copy_to_buffer(data->sg, data->sg_len,
  775. host->bounce_buf, xfer_bytes);
  776. dma_wmb();
  777. }
  778. cmd_data = host->bounce_dma_addr & CMD_DATA_MASK;
  779. } else {
  780. cmd_cfg |= FIELD_PREP(CMD_CFG_TIMEOUT_MASK,
  781. ilog2(SD_EMMC_CMD_TIMEOUT));
  782. }
  783. /* Last descriptor */
  784. cmd_cfg |= CMD_CFG_END_OF_CHAIN;
  785. writel(cmd_cfg, host->regs + SD_EMMC_CMD_CFG);
  786. writel(cmd_data, host->regs + SD_EMMC_CMD_DAT);
  787. writel(0, host->regs + SD_EMMC_CMD_RSP);
  788. wmb(); /* ensure descriptor is written before kicked */
  789. writel(cmd->arg, host->regs + SD_EMMC_CMD_ARG);
  790. }
  791. static void meson_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
  792. {
  793. struct meson_host *host = mmc_priv(mmc);
  794. bool needs_pre_post_req = mrq->data &&
  795. !(mrq->data->host_cookie & SD_EMMC_PRE_REQ_DONE);
  796. if (needs_pre_post_req) {
  797. meson_mmc_get_transfer_mode(mmc, mrq);
  798. if (!meson_mmc_desc_chain_mode(mrq->data))
  799. needs_pre_post_req = false;
  800. }
  801. if (needs_pre_post_req)
  802. meson_mmc_pre_req(mmc, mrq);
  803. /* Stop execution */
  804. writel(0, host->regs + SD_EMMC_START);
  805. meson_mmc_start_cmd(mmc, mrq->sbc ?: mrq->cmd);
  806. if (needs_pre_post_req)
  807. meson_mmc_post_req(mmc, mrq, 0);
  808. }
  809. static void meson_mmc_read_resp(struct mmc_host *mmc, struct mmc_command *cmd)
  810. {
  811. struct meson_host *host = mmc_priv(mmc);
  812. if (cmd->flags & MMC_RSP_136) {
  813. cmd->resp[0] = readl(host->regs + SD_EMMC_CMD_RSP3);
  814. cmd->resp[1] = readl(host->regs + SD_EMMC_CMD_RSP2);
  815. cmd->resp[2] = readl(host->regs + SD_EMMC_CMD_RSP1);
  816. cmd->resp[3] = readl(host->regs + SD_EMMC_CMD_RSP);
  817. } else if (cmd->flags & MMC_RSP_PRESENT) {
  818. cmd->resp[0] = readl(host->regs + SD_EMMC_CMD_RSP);
  819. }
  820. }
  821. static irqreturn_t meson_mmc_irq(int irq, void *dev_id)
  822. {
  823. struct meson_host *host = dev_id;
  824. struct mmc_command *cmd;
  825. struct mmc_data *data;
  826. u32 irq_en, status, raw_status;
  827. irqreturn_t ret = IRQ_NONE;
  828. if (WARN_ON(!host) || WARN_ON(!host->cmd))
  829. return IRQ_NONE;
  830. spin_lock(&host->lock);
  831. cmd = host->cmd;
  832. data = cmd->data;
  833. irq_en = readl(host->regs + SD_EMMC_IRQ_EN);
  834. raw_status = readl(host->regs + SD_EMMC_STATUS);
  835. status = raw_status & irq_en;
  836. cmd->error = 0;
  837. if (status & IRQ_CRC_ERR) {
  838. dev_dbg(host->dev, "CRC Error - status 0x%08x\n", status);
  839. cmd->error = -EILSEQ;
  840. ret = IRQ_HANDLED;
  841. goto out;
  842. }
  843. if (status & IRQ_TIMEOUTS) {
  844. dev_dbg(host->dev, "Timeout - status 0x%08x\n", status);
  845. cmd->error = -ETIMEDOUT;
  846. ret = IRQ_HANDLED;
  847. goto out;
  848. }
  849. meson_mmc_read_resp(host->mmc, cmd);
  850. if (status & IRQ_SDIO) {
  851. dev_dbg(host->dev, "IRQ: SDIO TODO.\n");
  852. ret = IRQ_HANDLED;
  853. }
  854. if (status & (IRQ_END_OF_CHAIN | IRQ_RESP_STATUS)) {
  855. if (data && !cmd->error)
  856. data->bytes_xfered = data->blksz * data->blocks;
  857. if (meson_mmc_bounce_buf_read(data) ||
  858. meson_mmc_get_next_command(cmd))
  859. ret = IRQ_WAKE_THREAD;
  860. else
  861. ret = IRQ_HANDLED;
  862. }
  863. out:
  864. /* ack all enabled interrupts */
  865. writel(irq_en, host->regs + SD_EMMC_STATUS);
  866. if (ret == IRQ_HANDLED)
  867. meson_mmc_request_done(host->mmc, cmd->mrq);
  868. else if (ret == IRQ_NONE)
  869. dev_warn(host->dev,
  870. "Unexpected IRQ! status=0x%08x, irq_en=0x%08x\n",
  871. raw_status, irq_en);
  872. spin_unlock(&host->lock);
  873. return ret;
  874. }
  875. static irqreturn_t meson_mmc_irq_thread(int irq, void *dev_id)
  876. {
  877. struct meson_host *host = dev_id;
  878. struct mmc_command *next_cmd, *cmd = host->cmd;
  879. struct mmc_data *data;
  880. unsigned int xfer_bytes;
  881. if (WARN_ON(!cmd))
  882. return IRQ_NONE;
  883. data = cmd->data;
  884. if (meson_mmc_bounce_buf_read(data)) {
  885. xfer_bytes = data->blksz * data->blocks;
  886. WARN_ON(xfer_bytes > host->bounce_buf_size);
  887. sg_copy_from_buffer(data->sg, data->sg_len,
  888. host->bounce_buf, xfer_bytes);
  889. }
  890. next_cmd = meson_mmc_get_next_command(cmd);
  891. if (next_cmd)
  892. meson_mmc_start_cmd(host->mmc, next_cmd);
  893. else
  894. meson_mmc_request_done(host->mmc, cmd->mrq);
  895. return IRQ_HANDLED;
  896. }
  897. /*
  898. * NOTE: we only need this until the GPIO/pinctrl driver can handle
  899. * interrupts. For now, the MMC core will use this for polling.
  900. */
  901. static int meson_mmc_get_cd(struct mmc_host *mmc)
  902. {
  903. int status = mmc_gpio_get_cd(mmc);
  904. if (status == -ENOSYS)
  905. return 1; /* assume present */
  906. return status;
  907. }
  908. static void meson_mmc_cfg_init(struct meson_host *host)
  909. {
  910. u32 cfg = 0;
  911. cfg |= FIELD_PREP(CFG_RESP_TIMEOUT_MASK,
  912. ilog2(SD_EMMC_CFG_RESP_TIMEOUT));
  913. cfg |= FIELD_PREP(CFG_RC_CC_MASK, ilog2(SD_EMMC_CFG_CMD_GAP));
  914. cfg |= FIELD_PREP(CFG_BLK_LEN_MASK, ilog2(SD_EMMC_CFG_BLK_SIZE));
  915. writel(cfg, host->regs + SD_EMMC_CFG);
  916. }
  917. static int meson_mmc_card_busy(struct mmc_host *mmc)
  918. {
  919. struct meson_host *host = mmc_priv(mmc);
  920. u32 regval;
  921. regval = readl(host->regs + SD_EMMC_STATUS);
  922. /* We are only interrested in lines 0 to 3, so mask the other ones */
  923. return !(FIELD_GET(STATUS_DATI, regval) & 0xf);
  924. }
  925. static int meson_mmc_voltage_switch(struct mmc_host *mmc, struct mmc_ios *ios)
  926. {
  927. /* vqmmc regulator is available */
  928. if (!IS_ERR(mmc->supply.vqmmc)) {
  929. /*
  930. * The usual amlogic setup uses a GPIO to switch from one
  931. * regulator to the other. While the voltage ramp up is
  932. * pretty fast, care must be taken when switching from 3.3v
  933. * to 1.8v. Please make sure the regulator framework is aware
  934. * of your own regulator constraints
  935. */
  936. return mmc_regulator_set_vqmmc(mmc, ios);
  937. }
  938. /* no vqmmc regulator, assume fixed regulator at 3/3.3V */
  939. if (ios->signal_voltage == MMC_SIGNAL_VOLTAGE_330)
  940. return 0;
  941. return -EINVAL;
  942. }
  943. static const struct mmc_host_ops meson_mmc_ops = {
  944. .request = meson_mmc_request,
  945. .set_ios = meson_mmc_set_ios,
  946. .get_cd = meson_mmc_get_cd,
  947. .pre_req = meson_mmc_pre_req,
  948. .post_req = meson_mmc_post_req,
  949. .execute_tuning = meson_mmc_execute_tuning,
  950. .card_busy = meson_mmc_card_busy,
  951. .start_signal_voltage_switch = meson_mmc_voltage_switch,
  952. };
  953. static int meson_mmc_probe(struct platform_device *pdev)
  954. {
  955. struct resource *res;
  956. struct meson_host *host;
  957. struct mmc_host *mmc;
  958. int ret, irq;
  959. mmc = mmc_alloc_host(sizeof(struct meson_host), &pdev->dev);
  960. if (!mmc)
  961. return -ENOMEM;
  962. host = mmc_priv(mmc);
  963. host->mmc = mmc;
  964. host->dev = &pdev->dev;
  965. dev_set_drvdata(&pdev->dev, host);
  966. spin_lock_init(&host->lock);
  967. /* Get regulators and the supported OCR mask */
  968. host->vqmmc_enabled = false;
  969. ret = mmc_regulator_get_supply(mmc);
  970. if (ret)
  971. goto free_host;
  972. ret = mmc_of_parse(mmc);
  973. if (ret) {
  974. if (ret != -EPROBE_DEFER)
  975. dev_warn(&pdev->dev, "error parsing DT: %d\n", ret);
  976. goto free_host;
  977. }
  978. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  979. host->regs = devm_ioremap_resource(&pdev->dev, res);
  980. if (IS_ERR(host->regs)) {
  981. ret = PTR_ERR(host->regs);
  982. goto free_host;
  983. }
  984. irq = platform_get_irq(pdev, 0);
  985. if (irq <= 0) {
  986. dev_err(&pdev->dev, "failed to get interrupt resource.\n");
  987. ret = -EINVAL;
  988. goto free_host;
  989. }
  990. host->pinctrl = devm_pinctrl_get(&pdev->dev);
  991. if (IS_ERR(host->pinctrl)) {
  992. ret = PTR_ERR(host->pinctrl);
  993. goto free_host;
  994. }
  995. host->pins_default = pinctrl_lookup_state(host->pinctrl,
  996. PINCTRL_STATE_DEFAULT);
  997. if (IS_ERR(host->pins_default)) {
  998. ret = PTR_ERR(host->pins_default);
  999. goto free_host;
  1000. }
  1001. host->pins_clk_gate = pinctrl_lookup_state(host->pinctrl,
  1002. "clk-gate");
  1003. if (IS_ERR(host->pins_clk_gate)) {
  1004. dev_warn(&pdev->dev,
  1005. "can't get clk-gate pinctrl, using clk_stop bit\n");
  1006. host->pins_clk_gate = NULL;
  1007. }
  1008. host->core_clk = devm_clk_get(&pdev->dev, "core");
  1009. if (IS_ERR(host->core_clk)) {
  1010. ret = PTR_ERR(host->core_clk);
  1011. goto free_host;
  1012. }
  1013. ret = clk_prepare_enable(host->core_clk);
  1014. if (ret)
  1015. goto free_host;
  1016. ret = meson_mmc_clk_init(host);
  1017. if (ret)
  1018. goto err_core_clk;
  1019. /* set config to sane default */
  1020. meson_mmc_cfg_init(host);
  1021. /* Stop execution */
  1022. writel(0, host->regs + SD_EMMC_START);
  1023. /* clear, ack and enable interrupts */
  1024. writel(0, host->regs + SD_EMMC_IRQ_EN);
  1025. writel(IRQ_CRC_ERR | IRQ_TIMEOUTS | IRQ_END_OF_CHAIN,
  1026. host->regs + SD_EMMC_STATUS);
  1027. writel(IRQ_CRC_ERR | IRQ_TIMEOUTS | IRQ_END_OF_CHAIN,
  1028. host->regs + SD_EMMC_IRQ_EN);
  1029. ret = devm_request_threaded_irq(&pdev->dev, irq, meson_mmc_irq,
  1030. meson_mmc_irq_thread, IRQF_SHARED,
  1031. NULL, host);
  1032. if (ret)
  1033. goto err_init_clk;
  1034. mmc->caps |= MMC_CAP_CMD23;
  1035. mmc->max_blk_count = CMD_CFG_LENGTH_MASK;
  1036. mmc->max_req_size = mmc->max_blk_count * mmc->max_blk_size;
  1037. mmc->max_segs = SD_EMMC_DESC_BUF_LEN / sizeof(struct sd_emmc_desc);
  1038. mmc->max_seg_size = mmc->max_req_size;
  1039. /* data bounce buffer */
  1040. host->bounce_buf_size = mmc->max_req_size;
  1041. host->bounce_buf =
  1042. dma_alloc_coherent(host->dev, host->bounce_buf_size,
  1043. &host->bounce_dma_addr, GFP_KERNEL);
  1044. if (host->bounce_buf == NULL) {
  1045. dev_err(host->dev, "Unable to map allocate DMA bounce buffer.\n");
  1046. ret = -ENOMEM;
  1047. goto err_init_clk;
  1048. }
  1049. host->descs = dma_alloc_coherent(host->dev, SD_EMMC_DESC_BUF_LEN,
  1050. &host->descs_dma_addr, GFP_KERNEL);
  1051. if (!host->descs) {
  1052. dev_err(host->dev, "Allocating descriptor DMA buffer failed\n");
  1053. ret = -ENOMEM;
  1054. goto err_bounce_buf;
  1055. }
  1056. mmc->ops = &meson_mmc_ops;
  1057. mmc_add_host(mmc);
  1058. return 0;
  1059. err_bounce_buf:
  1060. dma_free_coherent(host->dev, host->bounce_buf_size,
  1061. host->bounce_buf, host->bounce_dma_addr);
  1062. err_init_clk:
  1063. clk_disable_unprepare(host->mmc_clk);
  1064. err_core_clk:
  1065. clk_disable_unprepare(host->core_clk);
  1066. free_host:
  1067. mmc_free_host(mmc);
  1068. return ret;
  1069. }
  1070. static int meson_mmc_remove(struct platform_device *pdev)
  1071. {
  1072. struct meson_host *host = dev_get_drvdata(&pdev->dev);
  1073. mmc_remove_host(host->mmc);
  1074. /* disable interrupts */
  1075. writel(0, host->regs + SD_EMMC_IRQ_EN);
  1076. dma_free_coherent(host->dev, SD_EMMC_DESC_BUF_LEN,
  1077. host->descs, host->descs_dma_addr);
  1078. dma_free_coherent(host->dev, host->bounce_buf_size,
  1079. host->bounce_buf, host->bounce_dma_addr);
  1080. clk_disable_unprepare(host->mmc_clk);
  1081. clk_disable_unprepare(host->core_clk);
  1082. mmc_free_host(host->mmc);
  1083. return 0;
  1084. }
  1085. static const struct of_device_id meson_mmc_of_match[] = {
  1086. { .compatible = "amlogic,meson-gx-mmc", },
  1087. { .compatible = "amlogic,meson-gxbb-mmc", },
  1088. { .compatible = "amlogic,meson-gxl-mmc", },
  1089. { .compatible = "amlogic,meson-gxm-mmc", },
  1090. {}
  1091. };
  1092. MODULE_DEVICE_TABLE(of, meson_mmc_of_match);
  1093. static struct platform_driver meson_mmc_driver = {
  1094. .probe = meson_mmc_probe,
  1095. .remove = meson_mmc_remove,
  1096. .driver = {
  1097. .name = DRIVER_NAME,
  1098. .of_match_table = of_match_ptr(meson_mmc_of_match),
  1099. },
  1100. };
  1101. module_platform_driver(meson_mmc_driver);
  1102. MODULE_DESCRIPTION("Amlogic S905*/GX* SD/eMMC driver");
  1103. MODULE_AUTHOR("Kevin Hilman <khilman@baylibre.com>");
  1104. MODULE_LICENSE("GPL v2");