cavium.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  1. /*
  2. * Shared part of driver for MMC/SDHC controller on Cavium OCTEON and
  3. * ThunderX SOCs.
  4. *
  5. * This file is subject to the terms and conditions of the GNU General Public
  6. * License. See the file "COPYING" in the main directory of this archive
  7. * for more details.
  8. *
  9. * Copyright (C) 2012-2017 Cavium Inc.
  10. * Authors:
  11. * David Daney <david.daney@cavium.com>
  12. * Peter Swain <pswain@cavium.com>
  13. * Steven J. Hill <steven.hill@cavium.com>
  14. * Jan Glauber <jglauber@cavium.com>
  15. */
  16. #include <linux/bitfield.h>
  17. #include <linux/delay.h>
  18. #include <linux/dma-direction.h>
  19. #include <linux/dma-mapping.h>
  20. #include <linux/gpio/consumer.h>
  21. #include <linux/interrupt.h>
  22. #include <linux/mmc/mmc.h>
  23. #include <linux/mmc/slot-gpio.h>
  24. #include <linux/module.h>
  25. #include <linux/regulator/consumer.h>
  26. #include <linux/scatterlist.h>
  27. #include <linux/time.h>
  28. #include "cavium.h"
  29. const char *cvm_mmc_irq_names[] = {
  30. "MMC Buffer",
  31. "MMC Command",
  32. "MMC DMA",
  33. "MMC Command Error",
  34. "MMC DMA Error",
  35. "MMC Switch",
  36. "MMC Switch Error",
  37. "MMC DMA int Fifo",
  38. "MMC DMA int",
  39. };
  40. /*
  41. * The Cavium MMC host hardware assumes that all commands have fixed
  42. * command and response types. These are correct if MMC devices are
  43. * being used. However, non-MMC devices like SD use command and
  44. * response types that are unexpected by the host hardware.
  45. *
  46. * The command and response types can be overridden by supplying an
  47. * XOR value that is applied to the type. We calculate the XOR value
  48. * from the values in this table and the flags passed from the MMC
  49. * core.
  50. */
  51. static struct cvm_mmc_cr_type cvm_mmc_cr_types[] = {
  52. {0, 0}, /* CMD0 */
  53. {0, 3}, /* CMD1 */
  54. {0, 2}, /* CMD2 */
  55. {0, 1}, /* CMD3 */
  56. {0, 0}, /* CMD4 */
  57. {0, 1}, /* CMD5 */
  58. {0, 1}, /* CMD6 */
  59. {0, 1}, /* CMD7 */
  60. {1, 1}, /* CMD8 */
  61. {0, 2}, /* CMD9 */
  62. {0, 2}, /* CMD10 */
  63. {1, 1}, /* CMD11 */
  64. {0, 1}, /* CMD12 */
  65. {0, 1}, /* CMD13 */
  66. {1, 1}, /* CMD14 */
  67. {0, 0}, /* CMD15 */
  68. {0, 1}, /* CMD16 */
  69. {1, 1}, /* CMD17 */
  70. {1, 1}, /* CMD18 */
  71. {3, 1}, /* CMD19 */
  72. {2, 1}, /* CMD20 */
  73. {0, 0}, /* CMD21 */
  74. {0, 0}, /* CMD22 */
  75. {0, 1}, /* CMD23 */
  76. {2, 1}, /* CMD24 */
  77. {2, 1}, /* CMD25 */
  78. {2, 1}, /* CMD26 */
  79. {2, 1}, /* CMD27 */
  80. {0, 1}, /* CMD28 */
  81. {0, 1}, /* CMD29 */
  82. {1, 1}, /* CMD30 */
  83. {1, 1}, /* CMD31 */
  84. {0, 0}, /* CMD32 */
  85. {0, 0}, /* CMD33 */
  86. {0, 0}, /* CMD34 */
  87. {0, 1}, /* CMD35 */
  88. {0, 1}, /* CMD36 */
  89. {0, 0}, /* CMD37 */
  90. {0, 1}, /* CMD38 */
  91. {0, 4}, /* CMD39 */
  92. {0, 5}, /* CMD40 */
  93. {0, 0}, /* CMD41 */
  94. {2, 1}, /* CMD42 */
  95. {0, 0}, /* CMD43 */
  96. {0, 0}, /* CMD44 */
  97. {0, 0}, /* CMD45 */
  98. {0, 0}, /* CMD46 */
  99. {0, 0}, /* CMD47 */
  100. {0, 0}, /* CMD48 */
  101. {0, 0}, /* CMD49 */
  102. {0, 0}, /* CMD50 */
  103. {0, 0}, /* CMD51 */
  104. {0, 0}, /* CMD52 */
  105. {0, 0}, /* CMD53 */
  106. {0, 0}, /* CMD54 */
  107. {0, 1}, /* CMD55 */
  108. {0xff, 0xff}, /* CMD56 */
  109. {0, 0}, /* CMD57 */
  110. {0, 0}, /* CMD58 */
  111. {0, 0}, /* CMD59 */
  112. {0, 0}, /* CMD60 */
  113. {0, 0}, /* CMD61 */
  114. {0, 0}, /* CMD62 */
  115. {0, 0} /* CMD63 */
  116. };
  117. static struct cvm_mmc_cr_mods cvm_mmc_get_cr_mods(struct mmc_command *cmd)
  118. {
  119. struct cvm_mmc_cr_type *cr;
  120. u8 hardware_ctype, hardware_rtype;
  121. u8 desired_ctype = 0, desired_rtype = 0;
  122. struct cvm_mmc_cr_mods r;
  123. cr = cvm_mmc_cr_types + (cmd->opcode & 0x3f);
  124. hardware_ctype = cr->ctype;
  125. hardware_rtype = cr->rtype;
  126. if (cmd->opcode == MMC_GEN_CMD)
  127. hardware_ctype = (cmd->arg & 1) ? 1 : 2;
  128. switch (mmc_cmd_type(cmd)) {
  129. case MMC_CMD_ADTC:
  130. desired_ctype = (cmd->data->flags & MMC_DATA_WRITE) ? 2 : 1;
  131. break;
  132. case MMC_CMD_AC:
  133. case MMC_CMD_BC:
  134. case MMC_CMD_BCR:
  135. desired_ctype = 0;
  136. break;
  137. }
  138. switch (mmc_resp_type(cmd)) {
  139. case MMC_RSP_NONE:
  140. desired_rtype = 0;
  141. break;
  142. case MMC_RSP_R1:/* MMC_RSP_R5, MMC_RSP_R6, MMC_RSP_R7 */
  143. case MMC_RSP_R1B:
  144. desired_rtype = 1;
  145. break;
  146. case MMC_RSP_R2:
  147. desired_rtype = 2;
  148. break;
  149. case MMC_RSP_R3: /* MMC_RSP_R4 */
  150. desired_rtype = 3;
  151. break;
  152. }
  153. r.ctype_xor = desired_ctype ^ hardware_ctype;
  154. r.rtype_xor = desired_rtype ^ hardware_rtype;
  155. return r;
  156. }
  157. static void check_switch_errors(struct cvm_mmc_host *host)
  158. {
  159. u64 emm_switch;
  160. emm_switch = readq(host->base + MIO_EMM_SWITCH(host));
  161. if (emm_switch & MIO_EMM_SWITCH_ERR0)
  162. dev_err(host->dev, "Switch power class error\n");
  163. if (emm_switch & MIO_EMM_SWITCH_ERR1)
  164. dev_err(host->dev, "Switch hs timing error\n");
  165. if (emm_switch & MIO_EMM_SWITCH_ERR2)
  166. dev_err(host->dev, "Switch bus width error\n");
  167. }
  168. static void clear_bus_id(u64 *reg)
  169. {
  170. u64 bus_id_mask = GENMASK_ULL(61, 60);
  171. *reg &= ~bus_id_mask;
  172. }
  173. static void set_bus_id(u64 *reg, int bus_id)
  174. {
  175. clear_bus_id(reg);
  176. *reg |= FIELD_PREP(GENMASK(61, 60), bus_id);
  177. }
  178. static int get_bus_id(u64 reg)
  179. {
  180. return FIELD_GET(GENMASK_ULL(61, 60), reg);
  181. }
  182. /*
  183. * We never set the switch_exe bit since that would interfere
  184. * with the commands send by the MMC core.
  185. */
  186. static void do_switch(struct cvm_mmc_host *host, u64 emm_switch)
  187. {
  188. int retries = 100;
  189. u64 rsp_sts;
  190. int bus_id;
  191. /*
  192. * Modes setting only taken from slot 0. Work around that hardware
  193. * issue by first switching to slot 0.
  194. */
  195. bus_id = get_bus_id(emm_switch);
  196. clear_bus_id(&emm_switch);
  197. writeq(emm_switch, host->base + MIO_EMM_SWITCH(host));
  198. set_bus_id(&emm_switch, bus_id);
  199. writeq(emm_switch, host->base + MIO_EMM_SWITCH(host));
  200. /* wait for the switch to finish */
  201. do {
  202. rsp_sts = readq(host->base + MIO_EMM_RSP_STS(host));
  203. if (!(rsp_sts & MIO_EMM_RSP_STS_SWITCH_VAL))
  204. break;
  205. udelay(10);
  206. } while (--retries);
  207. check_switch_errors(host);
  208. }
  209. static bool switch_val_changed(struct cvm_mmc_slot *slot, u64 new_val)
  210. {
  211. /* Match BUS_ID, HS_TIMING, BUS_WIDTH, POWER_CLASS, CLK_HI, CLK_LO */
  212. u64 match = 0x3001070fffffffffull;
  213. return (slot->cached_switch & match) != (new_val & match);
  214. }
  215. static void set_wdog(struct cvm_mmc_slot *slot, unsigned int ns)
  216. {
  217. u64 timeout;
  218. if (!slot->clock)
  219. return;
  220. if (ns)
  221. timeout = (slot->clock * ns) / NSEC_PER_SEC;
  222. else
  223. timeout = (slot->clock * 850ull) / 1000ull;
  224. writeq(timeout, slot->host->base + MIO_EMM_WDOG(slot->host));
  225. }
  226. static void cvm_mmc_reset_bus(struct cvm_mmc_slot *slot)
  227. {
  228. struct cvm_mmc_host *host = slot->host;
  229. u64 emm_switch, wdog;
  230. emm_switch = readq(slot->host->base + MIO_EMM_SWITCH(host));
  231. emm_switch &= ~(MIO_EMM_SWITCH_EXE | MIO_EMM_SWITCH_ERR0 |
  232. MIO_EMM_SWITCH_ERR1 | MIO_EMM_SWITCH_ERR2);
  233. set_bus_id(&emm_switch, slot->bus_id);
  234. wdog = readq(slot->host->base + MIO_EMM_WDOG(host));
  235. do_switch(slot->host, emm_switch);
  236. slot->cached_switch = emm_switch;
  237. msleep(20);
  238. writeq(wdog, slot->host->base + MIO_EMM_WDOG(host));
  239. }
  240. /* Switch to another slot if needed */
  241. static void cvm_mmc_switch_to(struct cvm_mmc_slot *slot)
  242. {
  243. struct cvm_mmc_host *host = slot->host;
  244. struct cvm_mmc_slot *old_slot;
  245. u64 emm_sample, emm_switch;
  246. if (slot->bus_id == host->last_slot)
  247. return;
  248. if (host->last_slot >= 0 && host->slot[host->last_slot]) {
  249. old_slot = host->slot[host->last_slot];
  250. old_slot->cached_switch = readq(host->base + MIO_EMM_SWITCH(host));
  251. old_slot->cached_rca = readq(host->base + MIO_EMM_RCA(host));
  252. }
  253. writeq(slot->cached_rca, host->base + MIO_EMM_RCA(host));
  254. emm_switch = slot->cached_switch;
  255. set_bus_id(&emm_switch, slot->bus_id);
  256. do_switch(host, emm_switch);
  257. emm_sample = FIELD_PREP(MIO_EMM_SAMPLE_CMD_CNT, slot->cmd_cnt) |
  258. FIELD_PREP(MIO_EMM_SAMPLE_DAT_CNT, slot->dat_cnt);
  259. writeq(emm_sample, host->base + MIO_EMM_SAMPLE(host));
  260. host->last_slot = slot->bus_id;
  261. }
  262. static void do_read(struct cvm_mmc_host *host, struct mmc_request *req,
  263. u64 dbuf)
  264. {
  265. struct sg_mapping_iter *smi = &host->smi;
  266. int data_len = req->data->blocks * req->data->blksz;
  267. int bytes_xfered, shift = -1;
  268. u64 dat = 0;
  269. /* Auto inc from offset zero */
  270. writeq((0x10000 | (dbuf << 6)), host->base + MIO_EMM_BUF_IDX(host));
  271. for (bytes_xfered = 0; bytes_xfered < data_len;) {
  272. if (smi->consumed >= smi->length) {
  273. if (!sg_miter_next(smi))
  274. break;
  275. smi->consumed = 0;
  276. }
  277. if (shift < 0) {
  278. dat = readq(host->base + MIO_EMM_BUF_DAT(host));
  279. shift = 56;
  280. }
  281. while (smi->consumed < smi->length && shift >= 0) {
  282. ((u8 *)smi->addr)[smi->consumed] = (dat >> shift) & 0xff;
  283. bytes_xfered++;
  284. smi->consumed++;
  285. shift -= 8;
  286. }
  287. }
  288. sg_miter_stop(smi);
  289. req->data->bytes_xfered = bytes_xfered;
  290. req->data->error = 0;
  291. }
  292. static void do_write(struct mmc_request *req)
  293. {
  294. req->data->bytes_xfered = req->data->blocks * req->data->blksz;
  295. req->data->error = 0;
  296. }
  297. static void set_cmd_response(struct cvm_mmc_host *host, struct mmc_request *req,
  298. u64 rsp_sts)
  299. {
  300. u64 rsp_hi, rsp_lo;
  301. if (!(rsp_sts & MIO_EMM_RSP_STS_RSP_VAL))
  302. return;
  303. rsp_lo = readq(host->base + MIO_EMM_RSP_LO(host));
  304. switch (FIELD_GET(MIO_EMM_RSP_STS_RSP_TYPE, rsp_sts)) {
  305. case 1:
  306. case 3:
  307. req->cmd->resp[0] = (rsp_lo >> 8) & 0xffffffff;
  308. req->cmd->resp[1] = 0;
  309. req->cmd->resp[2] = 0;
  310. req->cmd->resp[3] = 0;
  311. break;
  312. case 2:
  313. req->cmd->resp[3] = rsp_lo & 0xffffffff;
  314. req->cmd->resp[2] = (rsp_lo >> 32) & 0xffffffff;
  315. rsp_hi = readq(host->base + MIO_EMM_RSP_HI(host));
  316. req->cmd->resp[1] = rsp_hi & 0xffffffff;
  317. req->cmd->resp[0] = (rsp_hi >> 32) & 0xffffffff;
  318. break;
  319. }
  320. }
  321. static int get_dma_dir(struct mmc_data *data)
  322. {
  323. return (data->flags & MMC_DATA_WRITE) ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
  324. }
  325. static int finish_dma_single(struct cvm_mmc_host *host, struct mmc_data *data)
  326. {
  327. data->bytes_xfered = data->blocks * data->blksz;
  328. data->error = 0;
  329. dma_unmap_sg(host->dev, data->sg, data->sg_len, get_dma_dir(data));
  330. return 1;
  331. }
  332. static int finish_dma_sg(struct cvm_mmc_host *host, struct mmc_data *data)
  333. {
  334. u64 fifo_cfg;
  335. int count;
  336. /* Check if there are any pending requests left */
  337. fifo_cfg = readq(host->dma_base + MIO_EMM_DMA_FIFO_CFG(host));
  338. count = FIELD_GET(MIO_EMM_DMA_FIFO_CFG_COUNT, fifo_cfg);
  339. if (count)
  340. dev_err(host->dev, "%u requests still pending\n", count);
  341. data->bytes_xfered = data->blocks * data->blksz;
  342. data->error = 0;
  343. /* Clear and disable FIFO */
  344. writeq(BIT_ULL(16), host->dma_base + MIO_EMM_DMA_FIFO_CFG(host));
  345. dma_unmap_sg(host->dev, data->sg, data->sg_len, get_dma_dir(data));
  346. return 1;
  347. }
  348. static int finish_dma(struct cvm_mmc_host *host, struct mmc_data *data)
  349. {
  350. if (host->use_sg && data->sg_len > 1)
  351. return finish_dma_sg(host, data);
  352. else
  353. return finish_dma_single(host, data);
  354. }
  355. static int check_status(u64 rsp_sts)
  356. {
  357. if (rsp_sts & MIO_EMM_RSP_STS_RSP_BAD_STS ||
  358. rsp_sts & MIO_EMM_RSP_STS_RSP_CRC_ERR ||
  359. rsp_sts & MIO_EMM_RSP_STS_BLK_CRC_ERR)
  360. return -EILSEQ;
  361. if (rsp_sts & MIO_EMM_RSP_STS_RSP_TIMEOUT ||
  362. rsp_sts & MIO_EMM_RSP_STS_BLK_TIMEOUT)
  363. return -ETIMEDOUT;
  364. if (rsp_sts & MIO_EMM_RSP_STS_DBUF_ERR)
  365. return -EIO;
  366. return 0;
  367. }
  368. /* Try to clean up failed DMA. */
  369. static void cleanup_dma(struct cvm_mmc_host *host, u64 rsp_sts)
  370. {
  371. u64 emm_dma;
  372. emm_dma = readq(host->base + MIO_EMM_DMA(host));
  373. emm_dma |= FIELD_PREP(MIO_EMM_DMA_VAL, 1) |
  374. FIELD_PREP(MIO_EMM_DMA_DAT_NULL, 1);
  375. set_bus_id(&emm_dma, get_bus_id(rsp_sts));
  376. writeq(emm_dma, host->base + MIO_EMM_DMA(host));
  377. }
  378. irqreturn_t cvm_mmc_interrupt(int irq, void *dev_id)
  379. {
  380. struct cvm_mmc_host *host = dev_id;
  381. struct mmc_request *req;
  382. u64 emm_int, rsp_sts;
  383. bool host_done;
  384. if (host->need_irq_handler_lock)
  385. spin_lock(&host->irq_handler_lock);
  386. else
  387. __acquire(&host->irq_handler_lock);
  388. /* Clear interrupt bits (write 1 clears ). */
  389. emm_int = readq(host->base + MIO_EMM_INT(host));
  390. writeq(emm_int, host->base + MIO_EMM_INT(host));
  391. if (emm_int & MIO_EMM_INT_SWITCH_ERR)
  392. check_switch_errors(host);
  393. req = host->current_req;
  394. if (!req)
  395. goto out;
  396. rsp_sts = readq(host->base + MIO_EMM_RSP_STS(host));
  397. /*
  398. * dma_val set means DMA is still in progress. Don't touch
  399. * the request and wait for the interrupt indicating that
  400. * the DMA is finished.
  401. */
  402. if ((rsp_sts & MIO_EMM_RSP_STS_DMA_VAL) && host->dma_active)
  403. goto out;
  404. if (!host->dma_active && req->data &&
  405. (emm_int & MIO_EMM_INT_BUF_DONE)) {
  406. unsigned int type = (rsp_sts >> 7) & 3;
  407. if (type == 1)
  408. do_read(host, req, rsp_sts & MIO_EMM_RSP_STS_DBUF);
  409. else if (type == 2)
  410. do_write(req);
  411. }
  412. host_done = emm_int & MIO_EMM_INT_CMD_DONE ||
  413. emm_int & MIO_EMM_INT_DMA_DONE ||
  414. emm_int & MIO_EMM_INT_CMD_ERR ||
  415. emm_int & MIO_EMM_INT_DMA_ERR;
  416. if (!(host_done && req->done))
  417. goto no_req_done;
  418. req->cmd->error = check_status(rsp_sts);
  419. if (host->dma_active && req->data)
  420. if (!finish_dma(host, req->data))
  421. goto no_req_done;
  422. set_cmd_response(host, req, rsp_sts);
  423. if ((emm_int & MIO_EMM_INT_DMA_ERR) &&
  424. (rsp_sts & MIO_EMM_RSP_STS_DMA_PEND))
  425. cleanup_dma(host, rsp_sts);
  426. host->current_req = NULL;
  427. req->done(req);
  428. no_req_done:
  429. if (host->dmar_fixup_done)
  430. host->dmar_fixup_done(host);
  431. if (host_done)
  432. host->release_bus(host);
  433. out:
  434. if (host->need_irq_handler_lock)
  435. spin_unlock(&host->irq_handler_lock);
  436. else
  437. __release(&host->irq_handler_lock);
  438. return IRQ_RETVAL(emm_int != 0);
  439. }
  440. /*
  441. * Program DMA_CFG and if needed DMA_ADR.
  442. * Returns 0 on error, DMA address otherwise.
  443. */
  444. static u64 prepare_dma_single(struct cvm_mmc_host *host, struct mmc_data *data)
  445. {
  446. u64 dma_cfg, addr;
  447. int count, rw;
  448. count = dma_map_sg(host->dev, data->sg, data->sg_len,
  449. get_dma_dir(data));
  450. if (!count)
  451. return 0;
  452. rw = (data->flags & MMC_DATA_WRITE) ? 1 : 0;
  453. dma_cfg = FIELD_PREP(MIO_EMM_DMA_CFG_EN, 1) |
  454. FIELD_PREP(MIO_EMM_DMA_CFG_RW, rw);
  455. #ifdef __LITTLE_ENDIAN
  456. dma_cfg |= FIELD_PREP(MIO_EMM_DMA_CFG_ENDIAN, 1);
  457. #endif
  458. dma_cfg |= FIELD_PREP(MIO_EMM_DMA_CFG_SIZE,
  459. (sg_dma_len(&data->sg[0]) / 8) - 1);
  460. addr = sg_dma_address(&data->sg[0]);
  461. if (!host->big_dma_addr)
  462. dma_cfg |= FIELD_PREP(MIO_EMM_DMA_CFG_ADR, addr);
  463. writeq(dma_cfg, host->dma_base + MIO_EMM_DMA_CFG(host));
  464. pr_debug("[%s] sg_dma_len: %u total sg_elem: %d\n",
  465. (rw) ? "W" : "R", sg_dma_len(&data->sg[0]), count);
  466. if (host->big_dma_addr)
  467. writeq(addr, host->dma_base + MIO_EMM_DMA_ADR(host));
  468. return addr;
  469. }
  470. /*
  471. * Queue complete sg list into the FIFO.
  472. * Returns 0 on error, 1 otherwise.
  473. */
  474. static u64 prepare_dma_sg(struct cvm_mmc_host *host, struct mmc_data *data)
  475. {
  476. struct scatterlist *sg;
  477. u64 fifo_cmd, addr;
  478. int count, i, rw;
  479. count = dma_map_sg(host->dev, data->sg, data->sg_len,
  480. get_dma_dir(data));
  481. if (!count)
  482. return 0;
  483. if (count > 16)
  484. goto error;
  485. /* Enable FIFO by removing CLR bit */
  486. writeq(0, host->dma_base + MIO_EMM_DMA_FIFO_CFG(host));
  487. for_each_sg(data->sg, sg, count, i) {
  488. /* Program DMA address */
  489. addr = sg_dma_address(sg);
  490. if (addr & 7)
  491. goto error;
  492. writeq(addr, host->dma_base + MIO_EMM_DMA_FIFO_ADR(host));
  493. /*
  494. * If we have scatter-gather support we also have an extra
  495. * register for the DMA addr, so no need to check
  496. * host->big_dma_addr here.
  497. */
  498. rw = (data->flags & MMC_DATA_WRITE) ? 1 : 0;
  499. fifo_cmd = FIELD_PREP(MIO_EMM_DMA_FIFO_CMD_RW, rw);
  500. /* enable interrupts on the last element */
  501. fifo_cmd |= FIELD_PREP(MIO_EMM_DMA_FIFO_CMD_INTDIS,
  502. (i + 1 == count) ? 0 : 1);
  503. #ifdef __LITTLE_ENDIAN
  504. fifo_cmd |= FIELD_PREP(MIO_EMM_DMA_FIFO_CMD_ENDIAN, 1);
  505. #endif
  506. fifo_cmd |= FIELD_PREP(MIO_EMM_DMA_FIFO_CMD_SIZE,
  507. sg_dma_len(sg) / 8 - 1);
  508. /*
  509. * The write copies the address and the command to the FIFO
  510. * and increments the FIFO's COUNT field.
  511. */
  512. writeq(fifo_cmd, host->dma_base + MIO_EMM_DMA_FIFO_CMD(host));
  513. pr_debug("[%s] sg_dma_len: %u sg_elem: %d/%d\n",
  514. (rw) ? "W" : "R", sg_dma_len(sg), i, count);
  515. }
  516. /*
  517. * In difference to prepare_dma_single we don't return the
  518. * address here, as it would not make sense for scatter-gather.
  519. * The dma fixup is only required on models that don't support
  520. * scatter-gather, so that is not a problem.
  521. */
  522. return 1;
  523. error:
  524. WARN_ON_ONCE(1);
  525. dma_unmap_sg(host->dev, data->sg, data->sg_len, get_dma_dir(data));
  526. /* Disable FIFO */
  527. writeq(BIT_ULL(16), host->dma_base + MIO_EMM_DMA_FIFO_CFG(host));
  528. return 0;
  529. }
  530. static u64 prepare_dma(struct cvm_mmc_host *host, struct mmc_data *data)
  531. {
  532. if (host->use_sg && data->sg_len > 1)
  533. return prepare_dma_sg(host, data);
  534. else
  535. return prepare_dma_single(host, data);
  536. }
  537. static u64 prepare_ext_dma(struct mmc_host *mmc, struct mmc_request *mrq)
  538. {
  539. struct cvm_mmc_slot *slot = mmc_priv(mmc);
  540. u64 emm_dma;
  541. emm_dma = FIELD_PREP(MIO_EMM_DMA_VAL, 1) |
  542. FIELD_PREP(MIO_EMM_DMA_SECTOR,
  543. mmc_card_is_blockaddr(mmc->card) ? 1 : 0) |
  544. FIELD_PREP(MIO_EMM_DMA_RW,
  545. (mrq->data->flags & MMC_DATA_WRITE) ? 1 : 0) |
  546. FIELD_PREP(MIO_EMM_DMA_BLOCK_CNT, mrq->data->blocks) |
  547. FIELD_PREP(MIO_EMM_DMA_CARD_ADDR, mrq->cmd->arg);
  548. set_bus_id(&emm_dma, slot->bus_id);
  549. if (mmc_card_mmc(mmc->card) || (mmc_card_sd(mmc->card) &&
  550. (mmc->card->scr.cmds & SD_SCR_CMD23_SUPPORT)))
  551. emm_dma |= FIELD_PREP(MIO_EMM_DMA_MULTI, 1);
  552. pr_debug("[%s] blocks: %u multi: %d\n",
  553. (emm_dma & MIO_EMM_DMA_RW) ? "W" : "R",
  554. mrq->data->blocks, (emm_dma & MIO_EMM_DMA_MULTI) ? 1 : 0);
  555. return emm_dma;
  556. }
  557. static void cvm_mmc_dma_request(struct mmc_host *mmc,
  558. struct mmc_request *mrq)
  559. {
  560. struct cvm_mmc_slot *slot = mmc_priv(mmc);
  561. struct cvm_mmc_host *host = slot->host;
  562. struct mmc_data *data;
  563. u64 emm_dma, addr;
  564. if (!mrq->data || !mrq->data->sg || !mrq->data->sg_len ||
  565. !mrq->stop || mrq->stop->opcode != MMC_STOP_TRANSMISSION) {
  566. dev_err(&mmc->card->dev, "Error: %s no data\n", __func__);
  567. goto error;
  568. }
  569. cvm_mmc_switch_to(slot);
  570. data = mrq->data;
  571. pr_debug("DMA request blocks: %d block_size: %d total_size: %d\n",
  572. data->blocks, data->blksz, data->blocks * data->blksz);
  573. if (data->timeout_ns)
  574. set_wdog(slot, data->timeout_ns);
  575. WARN_ON(host->current_req);
  576. host->current_req = mrq;
  577. emm_dma = prepare_ext_dma(mmc, mrq);
  578. addr = prepare_dma(host, data);
  579. if (!addr) {
  580. dev_err(host->dev, "prepare_dma failed\n");
  581. goto error;
  582. }
  583. host->dma_active = true;
  584. host->int_enable(host, MIO_EMM_INT_CMD_ERR | MIO_EMM_INT_DMA_DONE |
  585. MIO_EMM_INT_DMA_ERR);
  586. if (host->dmar_fixup)
  587. host->dmar_fixup(host, mrq->cmd, data, addr);
  588. /*
  589. * If we have a valid SD card in the slot, we set the response
  590. * bit mask to check for CRC errors and timeouts only.
  591. * Otherwise, use the default power reset value.
  592. */
  593. if (mmc_card_sd(mmc->card))
  594. writeq(0x00b00000ull, host->base + MIO_EMM_STS_MASK(host));
  595. else
  596. writeq(0xe4390080ull, host->base + MIO_EMM_STS_MASK(host));
  597. writeq(emm_dma, host->base + MIO_EMM_DMA(host));
  598. return;
  599. error:
  600. mrq->cmd->error = -EINVAL;
  601. if (mrq->done)
  602. mrq->done(mrq);
  603. host->release_bus(host);
  604. }
  605. static void do_read_request(struct cvm_mmc_host *host, struct mmc_request *mrq)
  606. {
  607. sg_miter_start(&host->smi, mrq->data->sg, mrq->data->sg_len,
  608. SG_MITER_ATOMIC | SG_MITER_TO_SG);
  609. }
  610. static void do_write_request(struct cvm_mmc_host *host, struct mmc_request *mrq)
  611. {
  612. unsigned int data_len = mrq->data->blocks * mrq->data->blksz;
  613. struct sg_mapping_iter *smi = &host->smi;
  614. unsigned int bytes_xfered;
  615. int shift = 56;
  616. u64 dat = 0;
  617. /* Copy data to the xmit buffer before issuing the command. */
  618. sg_miter_start(smi, mrq->data->sg, mrq->data->sg_len, SG_MITER_FROM_SG);
  619. /* Auto inc from offset zero, dbuf zero */
  620. writeq(0x10000ull, host->base + MIO_EMM_BUF_IDX(host));
  621. for (bytes_xfered = 0; bytes_xfered < data_len;) {
  622. if (smi->consumed >= smi->length) {
  623. if (!sg_miter_next(smi))
  624. break;
  625. smi->consumed = 0;
  626. }
  627. while (smi->consumed < smi->length && shift >= 0) {
  628. dat |= (u64)((u8 *)smi->addr)[smi->consumed] << shift;
  629. bytes_xfered++;
  630. smi->consumed++;
  631. shift -= 8;
  632. }
  633. if (shift < 0) {
  634. writeq(dat, host->base + MIO_EMM_BUF_DAT(host));
  635. shift = 56;
  636. dat = 0;
  637. }
  638. }
  639. sg_miter_stop(smi);
  640. }
  641. static void cvm_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
  642. {
  643. struct cvm_mmc_slot *slot = mmc_priv(mmc);
  644. struct cvm_mmc_host *host = slot->host;
  645. struct mmc_command *cmd = mrq->cmd;
  646. struct cvm_mmc_cr_mods mods;
  647. u64 emm_cmd, rsp_sts;
  648. int retries = 100;
  649. /*
  650. * Note about locking:
  651. * All MMC devices share the same bus and controller. Allow only a
  652. * single user of the bootbus/MMC bus at a time. The lock is acquired
  653. * on all entry points from the MMC layer.
  654. *
  655. * For requests the lock is only released after the completion
  656. * interrupt!
  657. */
  658. host->acquire_bus(host);
  659. if (cmd->opcode == MMC_READ_MULTIPLE_BLOCK ||
  660. cmd->opcode == MMC_WRITE_MULTIPLE_BLOCK)
  661. return cvm_mmc_dma_request(mmc, mrq);
  662. cvm_mmc_switch_to(slot);
  663. mods = cvm_mmc_get_cr_mods(cmd);
  664. WARN_ON(host->current_req);
  665. host->current_req = mrq;
  666. if (cmd->data) {
  667. if (cmd->data->flags & MMC_DATA_READ)
  668. do_read_request(host, mrq);
  669. else
  670. do_write_request(host, mrq);
  671. if (cmd->data->timeout_ns)
  672. set_wdog(slot, cmd->data->timeout_ns);
  673. } else
  674. set_wdog(slot, 0);
  675. host->dma_active = false;
  676. host->int_enable(host, MIO_EMM_INT_CMD_DONE | MIO_EMM_INT_CMD_ERR);
  677. emm_cmd = FIELD_PREP(MIO_EMM_CMD_VAL, 1) |
  678. FIELD_PREP(MIO_EMM_CMD_CTYPE_XOR, mods.ctype_xor) |
  679. FIELD_PREP(MIO_EMM_CMD_RTYPE_XOR, mods.rtype_xor) |
  680. FIELD_PREP(MIO_EMM_CMD_IDX, cmd->opcode) |
  681. FIELD_PREP(MIO_EMM_CMD_ARG, cmd->arg);
  682. set_bus_id(&emm_cmd, slot->bus_id);
  683. if (cmd->data && mmc_cmd_type(cmd) == MMC_CMD_ADTC)
  684. emm_cmd |= FIELD_PREP(MIO_EMM_CMD_OFFSET,
  685. 64 - ((cmd->data->blocks * cmd->data->blksz) / 8));
  686. writeq(0, host->base + MIO_EMM_STS_MASK(host));
  687. retry:
  688. rsp_sts = readq(host->base + MIO_EMM_RSP_STS(host));
  689. if (rsp_sts & MIO_EMM_RSP_STS_DMA_VAL ||
  690. rsp_sts & MIO_EMM_RSP_STS_CMD_VAL ||
  691. rsp_sts & MIO_EMM_RSP_STS_SWITCH_VAL ||
  692. rsp_sts & MIO_EMM_RSP_STS_DMA_PEND) {
  693. udelay(10);
  694. if (--retries)
  695. goto retry;
  696. }
  697. if (!retries)
  698. dev_err(host->dev, "Bad status: %llx before command write\n", rsp_sts);
  699. writeq(emm_cmd, host->base + MIO_EMM_CMD(host));
  700. }
  701. static void cvm_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
  702. {
  703. struct cvm_mmc_slot *slot = mmc_priv(mmc);
  704. struct cvm_mmc_host *host = slot->host;
  705. int clk_period = 0, power_class = 10, bus_width = 0;
  706. u64 clock, emm_switch;
  707. host->acquire_bus(host);
  708. cvm_mmc_switch_to(slot);
  709. /* Set the power state */
  710. switch (ios->power_mode) {
  711. case MMC_POWER_ON:
  712. break;
  713. case MMC_POWER_OFF:
  714. cvm_mmc_reset_bus(slot);
  715. if (host->global_pwr_gpiod)
  716. host->set_shared_power(host, 0);
  717. else if (!IS_ERR(mmc->supply.vmmc))
  718. mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
  719. break;
  720. case MMC_POWER_UP:
  721. if (host->global_pwr_gpiod)
  722. host->set_shared_power(host, 1);
  723. else if (!IS_ERR(mmc->supply.vmmc))
  724. mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, ios->vdd);
  725. break;
  726. }
  727. /* Convert bus width to HW definition */
  728. switch (ios->bus_width) {
  729. case MMC_BUS_WIDTH_8:
  730. bus_width = 2;
  731. break;
  732. case MMC_BUS_WIDTH_4:
  733. bus_width = 1;
  734. break;
  735. case MMC_BUS_WIDTH_1:
  736. bus_width = 0;
  737. break;
  738. }
  739. /* DDR is available for 4/8 bit bus width */
  740. if (ios->bus_width && ios->timing == MMC_TIMING_MMC_DDR52)
  741. bus_width |= 4;
  742. /* Change the clock frequency. */
  743. clock = ios->clock;
  744. if (clock > 52000000)
  745. clock = 52000000;
  746. slot->clock = clock;
  747. if (clock)
  748. clk_period = (host->sys_freq + clock - 1) / (2 * clock);
  749. emm_switch = FIELD_PREP(MIO_EMM_SWITCH_HS_TIMING,
  750. (ios->timing == MMC_TIMING_MMC_HS)) |
  751. FIELD_PREP(MIO_EMM_SWITCH_BUS_WIDTH, bus_width) |
  752. FIELD_PREP(MIO_EMM_SWITCH_POWER_CLASS, power_class) |
  753. FIELD_PREP(MIO_EMM_SWITCH_CLK_HI, clk_period) |
  754. FIELD_PREP(MIO_EMM_SWITCH_CLK_LO, clk_period);
  755. set_bus_id(&emm_switch, slot->bus_id);
  756. if (!switch_val_changed(slot, emm_switch))
  757. goto out;
  758. set_wdog(slot, 0);
  759. do_switch(host, emm_switch);
  760. slot->cached_switch = emm_switch;
  761. out:
  762. host->release_bus(host);
  763. }
  764. static const struct mmc_host_ops cvm_mmc_ops = {
  765. .request = cvm_mmc_request,
  766. .set_ios = cvm_mmc_set_ios,
  767. .get_ro = mmc_gpio_get_ro,
  768. .get_cd = mmc_gpio_get_cd,
  769. };
  770. static void cvm_mmc_set_clock(struct cvm_mmc_slot *slot, unsigned int clock)
  771. {
  772. struct mmc_host *mmc = slot->mmc;
  773. clock = min(clock, mmc->f_max);
  774. clock = max(clock, mmc->f_min);
  775. slot->clock = clock;
  776. }
  777. static int cvm_mmc_init_lowlevel(struct cvm_mmc_slot *slot)
  778. {
  779. struct cvm_mmc_host *host = slot->host;
  780. u64 emm_switch;
  781. /* Enable this bus slot. */
  782. host->emm_cfg |= (1ull << slot->bus_id);
  783. writeq(host->emm_cfg, slot->host->base + MIO_EMM_CFG(host));
  784. udelay(10);
  785. /* Program initial clock speed and power. */
  786. cvm_mmc_set_clock(slot, slot->mmc->f_min);
  787. emm_switch = FIELD_PREP(MIO_EMM_SWITCH_POWER_CLASS, 10);
  788. emm_switch |= FIELD_PREP(MIO_EMM_SWITCH_CLK_HI,
  789. (host->sys_freq / slot->clock) / 2);
  790. emm_switch |= FIELD_PREP(MIO_EMM_SWITCH_CLK_LO,
  791. (host->sys_freq / slot->clock) / 2);
  792. /* Make the changes take effect on this bus slot. */
  793. set_bus_id(&emm_switch, slot->bus_id);
  794. do_switch(host, emm_switch);
  795. slot->cached_switch = emm_switch;
  796. /*
  797. * Set watchdog timeout value and default reset value
  798. * for the mask register. Finally, set the CARD_RCA
  799. * bit so that we can get the card address relative
  800. * to the CMD register for CMD7 transactions.
  801. */
  802. set_wdog(slot, 0);
  803. writeq(0xe4390080ull, host->base + MIO_EMM_STS_MASK(host));
  804. writeq(1, host->base + MIO_EMM_RCA(host));
  805. return 0;
  806. }
  807. static int cvm_mmc_of_parse(struct device *dev, struct cvm_mmc_slot *slot)
  808. {
  809. u32 id, cmd_skew = 0, dat_skew = 0, bus_width = 0;
  810. struct device_node *node = dev->of_node;
  811. struct mmc_host *mmc = slot->mmc;
  812. u64 clock_period;
  813. int ret;
  814. ret = of_property_read_u32(node, "reg", &id);
  815. if (ret) {
  816. dev_err(dev, "Missing or invalid reg property on %pOF\n", node);
  817. return ret;
  818. }
  819. if (id >= CAVIUM_MAX_MMC || slot->host->slot[id]) {
  820. dev_err(dev, "Invalid reg property on %pOF\n", node);
  821. return -EINVAL;
  822. }
  823. ret = mmc_regulator_get_supply(mmc);
  824. if (ret)
  825. return ret;
  826. /*
  827. * Legacy Octeon firmware has no regulator entry, fall-back to
  828. * a hard-coded voltage to get a sane OCR.
  829. */
  830. if (IS_ERR(mmc->supply.vmmc))
  831. mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
  832. /* Common MMC bindings */
  833. ret = mmc_of_parse(mmc);
  834. if (ret)
  835. return ret;
  836. /* Set bus width */
  837. if (!(mmc->caps & (MMC_CAP_8_BIT_DATA | MMC_CAP_4_BIT_DATA))) {
  838. of_property_read_u32(node, "cavium,bus-max-width", &bus_width);
  839. if (bus_width == 8)
  840. mmc->caps |= MMC_CAP_8_BIT_DATA | MMC_CAP_4_BIT_DATA;
  841. else if (bus_width == 4)
  842. mmc->caps |= MMC_CAP_4_BIT_DATA;
  843. }
  844. /* Set maximum and minimum frequency */
  845. if (!mmc->f_max)
  846. of_property_read_u32(node, "spi-max-frequency", &mmc->f_max);
  847. if (!mmc->f_max || mmc->f_max > 52000000)
  848. mmc->f_max = 52000000;
  849. mmc->f_min = 400000;
  850. /* Sampling register settings, period in picoseconds */
  851. clock_period = 1000000000000ull / slot->host->sys_freq;
  852. of_property_read_u32(node, "cavium,cmd-clk-skew", &cmd_skew);
  853. of_property_read_u32(node, "cavium,dat-clk-skew", &dat_skew);
  854. slot->cmd_cnt = (cmd_skew + clock_period / 2) / clock_period;
  855. slot->dat_cnt = (dat_skew + clock_period / 2) / clock_period;
  856. return id;
  857. }
  858. int cvm_mmc_of_slot_probe(struct device *dev, struct cvm_mmc_host *host)
  859. {
  860. struct cvm_mmc_slot *slot;
  861. struct mmc_host *mmc;
  862. int ret, id;
  863. mmc = mmc_alloc_host(sizeof(struct cvm_mmc_slot), dev);
  864. if (!mmc)
  865. return -ENOMEM;
  866. slot = mmc_priv(mmc);
  867. slot->mmc = mmc;
  868. slot->host = host;
  869. ret = cvm_mmc_of_parse(dev, slot);
  870. if (ret < 0)
  871. goto error;
  872. id = ret;
  873. /* Set up host parameters */
  874. mmc->ops = &cvm_mmc_ops;
  875. /*
  876. * We only have a 3.3v supply, we cannot support any
  877. * of the UHS modes. We do support the high speed DDR
  878. * modes up to 52MHz.
  879. *
  880. * Disable bounce buffers for max_segs = 1
  881. */
  882. mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
  883. MMC_CAP_CMD23 | MMC_CAP_POWER_OFF_CARD | MMC_CAP_3_3V_DDR;
  884. if (host->use_sg)
  885. mmc->max_segs = 16;
  886. else
  887. mmc->max_segs = 1;
  888. /* DMA size field can address up to 8 MB */
  889. mmc->max_seg_size = min_t(unsigned int, 8 * 1024 * 1024,
  890. dma_get_max_seg_size(host->dev));
  891. mmc->max_req_size = mmc->max_seg_size;
  892. /* External DMA is in 512 byte blocks */
  893. mmc->max_blk_size = 512;
  894. /* DMA block count field is 15 bits */
  895. mmc->max_blk_count = 32767;
  896. slot->clock = mmc->f_min;
  897. slot->bus_id = id;
  898. slot->cached_rca = 1;
  899. host->acquire_bus(host);
  900. host->slot[id] = slot;
  901. cvm_mmc_switch_to(slot);
  902. cvm_mmc_init_lowlevel(slot);
  903. host->release_bus(host);
  904. ret = mmc_add_host(mmc);
  905. if (ret) {
  906. dev_err(dev, "mmc_add_host() returned %d\n", ret);
  907. slot->host->slot[id] = NULL;
  908. goto error;
  909. }
  910. return 0;
  911. error:
  912. mmc_free_host(slot->mmc);
  913. return ret;
  914. }
  915. int cvm_mmc_of_slot_remove(struct cvm_mmc_slot *slot)
  916. {
  917. mmc_remove_host(slot->mmc);
  918. slot->host->slot[slot->bus_id] = NULL;
  919. mmc_free_host(slot->mmc);
  920. return 0;
  921. }