spi-fsl-cpm.c 9.6 KB

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