spi-fsl-cpm.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. * Freescale SPI controller driver cpm functions.
  3. *
  4. * Maintainer: Kumar Gala
  5. *
  6. * Copyright (C) 2006 Polycom, Inc.
  7. * Copyright 2010 Freescale Semiconductor, Inc.
  8. *
  9. * CPM SPI and QE buffer descriptors mode support:
  10. * Copyright (c) 2009 MontaVista Software, Inc.
  11. * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
  12. *
  13. * This program is free software; you can redistribute it and/or modify it
  14. * under the terms of the GNU General Public License as published by the
  15. * Free Software Foundation; either version 2 of the License, or (at your
  16. * option) any later version.
  17. */
  18. #include <linux/types.h>
  19. #include <linux/kernel.h>
  20. #include <linux/spi/spi.h>
  21. #include <linux/fsl_devices.h>
  22. #include <linux/dma-mapping.h>
  23. #include <asm/cpm.h>
  24. #include <asm/qe.h>
  25. #include "spi-fsl-lib.h"
  26. #include "spi-fsl-cpm.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. cpm_command(CPM_SPI_CMD, CPM_CR_INIT_TRX);
  52. if (mspi->flags & SPI_CPM1) {
  53. out_be16(&mspi->pram->rbptr,
  54. in_be16(&mspi->pram->rbase));
  55. out_be16(&mspi->pram->tbptr,
  56. in_be16(&mspi->pram->tbase));
  57. }
  58. }
  59. }
  60. static void fsl_spi_cpm_bufs_start(struct mpc8xxx_spi *mspi)
  61. {
  62. struct cpm_buf_desc __iomem *tx_bd = mspi->tx_bd;
  63. struct cpm_buf_desc __iomem *rx_bd = mspi->rx_bd;
  64. unsigned int xfer_len = min(mspi->count, SPI_MRBLR);
  65. unsigned int xfer_ofs;
  66. struct fsl_spi_reg *reg_base = mspi->reg_base;
  67. xfer_ofs = mspi->xfer_in_progress->len - mspi->count;
  68. if (mspi->rx_dma == mspi->dma_dummy_rx)
  69. out_be32(&rx_bd->cbd_bufaddr, mspi->rx_dma);
  70. else
  71. out_be32(&rx_bd->cbd_bufaddr, mspi->rx_dma + xfer_ofs);
  72. out_be16(&rx_bd->cbd_datlen, 0);
  73. out_be16(&rx_bd->cbd_sc, BD_SC_EMPTY | BD_SC_INTRPT | BD_SC_WRAP);
  74. if (mspi->tx_dma == mspi->dma_dummy_tx)
  75. out_be32(&tx_bd->cbd_bufaddr, mspi->tx_dma);
  76. else
  77. out_be32(&tx_bd->cbd_bufaddr, mspi->tx_dma + xfer_ofs);
  78. out_be16(&tx_bd->cbd_datlen, xfer_len);
  79. out_be16(&tx_bd->cbd_sc, BD_SC_READY | BD_SC_INTRPT | BD_SC_WRAP |
  80. BD_SC_LAST);
  81. /* start transfer */
  82. mpc8xxx_spi_write_reg(&reg_base->command, SPCOM_STR);
  83. }
  84. int fsl_spi_cpm_bufs(struct mpc8xxx_spi *mspi,
  85. struct spi_transfer *t, bool is_dma_mapped)
  86. {
  87. struct device *dev = mspi->dev;
  88. struct fsl_spi_reg *reg_base = mspi->reg_base;
  89. if (is_dma_mapped) {
  90. mspi->map_tx_dma = 0;
  91. mspi->map_rx_dma = 0;
  92. } else {
  93. mspi->map_tx_dma = 1;
  94. mspi->map_rx_dma = 1;
  95. }
  96. if (!t->tx_buf) {
  97. mspi->tx_dma = mspi->dma_dummy_tx;
  98. mspi->map_tx_dma = 0;
  99. }
  100. if (!t->rx_buf) {
  101. mspi->rx_dma = mspi->dma_dummy_rx;
  102. mspi->map_rx_dma = 0;
  103. }
  104. if (mspi->map_tx_dma) {
  105. void *nonconst_tx = (void *)mspi->tx; /* shut up gcc */
  106. mspi->tx_dma = dma_map_single(dev, nonconst_tx, t->len,
  107. DMA_TO_DEVICE);
  108. if (dma_mapping_error(dev, mspi->tx_dma)) {
  109. dev_err(dev, "unable to map tx dma\n");
  110. return -ENOMEM;
  111. }
  112. } else if (t->tx_buf) {
  113. mspi->tx_dma = t->tx_dma;
  114. }
  115. if (mspi->map_rx_dma) {
  116. mspi->rx_dma = dma_map_single(dev, mspi->rx, t->len,
  117. DMA_FROM_DEVICE);
  118. if (dma_mapping_error(dev, mspi->rx_dma)) {
  119. dev_err(dev, "unable to map rx dma\n");
  120. goto err_rx_dma;
  121. }
  122. } else if (t->rx_buf) {
  123. mspi->rx_dma = t->rx_dma;
  124. }
  125. /* enable rx ints */
  126. mpc8xxx_spi_write_reg(&reg_base->mask, SPIE_RXB);
  127. mspi->xfer_in_progress = t;
  128. mspi->count = t->len;
  129. /* start CPM transfers */
  130. fsl_spi_cpm_bufs_start(mspi);
  131. return 0;
  132. err_rx_dma:
  133. if (mspi->map_tx_dma)
  134. dma_unmap_single(dev, mspi->tx_dma, t->len, DMA_TO_DEVICE);
  135. return -ENOMEM;
  136. }
  137. void fsl_spi_cpm_bufs_complete(struct mpc8xxx_spi *mspi)
  138. {
  139. struct device *dev = mspi->dev;
  140. struct spi_transfer *t = mspi->xfer_in_progress;
  141. if (mspi->map_tx_dma)
  142. dma_unmap_single(dev, mspi->tx_dma, t->len, DMA_TO_DEVICE);
  143. if (mspi->map_rx_dma)
  144. dma_unmap_single(dev, mspi->rx_dma, t->len, DMA_FROM_DEVICE);
  145. mspi->xfer_in_progress = NULL;
  146. }
  147. void fsl_spi_cpm_irq(struct mpc8xxx_spi *mspi, u32 events)
  148. {
  149. u16 len;
  150. struct fsl_spi_reg *reg_base = mspi->reg_base;
  151. dev_dbg(mspi->dev, "%s: bd datlen %d, count %d\n", __func__,
  152. in_be16(&mspi->rx_bd->cbd_datlen), mspi->count);
  153. len = in_be16(&mspi->rx_bd->cbd_datlen);
  154. if (len > mspi->count) {
  155. WARN_ON(1);
  156. len = mspi->count;
  157. }
  158. /* Clear the events */
  159. mpc8xxx_spi_write_reg(&reg_base->event, events);
  160. mspi->count -= len;
  161. if (mspi->count)
  162. fsl_spi_cpm_bufs_start(mspi);
  163. else
  164. complete(&mspi->done);
  165. }
  166. static void *fsl_spi_alloc_dummy_rx(void)
  167. {
  168. mutex_lock(&fsl_dummy_rx_lock);
  169. if (!fsl_dummy_rx)
  170. fsl_dummy_rx = kmalloc(SPI_MRBLR, GFP_KERNEL);
  171. if (fsl_dummy_rx)
  172. fsl_dummy_rx_refcnt++;
  173. mutex_unlock(&fsl_dummy_rx_lock);
  174. return fsl_dummy_rx;
  175. }
  176. static void fsl_spi_free_dummy_rx(void)
  177. {
  178. mutex_lock(&fsl_dummy_rx_lock);
  179. switch (fsl_dummy_rx_refcnt) {
  180. case 0:
  181. WARN_ON(1);
  182. break;
  183. case 1:
  184. kfree(fsl_dummy_rx);
  185. fsl_dummy_rx = NULL;
  186. /* fall through */
  187. default:
  188. fsl_dummy_rx_refcnt--;
  189. break;
  190. }
  191. mutex_unlock(&fsl_dummy_rx_lock);
  192. }
  193. static unsigned long fsl_spi_cpm_get_pram(struct mpc8xxx_spi *mspi)
  194. {
  195. struct device *dev = mspi->dev;
  196. struct device_node *np = dev->of_node;
  197. const u32 *iprop;
  198. int size;
  199. void __iomem *spi_base;
  200. unsigned long pram_ofs = -ENOMEM;
  201. /* Can't use of_address_to_resource(), QE muram isn't at 0. */
  202. iprop = of_get_property(np, "reg", &size);
  203. /* QE with a fixed pram location? */
  204. if (mspi->flags & SPI_QE && iprop && size == sizeof(*iprop) * 4)
  205. return cpm_muram_alloc_fixed(iprop[2], SPI_PRAM_SIZE);
  206. /* QE but with a dynamic pram location? */
  207. if (mspi->flags & SPI_QE) {
  208. pram_ofs = cpm_muram_alloc(SPI_PRAM_SIZE, 64);
  209. qe_issue_cmd(QE_ASSIGN_PAGE_TO_DEVICE, mspi->subblock,
  210. QE_CR_PROTOCOL_UNSPECIFIED, pram_ofs);
  211. return pram_ofs;
  212. }
  213. spi_base = of_iomap(np, 1);
  214. if (spi_base == NULL)
  215. return -EINVAL;
  216. if (mspi->flags & SPI_CPM2) {
  217. pram_ofs = cpm_muram_alloc(SPI_PRAM_SIZE, 64);
  218. out_be16(spi_base, pram_ofs);
  219. } else {
  220. struct spi_pram __iomem *pram = spi_base;
  221. u16 rpbase = in_be16(&pram->rpbase);
  222. /* Microcode relocation patch applied? */
  223. if (rpbase) {
  224. pram_ofs = rpbase;
  225. } else {
  226. pram_ofs = cpm_muram_alloc(SPI_PRAM_SIZE, 64);
  227. out_be16(spi_base, pram_ofs);
  228. }
  229. }
  230. iounmap(spi_base);
  231. return pram_ofs;
  232. }
  233. int fsl_spi_cpm_init(struct mpc8xxx_spi *mspi)
  234. {
  235. struct device *dev = mspi->dev;
  236. struct device_node *np = dev->of_node;
  237. const u32 *iprop;
  238. int size;
  239. unsigned long pram_ofs;
  240. unsigned long bds_ofs;
  241. if (!(mspi->flags & SPI_CPM_MODE))
  242. return 0;
  243. if (!fsl_spi_alloc_dummy_rx())
  244. return -ENOMEM;
  245. if (mspi->flags & SPI_QE) {
  246. iprop = of_get_property(np, "cell-index", &size);
  247. if (iprop && size == sizeof(*iprop))
  248. mspi->subblock = *iprop;
  249. switch (mspi->subblock) {
  250. default:
  251. dev_warn(dev, "cell-index unspecified, assuming SPI1");
  252. /* fall through */
  253. case 0:
  254. mspi->subblock = QE_CR_SUBBLOCK_SPI1;
  255. break;
  256. case 1:
  257. mspi->subblock = QE_CR_SUBBLOCK_SPI2;
  258. break;
  259. }
  260. }
  261. pram_ofs = fsl_spi_cpm_get_pram(mspi);
  262. if (IS_ERR_VALUE(pram_ofs)) {
  263. dev_err(dev, "can't allocate spi parameter ram\n");
  264. goto err_pram;
  265. }
  266. bds_ofs = cpm_muram_alloc(sizeof(*mspi->tx_bd) +
  267. sizeof(*mspi->rx_bd), 8);
  268. if (IS_ERR_VALUE(bds_ofs)) {
  269. dev_err(dev, "can't allocate bds\n");
  270. goto err_bds;
  271. }
  272. mspi->dma_dummy_tx = dma_map_single(dev, empty_zero_page, PAGE_SIZE,
  273. DMA_TO_DEVICE);
  274. if (dma_mapping_error(dev, mspi->dma_dummy_tx)) {
  275. dev_err(dev, "unable to map dummy tx buffer\n");
  276. goto err_dummy_tx;
  277. }
  278. mspi->dma_dummy_rx = dma_map_single(dev, fsl_dummy_rx, SPI_MRBLR,
  279. DMA_FROM_DEVICE);
  280. if (dma_mapping_error(dev, mspi->dma_dummy_rx)) {
  281. dev_err(dev, "unable to map dummy rx buffer\n");
  282. goto err_dummy_rx;
  283. }
  284. mspi->pram = cpm_muram_addr(pram_ofs);
  285. mspi->tx_bd = cpm_muram_addr(bds_ofs);
  286. mspi->rx_bd = cpm_muram_addr(bds_ofs + sizeof(*mspi->tx_bd));
  287. /* Initialize parameter ram. */
  288. out_be16(&mspi->pram->tbase, cpm_muram_offset(mspi->tx_bd));
  289. out_be16(&mspi->pram->rbase, cpm_muram_offset(mspi->rx_bd));
  290. out_8(&mspi->pram->tfcr, CPMFCR_EB | CPMFCR_GBL);
  291. out_8(&mspi->pram->rfcr, CPMFCR_EB | CPMFCR_GBL);
  292. out_be16(&mspi->pram->mrblr, SPI_MRBLR);
  293. out_be32(&mspi->pram->rstate, 0);
  294. out_be32(&mspi->pram->rdp, 0);
  295. out_be16(&mspi->pram->rbptr, 0);
  296. out_be16(&mspi->pram->rbc, 0);
  297. out_be32(&mspi->pram->rxtmp, 0);
  298. out_be32(&mspi->pram->tstate, 0);
  299. out_be32(&mspi->pram->tdp, 0);
  300. out_be16(&mspi->pram->tbptr, 0);
  301. out_be16(&mspi->pram->tbc, 0);
  302. out_be32(&mspi->pram->txtmp, 0);
  303. return 0;
  304. err_dummy_rx:
  305. dma_unmap_single(dev, mspi->dma_dummy_tx, PAGE_SIZE, DMA_TO_DEVICE);
  306. err_dummy_tx:
  307. cpm_muram_free(bds_ofs);
  308. err_bds:
  309. cpm_muram_free(pram_ofs);
  310. err_pram:
  311. fsl_spi_free_dummy_rx();
  312. return -ENOMEM;
  313. }
  314. void fsl_spi_cpm_free(struct mpc8xxx_spi *mspi)
  315. {
  316. struct device *dev = mspi->dev;
  317. if (!(mspi->flags & SPI_CPM_MODE))
  318. return;
  319. dma_unmap_single(dev, mspi->dma_dummy_rx, SPI_MRBLR, DMA_FROM_DEVICE);
  320. dma_unmap_single(dev, mspi->dma_dummy_tx, PAGE_SIZE, DMA_TO_DEVICE);
  321. cpm_muram_free(cpm_muram_offset(mspi->tx_bd));
  322. cpm_muram_free(cpm_muram_offset(mspi->pram));
  323. fsl_spi_free_dummy_rx();
  324. }