spi-fsl-cpm.c 10.0 KB

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