cavium-octeon.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * Driver for MMC and SSD cards for Cavium OCTEON SOCs.
  3. *
  4. * This file is subject to the terms and conditions of the GNU General Public
  5. * License. See the file "COPYING" in the main directory of this archive
  6. * for more details.
  7. *
  8. * Copyright (C) 2012-2017 Cavium Inc.
  9. */
  10. #include <linux/dma-mapping.h>
  11. #include <linux/gpio/consumer.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/mmc/mmc.h>
  14. #include <linux/mmc/slot-gpio.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/of_platform.h>
  18. #include <linux/platform_device.h>
  19. #include <asm/octeon/octeon.h>
  20. #include "cavium.h"
  21. #define CVMX_MIO_BOOT_CTL CVMX_ADD_IO_SEG(0x00011800000000D0ull)
  22. /*
  23. * The l2c* functions below are used for the EMMC-17978 workaround.
  24. *
  25. * Due to a bug in the design of the MMC bus hardware, the 2nd to last
  26. * cache block of a DMA read must be locked into the L2 Cache.
  27. * Otherwise, data corruption may occur.
  28. */
  29. static inline void *phys_to_ptr(u64 address)
  30. {
  31. return (void *)(address | (1ull << 63)); /* XKPHYS */
  32. }
  33. /*
  34. * Lock a single line into L2. The line is zeroed before locking
  35. * to make sure no dram accesses are made.
  36. */
  37. static void l2c_lock_line(u64 addr)
  38. {
  39. char *addr_ptr = phys_to_ptr(addr);
  40. asm volatile (
  41. "cache 31, %[line]" /* Unlock the line */
  42. ::[line] "m" (*addr_ptr));
  43. }
  44. /* Unlock a single line in the L2 cache. */
  45. static void l2c_unlock_line(u64 addr)
  46. {
  47. char *addr_ptr = phys_to_ptr(addr);
  48. asm volatile (
  49. "cache 23, %[line]" /* Unlock the line */
  50. ::[line] "m" (*addr_ptr));
  51. }
  52. /* Locks a memory region in the L2 cache. */
  53. static void l2c_lock_mem_region(u64 start, u64 len)
  54. {
  55. u64 end;
  56. /* Round start/end to cache line boundaries */
  57. end = ALIGN(start + len - 1, CVMX_CACHE_LINE_SIZE);
  58. start = ALIGN(start, CVMX_CACHE_LINE_SIZE);
  59. while (start <= end) {
  60. l2c_lock_line(start);
  61. start += CVMX_CACHE_LINE_SIZE;
  62. }
  63. asm volatile("sync");
  64. }
  65. /* Unlock a memory region in the L2 cache. */
  66. static void l2c_unlock_mem_region(u64 start, u64 len)
  67. {
  68. u64 end;
  69. /* Round start/end to cache line boundaries */
  70. end = ALIGN(start + len - 1, CVMX_CACHE_LINE_SIZE);
  71. start = ALIGN(start, CVMX_CACHE_LINE_SIZE);
  72. while (start <= end) {
  73. l2c_unlock_line(start);
  74. start += CVMX_CACHE_LINE_SIZE;
  75. }
  76. }
  77. static void octeon_mmc_acquire_bus(struct cvm_mmc_host *host)
  78. {
  79. if (!host->has_ciu3) {
  80. down(&octeon_bootbus_sem);
  81. /* For CN70XX, switch the MMC controller onto the bus. */
  82. if (OCTEON_IS_MODEL(OCTEON_CN70XX))
  83. writeq(0, (void __iomem *)CVMX_MIO_BOOT_CTL);
  84. } else {
  85. down(&host->mmc_serializer);
  86. }
  87. }
  88. static void octeon_mmc_release_bus(struct cvm_mmc_host *host)
  89. {
  90. if (!host->has_ciu3)
  91. up(&octeon_bootbus_sem);
  92. else
  93. up(&host->mmc_serializer);
  94. }
  95. static void octeon_mmc_int_enable(struct cvm_mmc_host *host, u64 val)
  96. {
  97. writeq(val, host->base + MIO_EMM_INT(host));
  98. if (!host->has_ciu3)
  99. writeq(val, host->base + MIO_EMM_INT_EN(host));
  100. }
  101. static void octeon_mmc_set_shared_power(struct cvm_mmc_host *host, int dir)
  102. {
  103. if (dir == 0)
  104. if (!atomic_dec_return(&host->shared_power_users))
  105. gpiod_set_value_cansleep(host->global_pwr_gpiod, 0);
  106. if (dir == 1)
  107. if (atomic_inc_return(&host->shared_power_users) == 1)
  108. gpiod_set_value_cansleep(host->global_pwr_gpiod, 1);
  109. }
  110. static void octeon_mmc_dmar_fixup(struct cvm_mmc_host *host,
  111. struct mmc_command *cmd,
  112. struct mmc_data *data,
  113. u64 addr)
  114. {
  115. if (cmd->opcode != MMC_WRITE_MULTIPLE_BLOCK)
  116. return;
  117. if (data->blksz * data->blocks <= 1024)
  118. return;
  119. host->n_minus_one = addr + (data->blksz * data->blocks) - 1024;
  120. l2c_lock_mem_region(host->n_minus_one, 512);
  121. }
  122. static void octeon_mmc_dmar_fixup_done(struct cvm_mmc_host *host)
  123. {
  124. if (!host->n_minus_one)
  125. return;
  126. l2c_unlock_mem_region(host->n_minus_one, 512);
  127. host->n_minus_one = 0;
  128. }
  129. static int octeon_mmc_probe(struct platform_device *pdev)
  130. {
  131. struct device_node *cn, *node = pdev->dev.of_node;
  132. struct cvm_mmc_host *host;
  133. void __iomem *base;
  134. int mmc_irq[9];
  135. int i, ret = 0;
  136. u64 val;
  137. host = devm_kzalloc(&pdev->dev, sizeof(*host), GFP_KERNEL);
  138. if (!host)
  139. return -ENOMEM;
  140. spin_lock_init(&host->irq_handler_lock);
  141. sema_init(&host->mmc_serializer, 1);
  142. host->dev = &pdev->dev;
  143. host->acquire_bus = octeon_mmc_acquire_bus;
  144. host->release_bus = octeon_mmc_release_bus;
  145. host->int_enable = octeon_mmc_int_enable;
  146. host->set_shared_power = octeon_mmc_set_shared_power;
  147. if (OCTEON_IS_MODEL(OCTEON_CN6XXX) ||
  148. OCTEON_IS_MODEL(OCTEON_CNF7XXX)) {
  149. host->dmar_fixup = octeon_mmc_dmar_fixup;
  150. host->dmar_fixup_done = octeon_mmc_dmar_fixup_done;
  151. }
  152. host->sys_freq = octeon_get_io_clock_rate();
  153. if (of_device_is_compatible(node, "cavium,octeon-7890-mmc")) {
  154. host->big_dma_addr = true;
  155. host->need_irq_handler_lock = true;
  156. host->has_ciu3 = true;
  157. host->use_sg = true;
  158. /*
  159. * First seven are the EMM_INT bits 0..6, then two for
  160. * the EMM_DMA_INT bits
  161. */
  162. for (i = 0; i < 9; i++) {
  163. mmc_irq[i] = platform_get_irq(pdev, i);
  164. if (mmc_irq[i] < 0)
  165. return mmc_irq[i];
  166. /* work around legacy u-boot device trees */
  167. irq_set_irq_type(mmc_irq[i], IRQ_TYPE_EDGE_RISING);
  168. }
  169. } else {
  170. host->big_dma_addr = false;
  171. host->need_irq_handler_lock = false;
  172. host->has_ciu3 = false;
  173. /* First one is EMM second DMA */
  174. for (i = 0; i < 2; i++) {
  175. mmc_irq[i] = platform_get_irq(pdev, i);
  176. if (mmc_irq[i] < 0)
  177. return mmc_irq[i];
  178. }
  179. }
  180. host->last_slot = -1;
  181. base = devm_platform_ioremap_resource(pdev, 0);
  182. if (IS_ERR(base))
  183. return PTR_ERR(base);
  184. host->base = base;
  185. host->reg_off = 0;
  186. base = devm_platform_ioremap_resource(pdev, 1);
  187. if (IS_ERR(base))
  188. return PTR_ERR(base);
  189. host->dma_base = base;
  190. /*
  191. * To keep the register addresses shared we intentionally use
  192. * a negative offset here, first register used on Octeon therefore
  193. * starts at 0x20 (MIO_EMM_DMA_CFG).
  194. */
  195. host->reg_off_dma = -0x20;
  196. ret = dma_set_mask(&pdev->dev, DMA_BIT_MASK(64));
  197. if (ret)
  198. return ret;
  199. /*
  200. * Clear out any pending interrupts that may be left over from
  201. * bootloader.
  202. */
  203. val = readq(host->base + MIO_EMM_INT(host));
  204. writeq(val, host->base + MIO_EMM_INT(host));
  205. if (host->has_ciu3) {
  206. /* Only CMD_DONE, DMA_DONE, CMD_ERR, DMA_ERR */
  207. for (i = 1; i <= 4; i++) {
  208. ret = devm_request_irq(&pdev->dev, mmc_irq[i],
  209. cvm_mmc_interrupt,
  210. 0, cvm_mmc_irq_names[i], host);
  211. if (ret < 0) {
  212. dev_err(&pdev->dev, "Error: devm_request_irq %d\n",
  213. mmc_irq[i]);
  214. return ret;
  215. }
  216. }
  217. } else {
  218. ret = devm_request_irq(&pdev->dev, mmc_irq[0],
  219. cvm_mmc_interrupt, 0, KBUILD_MODNAME,
  220. host);
  221. if (ret < 0) {
  222. dev_err(&pdev->dev, "Error: devm_request_irq %d\n",
  223. mmc_irq[0]);
  224. return ret;
  225. }
  226. }
  227. host->global_pwr_gpiod = devm_gpiod_get_optional(&pdev->dev,
  228. "power",
  229. GPIOD_OUT_HIGH);
  230. if (IS_ERR(host->global_pwr_gpiod)) {
  231. dev_err(&pdev->dev, "Invalid power GPIO\n");
  232. return PTR_ERR(host->global_pwr_gpiod);
  233. }
  234. platform_set_drvdata(pdev, host);
  235. i = 0;
  236. for_each_child_of_node(node, cn) {
  237. host->slot_pdev[i] =
  238. of_platform_device_create(cn, NULL, &pdev->dev);
  239. if (!host->slot_pdev[i]) {
  240. i++;
  241. continue;
  242. }
  243. ret = cvm_mmc_of_slot_probe(&host->slot_pdev[i]->dev, host);
  244. if (ret) {
  245. dev_err(&pdev->dev, "Error populating slots\n");
  246. octeon_mmc_set_shared_power(host, 0);
  247. of_node_put(cn);
  248. goto error;
  249. }
  250. i++;
  251. }
  252. return 0;
  253. error:
  254. for (i = 0; i < CAVIUM_MAX_MMC; i++) {
  255. if (host->slot[i])
  256. cvm_mmc_of_slot_remove(host->slot[i]);
  257. if (host->slot_pdev[i])
  258. of_platform_device_destroy(&host->slot_pdev[i]->dev, NULL);
  259. }
  260. return ret;
  261. }
  262. static void octeon_mmc_remove(struct platform_device *pdev)
  263. {
  264. struct cvm_mmc_host *host = platform_get_drvdata(pdev);
  265. u64 dma_cfg;
  266. int i;
  267. for (i = 0; i < CAVIUM_MAX_MMC; i++)
  268. if (host->slot[i])
  269. cvm_mmc_of_slot_remove(host->slot[i]);
  270. dma_cfg = readq(host->dma_base + MIO_EMM_DMA_CFG(host));
  271. dma_cfg &= ~MIO_EMM_DMA_CFG_EN;
  272. writeq(dma_cfg, host->dma_base + MIO_EMM_DMA_CFG(host));
  273. octeon_mmc_set_shared_power(host, 0);
  274. }
  275. static const struct of_device_id octeon_mmc_match[] = {
  276. {
  277. .compatible = "cavium,octeon-6130-mmc",
  278. },
  279. {
  280. .compatible = "cavium,octeon-7890-mmc",
  281. },
  282. {},
  283. };
  284. MODULE_DEVICE_TABLE(of, octeon_mmc_match);
  285. static struct platform_driver octeon_mmc_driver = {
  286. .probe = octeon_mmc_probe,
  287. .remove = octeon_mmc_remove,
  288. .driver = {
  289. .name = KBUILD_MODNAME,
  290. .probe_type = PROBE_PREFER_ASYNCHRONOUS,
  291. .of_match_table = octeon_mmc_match,
  292. },
  293. };
  294. module_platform_driver(octeon_mmc_driver);
  295. MODULE_AUTHOR("Cavium Inc. <support@cavium.com>");
  296. MODULE_DESCRIPTION("Low-level driver for Cavium OCTEON MMC/SSD card");
  297. MODULE_LICENSE("GPL");