spi-fsl-cpm.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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 <asm/cpm.h>
  19. #include <asm/qe.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/fsl_devices.h>
  22. #include <linux/kernel.h>
  23. #include <linux/of_address.h>
  24. #include <linux/spi/spi.h>
  25. #include <linux/types.h>
  26. #include "spi-fsl-cpm.h"
  27. #include "spi-fsl-lib.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. if (mspi->flags & SPI_CPM1) {
  53. out_be32(&mspi->pram->rstate, 0);
  54. out_be16(&mspi->pram->rbptr,
  55. in_be16(&mspi->pram->rbase));
  56. out_be32(&mspi->pram->tstate, 0);
  57. out_be16(&mspi->pram->tbptr,
  58. in_be16(&mspi->pram->tbase));
  59. } else {
  60. cpm_command(CPM_SPI_CMD, CPM_CR_INIT_TRX);
  61. }
  62. }
  63. }
  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 (mspi->map_tx_dma) {
  109. void *nonconst_tx = (void *)mspi->tx; /* shut up gcc */
  110. mspi->tx_dma = dma_map_single(dev, nonconst_tx, t->len,
  111. DMA_TO_DEVICE);
  112. if (dma_mapping_error(dev, mspi->tx_dma)) {
  113. dev_err(dev, "unable to map tx dma\n");
  114. return -ENOMEM;
  115. }
  116. } else if (t->tx_buf) {
  117. mspi->tx_dma = t->tx_dma;
  118. }
  119. if (mspi->map_rx_dma) {
  120. mspi->rx_dma = dma_map_single(dev, mspi->rx, t->len,
  121. DMA_FROM_DEVICE);
  122. if (dma_mapping_error(dev, mspi->rx_dma)) {
  123. dev_err(dev, "unable to map rx dma\n");
  124. goto err_rx_dma;
  125. }
  126. } else if (t->rx_buf) {
  127. mspi->rx_dma = t->rx_dma;
  128. }
  129. /* enable rx ints */
  130. mpc8xxx_spi_write_reg(&reg_base->mask, SPIE_RXB);
  131. mspi->xfer_in_progress = t;
  132. mspi->count = t->len;
  133. /* start CPM transfers */
  134. fsl_spi_cpm_bufs_start(mspi);
  135. return 0;
  136. err_rx_dma:
  137. if (mspi->map_tx_dma)
  138. dma_unmap_single(dev, mspi->tx_dma, t->len, DMA_TO_DEVICE);
  139. return -ENOMEM;
  140. }
  141. void fsl_spi_cpm_bufs_complete(struct mpc8xxx_spi *mspi)
  142. {
  143. struct device *dev = mspi->dev;
  144. struct spi_transfer *t = mspi->xfer_in_progress;
  145. if (mspi->map_tx_dma)
  146. dma_unmap_single(dev, mspi->tx_dma, t->len, DMA_TO_DEVICE);
  147. if (mspi->map_rx_dma)
  148. dma_unmap_single(dev, mspi->rx_dma, t->len, DMA_FROM_DEVICE);
  149. mspi->xfer_in_progress = NULL;
  150. }
  151. void fsl_spi_cpm_irq(struct mpc8xxx_spi *mspi, u32 events)
  152. {
  153. u16 len;
  154. struct fsl_spi_reg *reg_base = mspi->reg_base;
  155. dev_dbg(mspi->dev, "%s: bd datlen %d, count %d\n", __func__,
  156. in_be16(&mspi->rx_bd->cbd_datlen), mspi->count);
  157. len = in_be16(&mspi->rx_bd->cbd_datlen);
  158. if (len > mspi->count) {
  159. WARN_ON(1);
  160. len = mspi->count;
  161. }
  162. /* Clear the events */
  163. mpc8xxx_spi_write_reg(&reg_base->event, events);
  164. mspi->count -= len;
  165. if (mspi->count)
  166. fsl_spi_cpm_bufs_start(mspi);
  167. else
  168. complete(&mspi->done);
  169. }
  170. static void *fsl_spi_alloc_dummy_rx(void)
  171. {
  172. mutex_lock(&fsl_dummy_rx_lock);
  173. if (!fsl_dummy_rx)
  174. fsl_dummy_rx = kmalloc(SPI_MRBLR, GFP_KERNEL);
  175. if (fsl_dummy_rx)
  176. fsl_dummy_rx_refcnt++;
  177. mutex_unlock(&fsl_dummy_rx_lock);
  178. return fsl_dummy_rx;
  179. }
  180. static void fsl_spi_free_dummy_rx(void)
  181. {
  182. mutex_lock(&fsl_dummy_rx_lock);
  183. switch (fsl_dummy_rx_refcnt) {
  184. case 0:
  185. WARN_ON(1);
  186. break;
  187. case 1:
  188. kfree(fsl_dummy_rx);
  189. fsl_dummy_rx = NULL;
  190. /* fall through */
  191. default:
  192. fsl_dummy_rx_refcnt--;
  193. break;
  194. }
  195. mutex_unlock(&fsl_dummy_rx_lock);
  196. }
  197. static unsigned long fsl_spi_cpm_get_pram(struct mpc8xxx_spi *mspi)
  198. {
  199. struct device *dev = mspi->dev;
  200. struct device_node *np = dev->of_node;
  201. const u32 *iprop;
  202. int size;
  203. void __iomem *spi_base;
  204. unsigned long pram_ofs = -ENOMEM;
  205. /* Can't use of_address_to_resource(), QE muram isn't at 0. */
  206. iprop = of_get_property(np, "reg", &size);
  207. /* QE with a fixed pram location? */
  208. if (mspi->flags & SPI_QE && iprop && size == sizeof(*iprop) * 4)
  209. return cpm_muram_alloc_fixed(iprop[2], SPI_PRAM_SIZE);
  210. /* QE but with a dynamic pram location? */
  211. if (mspi->flags & SPI_QE) {
  212. pram_ofs = cpm_muram_alloc(SPI_PRAM_SIZE, 64);
  213. qe_issue_cmd(QE_ASSIGN_PAGE_TO_DEVICE, mspi->subblock,
  214. QE_CR_PROTOCOL_UNSPECIFIED, pram_ofs);
  215. return pram_ofs;
  216. }
  217. spi_base = of_iomap(np, 1);
  218. if (spi_base == NULL)
  219. return -EINVAL;
  220. if (mspi->flags & SPI_CPM2) {
  221. pram_ofs = cpm_muram_alloc(SPI_PRAM_SIZE, 64);
  222. out_be16(spi_base, pram_ofs);
  223. } else {
  224. struct spi_pram __iomem *pram = spi_base;
  225. u16 rpbase = in_be16(&pram->rpbase);
  226. /* Microcode relocation patch applied? */
  227. if (rpbase) {
  228. pram_ofs = rpbase;
  229. } else {
  230. pram_ofs = cpm_muram_alloc(SPI_PRAM_SIZE, 64);
  231. out_be16(spi_base, pram_ofs);
  232. }
  233. }
  234. iounmap(spi_base);
  235. return pram_ofs;
  236. }
  237. int fsl_spi_cpm_init(struct mpc8xxx_spi *mspi)
  238. {
  239. struct device *dev = mspi->dev;
  240. struct device_node *np = dev->of_node;
  241. const u32 *iprop;
  242. int size;
  243. unsigned long pram_ofs;
  244. unsigned long bds_ofs;
  245. if (!(mspi->flags & SPI_CPM_MODE))
  246. return 0;
  247. if (!fsl_spi_alloc_dummy_rx())
  248. return -ENOMEM;
  249. if (mspi->flags & SPI_QE) {
  250. iprop = of_get_property(np, "cell-index", &size);
  251. if (iprop && size == sizeof(*iprop))
  252. mspi->subblock = *iprop;
  253. switch (mspi->subblock) {
  254. default:
  255. dev_warn(dev, "cell-index unspecified, assuming SPI1\n");
  256. /* fall through */
  257. case 0:
  258. mspi->subblock = QE_CR_SUBBLOCK_SPI1;
  259. break;
  260. case 1:
  261. mspi->subblock = QE_CR_SUBBLOCK_SPI2;
  262. break;
  263. }
  264. }
  265. pram_ofs = fsl_spi_cpm_get_pram(mspi);
  266. if (IS_ERR_VALUE(pram_ofs)) {
  267. dev_err(dev, "can't allocate spi parameter ram\n");
  268. goto err_pram;
  269. }
  270. bds_ofs = cpm_muram_alloc(sizeof(*mspi->tx_bd) +
  271. sizeof(*mspi->rx_bd), 8);
  272. if (IS_ERR_VALUE(bds_ofs)) {
  273. dev_err(dev, "can't allocate bds\n");
  274. goto err_bds;
  275. }
  276. mspi->dma_dummy_tx = dma_map_single(dev, empty_zero_page, PAGE_SIZE,
  277. DMA_TO_DEVICE);
  278. if (dma_mapping_error(dev, mspi->dma_dummy_tx)) {
  279. dev_err(dev, "unable to map dummy tx buffer\n");
  280. goto err_dummy_tx;
  281. }
  282. mspi->dma_dummy_rx = dma_map_single(dev, fsl_dummy_rx, SPI_MRBLR,
  283. DMA_FROM_DEVICE);
  284. if (dma_mapping_error(dev, mspi->dma_dummy_rx)) {
  285. dev_err(dev, "unable to map dummy rx buffer\n");
  286. goto err_dummy_rx;
  287. }
  288. mspi->pram = cpm_muram_addr(pram_ofs);
  289. mspi->tx_bd = cpm_muram_addr(bds_ofs);
  290. mspi->rx_bd = cpm_muram_addr(bds_ofs + sizeof(*mspi->tx_bd));
  291. /* Initialize parameter ram. */
  292. out_be16(&mspi->pram->tbase, cpm_muram_offset(mspi->tx_bd));
  293. out_be16(&mspi->pram->rbase, cpm_muram_offset(mspi->rx_bd));
  294. out_8(&mspi->pram->tfcr, CPMFCR_EB | CPMFCR_GBL);
  295. out_8(&mspi->pram->rfcr, CPMFCR_EB | CPMFCR_GBL);
  296. out_be16(&mspi->pram->mrblr, SPI_MRBLR);
  297. out_be32(&mspi->pram->rstate, 0);
  298. out_be32(&mspi->pram->rdp, 0);
  299. out_be16(&mspi->pram->rbptr, 0);
  300. out_be16(&mspi->pram->rbc, 0);
  301. out_be32(&mspi->pram->rxtmp, 0);
  302. out_be32(&mspi->pram->tstate, 0);
  303. out_be32(&mspi->pram->tdp, 0);
  304. out_be16(&mspi->pram->tbptr, 0);
  305. out_be16(&mspi->pram->tbc, 0);
  306. out_be32(&mspi->pram->txtmp, 0);
  307. return 0;
  308. err_dummy_rx:
  309. dma_unmap_single(dev, mspi->dma_dummy_tx, PAGE_SIZE, DMA_TO_DEVICE);
  310. err_dummy_tx:
  311. cpm_muram_free(bds_ofs);
  312. err_bds:
  313. cpm_muram_free(pram_ofs);
  314. err_pram:
  315. fsl_spi_free_dummy_rx();
  316. return -ENOMEM;
  317. }
  318. void fsl_spi_cpm_free(struct mpc8xxx_spi *mspi)
  319. {
  320. struct device *dev = mspi->dev;
  321. if (!(mspi->flags & SPI_CPM_MODE))
  322. return;
  323. dma_unmap_single(dev, mspi->dma_dummy_rx, SPI_MRBLR, DMA_FROM_DEVICE);
  324. dma_unmap_single(dev, mspi->dma_dummy_tx, PAGE_SIZE, DMA_TO_DEVICE);
  325. cpm_muram_free(cpm_muram_offset(mspi->tx_bd));
  326. cpm_muram_free(cpm_muram_offset(mspi->pram));
  327. fsl_spi_free_dummy_rx();
  328. }