spi-fsl-cpm.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Freescale SPI controller driver cpm functions.
  4. *
  5. * Maintainer: Kumar Gala
  6. *
  7. * Copyright (C) 2006 Polycom, Inc.
  8. * Copyright 2010 Freescale Semiconductor, Inc.
  9. *
  10. * CPM SPI and QE buffer descriptors mode support:
  11. * Copyright (c) 2009 MontaVista Software, Inc.
  12. * Author: Anton Vorontsov <[email protected]>
  13. */
  14. #include <asm/cpm.h>
  15. #include <soc/fsl/qe/qe.h>
  16. #include <linux/dma-mapping.h>
  17. #include <linux/fsl_devices.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/of_address.h>
  21. #include <linux/spi/spi.h>
  22. #include <linux/types.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/byteorder/generic.h>
  25. #include "spi-fsl-cpm.h"
  26. #include "spi-fsl-lib.h"
  27. #include "spi-fsl-spi.h"
  28. /* CPM1 and CPM2 are mutually exclusive. */
  29. #ifdef CONFIG_CPM1
  30. #include <asm/cpm1.h>
  31. #define CPM_SPI_CMD mk_cr_cmd(CPM_CR_CH_SPI, 0)
  32. #else
  33. #include <asm/cpm2.h>
  34. #define CPM_SPI_CMD mk_cr_cmd(CPM_CR_SPI_PAGE, CPM_CR_SPI_SBLOCK, 0, 0)
  35. #endif
  36. #define SPIE_TXB 0x00000200 /* Last char is written to tx fifo */
  37. #define SPIE_RXB 0x00000100 /* Last char is written to rx buf */
  38. /* SPCOM register values */
  39. #define SPCOM_STR (1 << 23) /* Start transmit */
  40. #define SPI_PRAM_SIZE 0x100
  41. #define SPI_MRBLR ((unsigned int)PAGE_SIZE)
  42. static void *fsl_dummy_rx;
  43. static DEFINE_MUTEX(fsl_dummy_rx_lock);
  44. static int fsl_dummy_rx_refcnt;
  45. void fsl_spi_cpm_reinit_txrx(struct mpc8xxx_spi *mspi)
  46. {
  47. if (mspi->flags & SPI_QE) {
  48. qe_issue_cmd(QE_INIT_TX_RX, mspi->subblock,
  49. QE_CR_PROTOCOL_UNSPECIFIED, 0);
  50. } else {
  51. if (mspi->flags & SPI_CPM1) {
  52. out_be32(&mspi->pram->rstate, 0);
  53. out_be16(&mspi->pram->rbptr,
  54. in_be16(&mspi->pram->rbase));
  55. out_be32(&mspi->pram->tstate, 0);
  56. out_be16(&mspi->pram->tbptr,
  57. in_be16(&mspi->pram->tbase));
  58. } else {
  59. cpm_command(CPM_SPI_CMD, CPM_CR_INIT_TRX);
  60. }
  61. }
  62. }
  63. EXPORT_SYMBOL_GPL(fsl_spi_cpm_reinit_txrx);
  64. static void fsl_spi_cpm_bufs_start(struct mpc8xxx_spi *mspi)
  65. {
  66. struct cpm_buf_desc __iomem *tx_bd = mspi->tx_bd;
  67. struct cpm_buf_desc __iomem *rx_bd = mspi->rx_bd;
  68. unsigned int xfer_len = min(mspi->count, SPI_MRBLR);
  69. unsigned int xfer_ofs;
  70. struct fsl_spi_reg *reg_base = mspi->reg_base;
  71. xfer_ofs = mspi->xfer_in_progress->len - mspi->count;
  72. if (mspi->rx_dma == mspi->dma_dummy_rx)
  73. out_be32(&rx_bd->cbd_bufaddr, mspi->rx_dma);
  74. else
  75. out_be32(&rx_bd->cbd_bufaddr, mspi->rx_dma + xfer_ofs);
  76. out_be16(&rx_bd->cbd_datlen, 0);
  77. out_be16(&rx_bd->cbd_sc, BD_SC_EMPTY | BD_SC_INTRPT | BD_SC_WRAP);
  78. if (mspi->tx_dma == mspi->dma_dummy_tx)
  79. out_be32(&tx_bd->cbd_bufaddr, mspi->tx_dma);
  80. else
  81. out_be32(&tx_bd->cbd_bufaddr, mspi->tx_dma + xfer_ofs);
  82. out_be16(&tx_bd->cbd_datlen, xfer_len);
  83. out_be16(&tx_bd->cbd_sc, BD_SC_READY | BD_SC_INTRPT | BD_SC_WRAP |
  84. BD_SC_LAST);
  85. /* start transfer */
  86. mpc8xxx_spi_write_reg(&reg_base->command, SPCOM_STR);
  87. }
  88. int fsl_spi_cpm_bufs(struct mpc8xxx_spi *mspi,
  89. struct spi_transfer *t, bool is_dma_mapped)
  90. {
  91. struct device *dev = mspi->dev;
  92. struct fsl_spi_reg *reg_base = mspi->reg_base;
  93. if (is_dma_mapped) {
  94. mspi->map_tx_dma = 0;
  95. mspi->map_rx_dma = 0;
  96. } else {
  97. mspi->map_tx_dma = 1;
  98. mspi->map_rx_dma = 1;
  99. }
  100. if (!t->tx_buf) {
  101. mspi->tx_dma = mspi->dma_dummy_tx;
  102. mspi->map_tx_dma = 0;
  103. }
  104. if (!t->rx_buf) {
  105. mspi->rx_dma = mspi->dma_dummy_rx;
  106. mspi->map_rx_dma = 0;
  107. }
  108. if (t->bits_per_word == 16 && t->tx_buf) {
  109. const u16 *src = t->tx_buf;
  110. u16 *dst;
  111. int i;
  112. dst = kmalloc(t->len, GFP_KERNEL);
  113. if (!dst)
  114. return -ENOMEM;
  115. for (i = 0; i < t->len >> 1; i++)
  116. dst[i] = cpu_to_le16p(src + i);
  117. mspi->tx = dst;
  118. mspi->map_tx_dma = 1;
  119. }
  120. if (mspi->map_tx_dma) {
  121. void *nonconst_tx = (void *)mspi->tx; /* shut up gcc */
  122. mspi->tx_dma = dma_map_single(dev, nonconst_tx, t->len,
  123. DMA_TO_DEVICE);
  124. if (dma_mapping_error(dev, mspi->tx_dma)) {
  125. dev_err(dev, "unable to map tx dma\n");
  126. return -ENOMEM;
  127. }
  128. } else if (t->tx_buf) {
  129. mspi->tx_dma = t->tx_dma;
  130. }
  131. if (mspi->map_rx_dma) {
  132. mspi->rx_dma = dma_map_single(dev, mspi->rx, t->len,
  133. DMA_FROM_DEVICE);
  134. if (dma_mapping_error(dev, mspi->rx_dma)) {
  135. dev_err(dev, "unable to map rx dma\n");
  136. goto err_rx_dma;
  137. }
  138. } else if (t->rx_buf) {
  139. mspi->rx_dma = t->rx_dma;
  140. }
  141. /* enable rx ints */
  142. mpc8xxx_spi_write_reg(&reg_base->mask, SPIE_RXB);
  143. mspi->xfer_in_progress = t;
  144. mspi->count = t->len;
  145. /* start CPM transfers */
  146. fsl_spi_cpm_bufs_start(mspi);
  147. return 0;
  148. err_rx_dma:
  149. if (mspi->map_tx_dma)
  150. dma_unmap_single(dev, mspi->tx_dma, t->len, DMA_TO_DEVICE);
  151. return -ENOMEM;
  152. }
  153. EXPORT_SYMBOL_GPL(fsl_spi_cpm_bufs);
  154. void fsl_spi_cpm_bufs_complete(struct mpc8xxx_spi *mspi)
  155. {
  156. struct device *dev = mspi->dev;
  157. struct spi_transfer *t = mspi->xfer_in_progress;
  158. if (mspi->map_tx_dma)
  159. dma_unmap_single(dev, mspi->tx_dma, t->len, DMA_TO_DEVICE);
  160. if (mspi->map_rx_dma)
  161. dma_unmap_single(dev, mspi->rx_dma, t->len, DMA_FROM_DEVICE);
  162. mspi->xfer_in_progress = NULL;
  163. if (t->bits_per_word == 16 && t->rx_buf) {
  164. int i;
  165. for (i = 0; i < t->len; i += 2)
  166. le16_to_cpus(t->rx_buf + i);
  167. }
  168. }
  169. EXPORT_SYMBOL_GPL(fsl_spi_cpm_bufs_complete);
  170. void fsl_spi_cpm_irq(struct mpc8xxx_spi *mspi, u32 events)
  171. {
  172. u16 len;
  173. struct fsl_spi_reg *reg_base = mspi->reg_base;
  174. dev_dbg(mspi->dev, "%s: bd datlen %d, count %d\n", __func__,
  175. in_be16(&mspi->rx_bd->cbd_datlen), mspi->count);
  176. len = in_be16(&mspi->rx_bd->cbd_datlen);
  177. if (len > mspi->count) {
  178. WARN_ON(1);
  179. len = mspi->count;
  180. }
  181. /* Clear the events */
  182. mpc8xxx_spi_write_reg(&reg_base->event, events);
  183. mspi->count -= len;
  184. if (mspi->count)
  185. fsl_spi_cpm_bufs_start(mspi);
  186. else
  187. complete(&mspi->done);
  188. }
  189. EXPORT_SYMBOL_GPL(fsl_spi_cpm_irq);
  190. static void *fsl_spi_alloc_dummy_rx(void)
  191. {
  192. mutex_lock(&fsl_dummy_rx_lock);
  193. if (!fsl_dummy_rx)
  194. fsl_dummy_rx = kmalloc(SPI_MRBLR, GFP_KERNEL);
  195. if (fsl_dummy_rx)
  196. fsl_dummy_rx_refcnt++;
  197. mutex_unlock(&fsl_dummy_rx_lock);
  198. return fsl_dummy_rx;
  199. }
  200. static void fsl_spi_free_dummy_rx(void)
  201. {
  202. mutex_lock(&fsl_dummy_rx_lock);
  203. switch (fsl_dummy_rx_refcnt) {
  204. case 0:
  205. WARN_ON(1);
  206. break;
  207. case 1:
  208. kfree(fsl_dummy_rx);
  209. fsl_dummy_rx = NULL;
  210. fallthrough;
  211. default:
  212. fsl_dummy_rx_refcnt--;
  213. break;
  214. }
  215. mutex_unlock(&fsl_dummy_rx_lock);
  216. }
  217. static unsigned long fsl_spi_cpm_get_pram(struct mpc8xxx_spi *mspi)
  218. {
  219. struct device *dev = mspi->dev;
  220. struct device_node *np = dev->of_node;
  221. const u32 *iprop;
  222. int size;
  223. void __iomem *spi_base;
  224. unsigned long pram_ofs = -ENOMEM;
  225. /* Can't use of_address_to_resource(), QE muram isn't at 0. */
  226. iprop = of_get_property(np, "reg", &size);
  227. /* QE with a fixed pram location? */
  228. if (mspi->flags & SPI_QE && iprop && size == sizeof(*iprop) * 4)
  229. return cpm_muram_alloc_fixed(iprop[2], SPI_PRAM_SIZE);
  230. /* QE but with a dynamic pram location? */
  231. if (mspi->flags & SPI_QE) {
  232. pram_ofs = cpm_muram_alloc(SPI_PRAM_SIZE, 64);
  233. qe_issue_cmd(QE_ASSIGN_PAGE_TO_DEVICE, mspi->subblock,
  234. QE_CR_PROTOCOL_UNSPECIFIED, pram_ofs);
  235. return pram_ofs;
  236. }
  237. spi_base = of_iomap(np, 1);
  238. if (spi_base == NULL)
  239. return -EINVAL;
  240. if (mspi->flags & SPI_CPM2) {
  241. pram_ofs = cpm_muram_alloc(SPI_PRAM_SIZE, 64);
  242. out_be16(spi_base, pram_ofs);
  243. }
  244. iounmap(spi_base);
  245. return pram_ofs;
  246. }
  247. int fsl_spi_cpm_init(struct mpc8xxx_spi *mspi)
  248. {
  249. struct device *dev = mspi->dev;
  250. struct device_node *np = dev->of_node;
  251. const u32 *iprop;
  252. int size;
  253. unsigned long bds_ofs;
  254. if (!(mspi->flags & SPI_CPM_MODE))
  255. return 0;
  256. if (!fsl_spi_alloc_dummy_rx())
  257. return -ENOMEM;
  258. if (mspi->flags & SPI_QE) {
  259. iprop = of_get_property(np, "cell-index", &size);
  260. if (iprop && size == sizeof(*iprop))
  261. mspi->subblock = *iprop;
  262. switch (mspi->subblock) {
  263. default:
  264. dev_warn(dev, "cell-index unspecified, assuming SPI1\n");
  265. fallthrough;
  266. case 0:
  267. mspi->subblock = QE_CR_SUBBLOCK_SPI1;
  268. break;
  269. case 1:
  270. mspi->subblock = QE_CR_SUBBLOCK_SPI2;
  271. break;
  272. }
  273. }
  274. if (mspi->flags & SPI_CPM1) {
  275. void *pram;
  276. pram = devm_platform_ioremap_resource(to_platform_device(dev),
  277. 1);
  278. if (IS_ERR(pram))
  279. mspi->pram = NULL;
  280. else
  281. mspi->pram = pram;
  282. } else {
  283. unsigned long pram_ofs = fsl_spi_cpm_get_pram(mspi);
  284. if (IS_ERR_VALUE(pram_ofs))
  285. mspi->pram = NULL;
  286. else
  287. mspi->pram = cpm_muram_addr(pram_ofs);
  288. }
  289. if (mspi->pram == NULL) {
  290. dev_err(dev, "can't allocate spi parameter ram\n");
  291. goto err_pram;
  292. }
  293. bds_ofs = cpm_muram_alloc(sizeof(*mspi->tx_bd) +
  294. sizeof(*mspi->rx_bd), 8);
  295. if (IS_ERR_VALUE(bds_ofs)) {
  296. dev_err(dev, "can't allocate bds\n");
  297. goto err_bds;
  298. }
  299. mspi->dma_dummy_tx = dma_map_single(dev, empty_zero_page, PAGE_SIZE,
  300. DMA_TO_DEVICE);
  301. if (dma_mapping_error(dev, mspi->dma_dummy_tx)) {
  302. dev_err(dev, "unable to map dummy tx buffer\n");
  303. goto err_dummy_tx;
  304. }
  305. mspi->dma_dummy_rx = dma_map_single(dev, fsl_dummy_rx, SPI_MRBLR,
  306. DMA_FROM_DEVICE);
  307. if (dma_mapping_error(dev, mspi->dma_dummy_rx)) {
  308. dev_err(dev, "unable to map dummy rx buffer\n");
  309. goto err_dummy_rx;
  310. }
  311. mspi->tx_bd = cpm_muram_addr(bds_ofs);
  312. mspi->rx_bd = cpm_muram_addr(bds_ofs + sizeof(*mspi->tx_bd));
  313. /* Initialize parameter ram. */
  314. out_be16(&mspi->pram->tbase, cpm_muram_offset(mspi->tx_bd));
  315. out_be16(&mspi->pram->rbase, cpm_muram_offset(mspi->rx_bd));
  316. out_8(&mspi->pram->tfcr, CPMFCR_EB | CPMFCR_GBL);
  317. out_8(&mspi->pram->rfcr, CPMFCR_EB | CPMFCR_GBL);
  318. out_be16(&mspi->pram->mrblr, SPI_MRBLR);
  319. out_be32(&mspi->pram->rstate, 0);
  320. out_be32(&mspi->pram->rdp, 0);
  321. out_be16(&mspi->pram->rbptr, 0);
  322. out_be16(&mspi->pram->rbc, 0);
  323. out_be32(&mspi->pram->rxtmp, 0);
  324. out_be32(&mspi->pram->tstate, 0);
  325. out_be32(&mspi->pram->tdp, 0);
  326. out_be16(&mspi->pram->tbptr, 0);
  327. out_be16(&mspi->pram->tbc, 0);
  328. out_be32(&mspi->pram->txtmp, 0);
  329. return 0;
  330. err_dummy_rx:
  331. dma_unmap_single(dev, mspi->dma_dummy_tx, PAGE_SIZE, DMA_TO_DEVICE);
  332. err_dummy_tx:
  333. cpm_muram_free(bds_ofs);
  334. err_bds:
  335. if (!(mspi->flags & SPI_CPM1))
  336. cpm_muram_free(cpm_muram_offset(mspi->pram));
  337. err_pram:
  338. fsl_spi_free_dummy_rx();
  339. return -ENOMEM;
  340. }
  341. EXPORT_SYMBOL_GPL(fsl_spi_cpm_init);
  342. void fsl_spi_cpm_free(struct mpc8xxx_spi *mspi)
  343. {
  344. struct device *dev = mspi->dev;
  345. if (!(mspi->flags & SPI_CPM_MODE))
  346. return;
  347. dma_unmap_single(dev, mspi->dma_dummy_rx, SPI_MRBLR, DMA_FROM_DEVICE);
  348. dma_unmap_single(dev, mspi->dma_dummy_tx, PAGE_SIZE, DMA_TO_DEVICE);
  349. cpm_muram_free(cpm_muram_offset(mspi->tx_bd));
  350. if (!(mspi->flags & SPI_CPM1))
  351. cpm_muram_free(cpm_muram_offset(mspi->pram));
  352. fsl_spi_free_dummy_rx();
  353. }
  354. EXPORT_SYMBOL_GPL(fsl_spi_cpm_free);
  355. MODULE_LICENSE("GPL");