xref: /openbmc/linux/drivers/spi/spi-fsl-cpm.c (revision ddaec4e4)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2e8beacbbSAndreas Larsson /*
3e8beacbbSAndreas Larsson  * Freescale SPI controller driver cpm functions.
4e8beacbbSAndreas Larsson  *
5e8beacbbSAndreas Larsson  * Maintainer: Kumar Gala
6e8beacbbSAndreas Larsson  *
7e8beacbbSAndreas Larsson  * Copyright (C) 2006 Polycom, Inc.
8e8beacbbSAndreas Larsson  * Copyright 2010 Freescale Semiconductor, Inc.
9e8beacbbSAndreas Larsson  *
10e8beacbbSAndreas Larsson  * CPM SPI and QE buffer descriptors mode support:
11e8beacbbSAndreas Larsson  * Copyright (c) 2009  MontaVista Software, Inc.
12e8beacbbSAndreas Larsson  * Author: Anton Vorontsov <avorontsov@ru.mvista.com>
13e8beacbbSAndreas Larsson  */
14e8beacbbSAndreas Larsson #include <asm/cpm.h>
157aa1aa6eSZhao Qiang #include <soc/fsl/qe/qe.h>
16a3108360SXiubo Li #include <linux/dma-mapping.h>
17a3108360SXiubo Li #include <linux/fsl_devices.h>
18a3108360SXiubo Li #include <linux/kernel.h>
1938455d7aSEsben Haabendal #include <linux/module.h>
20a3108360SXiubo Li #include <linux/of_address.h>
21a3108360SXiubo Li #include <linux/spi/spi.h>
22a3108360SXiubo Li #include <linux/types.h>
23575bec53SChristophe Leroy #include <linux/platform_device.h>
24fc96ec82SChristophe Leroy #include <linux/byteorder/generic.h>
25e8beacbbSAndreas Larsson 
26e8beacbbSAndreas Larsson #include "spi-fsl-cpm.h"
27a3108360SXiubo Li #include "spi-fsl-lib.h"
28e8beacbbSAndreas Larsson #include "spi-fsl-spi.h"
29e8beacbbSAndreas Larsson 
30e8beacbbSAndreas Larsson /* CPM1 and CPM2 are mutually exclusive. */
31e8beacbbSAndreas Larsson #ifdef CONFIG_CPM1
32e8beacbbSAndreas Larsson #include <asm/cpm1.h>
33e8beacbbSAndreas Larsson #define CPM_SPI_CMD mk_cr_cmd(CPM_CR_CH_SPI, 0)
34e8beacbbSAndreas Larsson #else
35e8beacbbSAndreas Larsson #include <asm/cpm2.h>
36e8beacbbSAndreas Larsson #define CPM_SPI_CMD mk_cr_cmd(CPM_CR_SPI_PAGE, CPM_CR_SPI_SBLOCK, 0, 0)
37e8beacbbSAndreas Larsson #endif
38e8beacbbSAndreas Larsson 
39e8beacbbSAndreas Larsson #define	SPIE_TXB	0x00000200	/* Last char is written to tx fifo */
40e8beacbbSAndreas Larsson #define	SPIE_RXB	0x00000100	/* Last char is written to rx buf */
41e8beacbbSAndreas Larsson 
42e8beacbbSAndreas Larsson /* SPCOM register values */
43e8beacbbSAndreas Larsson #define	SPCOM_STR	(1 << 23)	/* Start transmit */
44e8beacbbSAndreas Larsson 
45e8beacbbSAndreas Larsson #define	SPI_PRAM_SIZE	0x100
46e8beacbbSAndreas Larsson #define	SPI_MRBLR	((unsigned int)PAGE_SIZE)
47e8beacbbSAndreas Larsson 
48e8beacbbSAndreas Larsson static void *fsl_dummy_rx;
49e8beacbbSAndreas Larsson static DEFINE_MUTEX(fsl_dummy_rx_lock);
50e8beacbbSAndreas Larsson static int fsl_dummy_rx_refcnt;
51e8beacbbSAndreas Larsson 
fsl_spi_cpm_reinit_txrx(struct mpc8xxx_spi * mspi)52e8beacbbSAndreas Larsson void fsl_spi_cpm_reinit_txrx(struct mpc8xxx_spi *mspi)
53e8beacbbSAndreas Larsson {
54e8beacbbSAndreas Larsson 	if (mspi->flags & SPI_QE) {
55e8beacbbSAndreas Larsson 		qe_issue_cmd(QE_INIT_TX_RX, mspi->subblock,
56e8beacbbSAndreas Larsson 			     QE_CR_PROTOCOL_UNSPECIFIED, 0);
57e8beacbbSAndreas Larsson 	} else {
58e8beacbbSAndreas Larsson 		if (mspi->flags & SPI_CPM1) {
59*ddaec4e4SChristophe Leroy 			iowrite32be(0, &mspi->pram->rstate);
60*ddaec4e4SChristophe Leroy 			iowrite16be(ioread16be(&mspi->pram->rbase),
61*ddaec4e4SChristophe Leroy 				    &mspi->pram->rbptr);
62*ddaec4e4SChristophe Leroy 			iowrite32be(0, &mspi->pram->tstate);
63*ddaec4e4SChristophe Leroy 			iowrite16be(ioread16be(&mspi->pram->tbase),
64*ddaec4e4SChristophe Leroy 				    &mspi->pram->tbptr);
65194ed900SChristophe Leroy 		} else {
66194ed900SChristophe Leroy 			cpm_command(CPM_SPI_CMD, CPM_CR_INIT_TRX);
67e8beacbbSAndreas Larsson 		}
68e8beacbbSAndreas Larsson 	}
69e8beacbbSAndreas Larsson }
7038455d7aSEsben Haabendal EXPORT_SYMBOL_GPL(fsl_spi_cpm_reinit_txrx);
71e8beacbbSAndreas Larsson 
fsl_spi_cpm_bufs_start(struct mpc8xxx_spi * mspi)72e8beacbbSAndreas Larsson static void fsl_spi_cpm_bufs_start(struct mpc8xxx_spi *mspi)
73e8beacbbSAndreas Larsson {
74e8beacbbSAndreas Larsson 	struct cpm_buf_desc __iomem *tx_bd = mspi->tx_bd;
75e8beacbbSAndreas Larsson 	struct cpm_buf_desc __iomem *rx_bd = mspi->rx_bd;
76e8beacbbSAndreas Larsson 	unsigned int xfer_len = min(mspi->count, SPI_MRBLR);
77e8beacbbSAndreas Larsson 	unsigned int xfer_ofs;
78*ddaec4e4SChristophe Leroy 	struct fsl_spi_reg __iomem *reg_base = mspi->reg_base;
79e8beacbbSAndreas Larsson 
80e8beacbbSAndreas Larsson 	xfer_ofs = mspi->xfer_in_progress->len - mspi->count;
81e8beacbbSAndreas Larsson 
82e8beacbbSAndreas Larsson 	if (mspi->rx_dma == mspi->dma_dummy_rx)
83*ddaec4e4SChristophe Leroy 		iowrite32be(mspi->rx_dma, &rx_bd->cbd_bufaddr);
84e8beacbbSAndreas Larsson 	else
85*ddaec4e4SChristophe Leroy 		iowrite32be(mspi->rx_dma + xfer_ofs, &rx_bd->cbd_bufaddr);
86*ddaec4e4SChristophe Leroy 	iowrite16be(0, &rx_bd->cbd_datlen);
87*ddaec4e4SChristophe Leroy 	iowrite16be(BD_SC_EMPTY | BD_SC_INTRPT | BD_SC_WRAP, &rx_bd->cbd_sc);
88e8beacbbSAndreas Larsson 
89e8beacbbSAndreas Larsson 	if (mspi->tx_dma == mspi->dma_dummy_tx)
90*ddaec4e4SChristophe Leroy 		iowrite32be(mspi->tx_dma, &tx_bd->cbd_bufaddr);
91e8beacbbSAndreas Larsson 	else
92*ddaec4e4SChristophe Leroy 		iowrite32be(mspi->tx_dma + xfer_ofs, &tx_bd->cbd_bufaddr);
93*ddaec4e4SChristophe Leroy 	iowrite16be(xfer_len, &tx_bd->cbd_datlen);
94*ddaec4e4SChristophe Leroy 	iowrite16be(BD_SC_READY | BD_SC_INTRPT | BD_SC_WRAP | BD_SC_LAST,
95*ddaec4e4SChristophe Leroy 		    &tx_bd->cbd_sc);
96e8beacbbSAndreas Larsson 
97e8beacbbSAndreas Larsson 	/* start transfer */
98e8beacbbSAndreas Larsson 	mpc8xxx_spi_write_reg(&reg_base->command, SPCOM_STR);
99e8beacbbSAndreas Larsson }
100e8beacbbSAndreas Larsson 
fsl_spi_cpm_bufs(struct mpc8xxx_spi * mspi,struct spi_transfer * t,bool is_dma_mapped)101e8beacbbSAndreas Larsson int fsl_spi_cpm_bufs(struct mpc8xxx_spi *mspi,
102e8beacbbSAndreas Larsson 		     struct spi_transfer *t, bool is_dma_mapped)
103e8beacbbSAndreas Larsson {
104e8beacbbSAndreas Larsson 	struct device *dev = mspi->dev;
105*ddaec4e4SChristophe Leroy 	struct fsl_spi_reg __iomem *reg_base = mspi->reg_base;
106e8beacbbSAndreas Larsson 
107e8beacbbSAndreas Larsson 	if (is_dma_mapped) {
108e8beacbbSAndreas Larsson 		mspi->map_tx_dma = 0;
109e8beacbbSAndreas Larsson 		mspi->map_rx_dma = 0;
110e8beacbbSAndreas Larsson 	} else {
111e8beacbbSAndreas Larsson 		mspi->map_tx_dma = 1;
112e8beacbbSAndreas Larsson 		mspi->map_rx_dma = 1;
113e8beacbbSAndreas Larsson 	}
114e8beacbbSAndreas Larsson 
115e8beacbbSAndreas Larsson 	if (!t->tx_buf) {
116e8beacbbSAndreas Larsson 		mspi->tx_dma = mspi->dma_dummy_tx;
117e8beacbbSAndreas Larsson 		mspi->map_tx_dma = 0;
118e8beacbbSAndreas Larsson 	}
119e8beacbbSAndreas Larsson 
120e8beacbbSAndreas Larsson 	if (!t->rx_buf) {
121e8beacbbSAndreas Larsson 		mspi->rx_dma = mspi->dma_dummy_rx;
122e8beacbbSAndreas Larsson 		mspi->map_rx_dma = 0;
123e8beacbbSAndreas Larsson 	}
124fc96ec82SChristophe Leroy 	if (t->bits_per_word == 16 && t->tx_buf) {
125fc96ec82SChristophe Leroy 		const u16 *src = t->tx_buf;
126*ddaec4e4SChristophe Leroy 		__le16 *dst;
127fc96ec82SChristophe Leroy 		int i;
128fc96ec82SChristophe Leroy 
129fc96ec82SChristophe Leroy 		dst = kmalloc(t->len, GFP_KERNEL);
130fc96ec82SChristophe Leroy 		if (!dst)
131fc96ec82SChristophe Leroy 			return -ENOMEM;
132fc96ec82SChristophe Leroy 
133fc96ec82SChristophe Leroy 		for (i = 0; i < t->len >> 1; i++)
134fc96ec82SChristophe Leroy 			dst[i] = cpu_to_le16p(src + i);
135fc96ec82SChristophe Leroy 
136fc96ec82SChristophe Leroy 		mspi->tx = dst;
137fc96ec82SChristophe Leroy 		mspi->map_tx_dma = 1;
138fc96ec82SChristophe Leroy 	}
139e8beacbbSAndreas Larsson 
140e8beacbbSAndreas Larsson 	if (mspi->map_tx_dma) {
141e8beacbbSAndreas Larsson 		void *nonconst_tx = (void *)mspi->tx; /* shut up gcc */
142e8beacbbSAndreas Larsson 
143e8beacbbSAndreas Larsson 		mspi->tx_dma = dma_map_single(dev, nonconst_tx, t->len,
144e8beacbbSAndreas Larsson 					      DMA_TO_DEVICE);
145e8beacbbSAndreas Larsson 		if (dma_mapping_error(dev, mspi->tx_dma)) {
146e8beacbbSAndreas Larsson 			dev_err(dev, "unable to map tx dma\n");
147e8beacbbSAndreas Larsson 			return -ENOMEM;
148e8beacbbSAndreas Larsson 		}
149e8beacbbSAndreas Larsson 	} else if (t->tx_buf) {
150e8beacbbSAndreas Larsson 		mspi->tx_dma = t->tx_dma;
151e8beacbbSAndreas Larsson 	}
152e8beacbbSAndreas Larsson 
153e8beacbbSAndreas Larsson 	if (mspi->map_rx_dma) {
154e8beacbbSAndreas Larsson 		mspi->rx_dma = dma_map_single(dev, mspi->rx, t->len,
155e8beacbbSAndreas Larsson 					      DMA_FROM_DEVICE);
156e8beacbbSAndreas Larsson 		if (dma_mapping_error(dev, mspi->rx_dma)) {
157e8beacbbSAndreas Larsson 			dev_err(dev, "unable to map rx dma\n");
158e8beacbbSAndreas Larsson 			goto err_rx_dma;
159e8beacbbSAndreas Larsson 		}
160e8beacbbSAndreas Larsson 	} else if (t->rx_buf) {
161e8beacbbSAndreas Larsson 		mspi->rx_dma = t->rx_dma;
162e8beacbbSAndreas Larsson 	}
163e8beacbbSAndreas Larsson 
164e8beacbbSAndreas Larsson 	/* enable rx ints */
165e8beacbbSAndreas Larsson 	mpc8xxx_spi_write_reg(&reg_base->mask, SPIE_RXB);
166e8beacbbSAndreas Larsson 
167e8beacbbSAndreas Larsson 	mspi->xfer_in_progress = t;
168e8beacbbSAndreas Larsson 	mspi->count = t->len;
169e8beacbbSAndreas Larsson 
170e8beacbbSAndreas Larsson 	/* start CPM transfers */
171e8beacbbSAndreas Larsson 	fsl_spi_cpm_bufs_start(mspi);
172e8beacbbSAndreas Larsson 
173e8beacbbSAndreas Larsson 	return 0;
174e8beacbbSAndreas Larsson 
175e8beacbbSAndreas Larsson err_rx_dma:
176e8beacbbSAndreas Larsson 	if (mspi->map_tx_dma)
177e8beacbbSAndreas Larsson 		dma_unmap_single(dev, mspi->tx_dma, t->len, DMA_TO_DEVICE);
178e8beacbbSAndreas Larsson 	return -ENOMEM;
179e8beacbbSAndreas Larsson }
18038455d7aSEsben Haabendal EXPORT_SYMBOL_GPL(fsl_spi_cpm_bufs);
181e8beacbbSAndreas Larsson 
fsl_spi_cpm_bufs_complete(struct mpc8xxx_spi * mspi)182e8beacbbSAndreas Larsson void fsl_spi_cpm_bufs_complete(struct mpc8xxx_spi *mspi)
183e8beacbbSAndreas Larsson {
184e8beacbbSAndreas Larsson 	struct device *dev = mspi->dev;
185e8beacbbSAndreas Larsson 	struct spi_transfer *t = mspi->xfer_in_progress;
186e8beacbbSAndreas Larsson 
187e8beacbbSAndreas Larsson 	if (mspi->map_tx_dma)
188e8beacbbSAndreas Larsson 		dma_unmap_single(dev, mspi->tx_dma, t->len, DMA_TO_DEVICE);
189e8beacbbSAndreas Larsson 	if (mspi->map_rx_dma)
190e8beacbbSAndreas Larsson 		dma_unmap_single(dev, mspi->rx_dma, t->len, DMA_FROM_DEVICE);
191e8beacbbSAndreas Larsson 	mspi->xfer_in_progress = NULL;
192fc96ec82SChristophe Leroy 
193fc96ec82SChristophe Leroy 	if (t->bits_per_word == 16 && t->rx_buf) {
194fc96ec82SChristophe Leroy 		int i;
195fc96ec82SChristophe Leroy 
196fc96ec82SChristophe Leroy 		for (i = 0; i < t->len; i += 2)
197fc96ec82SChristophe Leroy 			le16_to_cpus(t->rx_buf + i);
198fc96ec82SChristophe Leroy 	}
199e8beacbbSAndreas Larsson }
20038455d7aSEsben Haabendal EXPORT_SYMBOL_GPL(fsl_spi_cpm_bufs_complete);
201e8beacbbSAndreas Larsson 
fsl_spi_cpm_irq(struct mpc8xxx_spi * mspi,u32 events)202e8beacbbSAndreas Larsson void fsl_spi_cpm_irq(struct mpc8xxx_spi *mspi, u32 events)
203e8beacbbSAndreas Larsson {
204e8beacbbSAndreas Larsson 	u16 len;
205*ddaec4e4SChristophe Leroy 	struct fsl_spi_reg __iomem *reg_base = mspi->reg_base;
206e8beacbbSAndreas Larsson 
207e8beacbbSAndreas Larsson 	dev_dbg(mspi->dev, "%s: bd datlen %d, count %d\n", __func__,
208*ddaec4e4SChristophe Leroy 		ioread16be(&mspi->rx_bd->cbd_datlen), mspi->count);
209e8beacbbSAndreas Larsson 
210*ddaec4e4SChristophe Leroy 	len = ioread16be(&mspi->rx_bd->cbd_datlen);
211e8beacbbSAndreas Larsson 	if (len > mspi->count) {
212e8beacbbSAndreas Larsson 		WARN_ON(1);
213e8beacbbSAndreas Larsson 		len = mspi->count;
214e8beacbbSAndreas Larsson 	}
215e8beacbbSAndreas Larsson 
216e8beacbbSAndreas Larsson 	/* Clear the events */
217e8beacbbSAndreas Larsson 	mpc8xxx_spi_write_reg(&reg_base->event, events);
218e8beacbbSAndreas Larsson 
219e8beacbbSAndreas Larsson 	mspi->count -= len;
220e8beacbbSAndreas Larsson 	if (mspi->count)
221e8beacbbSAndreas Larsson 		fsl_spi_cpm_bufs_start(mspi);
222e8beacbbSAndreas Larsson 	else
223e8beacbbSAndreas Larsson 		complete(&mspi->done);
224e8beacbbSAndreas Larsson }
22538455d7aSEsben Haabendal EXPORT_SYMBOL_GPL(fsl_spi_cpm_irq);
226e8beacbbSAndreas Larsson 
fsl_spi_alloc_dummy_rx(void)227e8beacbbSAndreas Larsson static void *fsl_spi_alloc_dummy_rx(void)
228e8beacbbSAndreas Larsson {
229e8beacbbSAndreas Larsson 	mutex_lock(&fsl_dummy_rx_lock);
230e8beacbbSAndreas Larsson 
231e8beacbbSAndreas Larsson 	if (!fsl_dummy_rx)
232e8beacbbSAndreas Larsson 		fsl_dummy_rx = kmalloc(SPI_MRBLR, GFP_KERNEL);
233e8beacbbSAndreas Larsson 	if (fsl_dummy_rx)
234e8beacbbSAndreas Larsson 		fsl_dummy_rx_refcnt++;
235e8beacbbSAndreas Larsson 
236e8beacbbSAndreas Larsson 	mutex_unlock(&fsl_dummy_rx_lock);
237e8beacbbSAndreas Larsson 
238e8beacbbSAndreas Larsson 	return fsl_dummy_rx;
239e8beacbbSAndreas Larsson }
240e8beacbbSAndreas Larsson 
fsl_spi_free_dummy_rx(void)241e8beacbbSAndreas Larsson static void fsl_spi_free_dummy_rx(void)
242e8beacbbSAndreas Larsson {
243e8beacbbSAndreas Larsson 	mutex_lock(&fsl_dummy_rx_lock);
244e8beacbbSAndreas Larsson 
245e8beacbbSAndreas Larsson 	switch (fsl_dummy_rx_refcnt) {
246e8beacbbSAndreas Larsson 	case 0:
247e8beacbbSAndreas Larsson 		WARN_ON(1);
248e8beacbbSAndreas Larsson 		break;
249e8beacbbSAndreas Larsson 	case 1:
250e8beacbbSAndreas Larsson 		kfree(fsl_dummy_rx);
251e8beacbbSAndreas Larsson 		fsl_dummy_rx = NULL;
252df561f66SGustavo A. R. Silva 		fallthrough;
253e8beacbbSAndreas Larsson 	default:
254e8beacbbSAndreas Larsson 		fsl_dummy_rx_refcnt--;
255e8beacbbSAndreas Larsson 		break;
256e8beacbbSAndreas Larsson 	}
257e8beacbbSAndreas Larsson 
258e8beacbbSAndreas Larsson 	mutex_unlock(&fsl_dummy_rx_lock);
259e8beacbbSAndreas Larsson }
260e8beacbbSAndreas Larsson 
fsl_spi_cpm_get_pram(struct mpc8xxx_spi * mspi)261e8beacbbSAndreas Larsson static unsigned long fsl_spi_cpm_get_pram(struct mpc8xxx_spi *mspi)
262e8beacbbSAndreas Larsson {
263e8beacbbSAndreas Larsson 	struct device *dev = mspi->dev;
264e8beacbbSAndreas Larsson 	struct device_node *np = dev->of_node;
265e8beacbbSAndreas Larsson 	const u32 *iprop;
266e8beacbbSAndreas Larsson 	int size;
267e8beacbbSAndreas Larsson 	void __iomem *spi_base;
268e8beacbbSAndreas Larsson 	unsigned long pram_ofs = -ENOMEM;
269e8beacbbSAndreas Larsson 
270e8beacbbSAndreas Larsson 	/* Can't use of_address_to_resource(), QE muram isn't at 0. */
271e8beacbbSAndreas Larsson 	iprop = of_get_property(np, "reg", &size);
272e8beacbbSAndreas Larsson 
273e8beacbbSAndreas Larsson 	/* QE with a fixed pram location? */
274e8beacbbSAndreas Larsson 	if (mspi->flags & SPI_QE && iprop && size == sizeof(*iprop) * 4)
275e8beacbbSAndreas Larsson 		return cpm_muram_alloc_fixed(iprop[2], SPI_PRAM_SIZE);
276e8beacbbSAndreas Larsson 
277e8beacbbSAndreas Larsson 	/* QE but with a dynamic pram location? */
278e8beacbbSAndreas Larsson 	if (mspi->flags & SPI_QE) {
279e8beacbbSAndreas Larsson 		pram_ofs = cpm_muram_alloc(SPI_PRAM_SIZE, 64);
280e8beacbbSAndreas Larsson 		qe_issue_cmd(QE_ASSIGN_PAGE_TO_DEVICE, mspi->subblock,
281e8beacbbSAndreas Larsson 			     QE_CR_PROTOCOL_UNSPECIFIED, pram_ofs);
282e8beacbbSAndreas Larsson 		return pram_ofs;
283e8beacbbSAndreas Larsson 	}
284e8beacbbSAndreas Larsson 
285e8beacbbSAndreas Larsson 	spi_base = of_iomap(np, 1);
286e8beacbbSAndreas Larsson 	if (spi_base == NULL)
287e8beacbbSAndreas Larsson 		return -EINVAL;
288e8beacbbSAndreas Larsson 
289e8beacbbSAndreas Larsson 	if (mspi->flags & SPI_CPM2) {
290e8beacbbSAndreas Larsson 		pram_ofs = cpm_muram_alloc(SPI_PRAM_SIZE, 64);
291e8beacbbSAndreas Larsson 		out_be16(spi_base, pram_ofs);
292e8beacbbSAndreas Larsson 	}
293e8beacbbSAndreas Larsson 
294e8beacbbSAndreas Larsson 	iounmap(spi_base);
295e8beacbbSAndreas Larsson 	return pram_ofs;
296e8beacbbSAndreas Larsson }
297e8beacbbSAndreas Larsson 
fsl_spi_cpm_init(struct mpc8xxx_spi * mspi)298e8beacbbSAndreas Larsson int fsl_spi_cpm_init(struct mpc8xxx_spi *mspi)
299e8beacbbSAndreas Larsson {
300e8beacbbSAndreas Larsson 	struct device *dev = mspi->dev;
301e8beacbbSAndreas Larsson 	struct device_node *np = dev->of_node;
302e8beacbbSAndreas Larsson 	const u32 *iprop;
303e8beacbbSAndreas Larsson 	int size;
304e8beacbbSAndreas Larsson 	unsigned long bds_ofs;
305e8beacbbSAndreas Larsson 
306e8beacbbSAndreas Larsson 	if (!(mspi->flags & SPI_CPM_MODE))
307e8beacbbSAndreas Larsson 		return 0;
308e8beacbbSAndreas Larsson 
309e8beacbbSAndreas Larsson 	if (!fsl_spi_alloc_dummy_rx())
310e8beacbbSAndreas Larsson 		return -ENOMEM;
311e8beacbbSAndreas Larsson 
312e8beacbbSAndreas Larsson 	if (mspi->flags & SPI_QE) {
313e8beacbbSAndreas Larsson 		iprop = of_get_property(np, "cell-index", &size);
314e8beacbbSAndreas Larsson 		if (iprop && size == sizeof(*iprop))
315e8beacbbSAndreas Larsson 			mspi->subblock = *iprop;
316e8beacbbSAndreas Larsson 
317e8beacbbSAndreas Larsson 		switch (mspi->subblock) {
318e8beacbbSAndreas Larsson 		default:
319a1829d2bSJarkko Nikula 			dev_warn(dev, "cell-index unspecified, assuming SPI1\n");
320df561f66SGustavo A. R. Silva 			fallthrough;
321e8beacbbSAndreas Larsson 		case 0:
322e8beacbbSAndreas Larsson 			mspi->subblock = QE_CR_SUBBLOCK_SPI1;
323e8beacbbSAndreas Larsson 			break;
324e8beacbbSAndreas Larsson 		case 1:
325e8beacbbSAndreas Larsson 			mspi->subblock = QE_CR_SUBBLOCK_SPI2;
326e8beacbbSAndreas Larsson 			break;
327e8beacbbSAndreas Larsson 		}
328e8beacbbSAndreas Larsson 	}
329e8beacbbSAndreas Larsson 
330575bec53SChristophe Leroy 	if (mspi->flags & SPI_CPM1) {
331*ddaec4e4SChristophe Leroy 		void __iomem *pram;
332575bec53SChristophe Leroy 
33343a5baa6SYueHaibing 		pram = devm_platform_ioremap_resource(to_platform_device(dev),
33443a5baa6SYueHaibing 						      1);
33573ee39a4SChristophe Leroy 		if (IS_ERR(pram))
33673ee39a4SChristophe Leroy 			mspi->pram = NULL;
33773ee39a4SChristophe Leroy 		else
33873ee39a4SChristophe Leroy 			mspi->pram = pram;
339575bec53SChristophe Leroy 	} else {
340575bec53SChristophe Leroy 		unsigned long pram_ofs = fsl_spi_cpm_get_pram(mspi);
341575bec53SChristophe Leroy 
342575bec53SChristophe Leroy 		if (IS_ERR_VALUE(pram_ofs))
343575bec53SChristophe Leroy 			mspi->pram = NULL;
344575bec53SChristophe Leroy 		else
345575bec53SChristophe Leroy 			mspi->pram = cpm_muram_addr(pram_ofs);
346575bec53SChristophe Leroy 	}
347575bec53SChristophe Leroy 	if (mspi->pram == NULL) {
348e8beacbbSAndreas Larsson 		dev_err(dev, "can't allocate spi parameter ram\n");
349e8beacbbSAndreas Larsson 		goto err_pram;
350e8beacbbSAndreas Larsson 	}
351e8beacbbSAndreas Larsson 
352e8beacbbSAndreas Larsson 	bds_ofs = cpm_muram_alloc(sizeof(*mspi->tx_bd) +
353e8beacbbSAndreas Larsson 				  sizeof(*mspi->rx_bd), 8);
354e8beacbbSAndreas Larsson 	if (IS_ERR_VALUE(bds_ofs)) {
355e8beacbbSAndreas Larsson 		dev_err(dev, "can't allocate bds\n");
356e8beacbbSAndreas Larsson 		goto err_bds;
357e8beacbbSAndreas Larsson 	}
358e8beacbbSAndreas Larsson 
3593be6acdaSGiulio Benetti 	mspi->dma_dummy_tx = dma_map_single(dev, ZERO_PAGE(0), PAGE_SIZE,
360e8beacbbSAndreas Larsson 					    DMA_TO_DEVICE);
361e8beacbbSAndreas Larsson 	if (dma_mapping_error(dev, mspi->dma_dummy_tx)) {
362e8beacbbSAndreas Larsson 		dev_err(dev, "unable to map dummy tx buffer\n");
363e8beacbbSAndreas Larsson 		goto err_dummy_tx;
364e8beacbbSAndreas Larsson 	}
365e8beacbbSAndreas Larsson 
366e8beacbbSAndreas Larsson 	mspi->dma_dummy_rx = dma_map_single(dev, fsl_dummy_rx, SPI_MRBLR,
367e8beacbbSAndreas Larsson 					    DMA_FROM_DEVICE);
368e8beacbbSAndreas Larsson 	if (dma_mapping_error(dev, mspi->dma_dummy_rx)) {
369e8beacbbSAndreas Larsson 		dev_err(dev, "unable to map dummy rx buffer\n");
370e8beacbbSAndreas Larsson 		goto err_dummy_rx;
371e8beacbbSAndreas Larsson 	}
372e8beacbbSAndreas Larsson 
373e8beacbbSAndreas Larsson 	mspi->tx_bd = cpm_muram_addr(bds_ofs);
374e8beacbbSAndreas Larsson 	mspi->rx_bd = cpm_muram_addr(bds_ofs + sizeof(*mspi->tx_bd));
375e8beacbbSAndreas Larsson 
376e8beacbbSAndreas Larsson 	/* Initialize parameter ram. */
377*ddaec4e4SChristophe Leroy 	iowrite16be(cpm_muram_offset(mspi->tx_bd), &mspi->pram->tbase);
378*ddaec4e4SChristophe Leroy 	iowrite16be(cpm_muram_offset(mspi->rx_bd), &mspi->pram->rbase);
379*ddaec4e4SChristophe Leroy 	iowrite8(CPMFCR_EB | CPMFCR_GBL, &mspi->pram->tfcr);
380*ddaec4e4SChristophe Leroy 	iowrite8(CPMFCR_EB | CPMFCR_GBL, &mspi->pram->rfcr);
381*ddaec4e4SChristophe Leroy 	iowrite16be(SPI_MRBLR, &mspi->pram->mrblr);
382*ddaec4e4SChristophe Leroy 	iowrite32be(0, &mspi->pram->rstate);
383*ddaec4e4SChristophe Leroy 	iowrite32be(0, &mspi->pram->rdp);
384*ddaec4e4SChristophe Leroy 	iowrite16be(0, &mspi->pram->rbptr);
385*ddaec4e4SChristophe Leroy 	iowrite16be(0, &mspi->pram->rbc);
386*ddaec4e4SChristophe Leroy 	iowrite32be(0, &mspi->pram->rxtmp);
387*ddaec4e4SChristophe Leroy 	iowrite32be(0, &mspi->pram->tstate);
388*ddaec4e4SChristophe Leroy 	iowrite32be(0, &mspi->pram->tdp);
389*ddaec4e4SChristophe Leroy 	iowrite16be(0, &mspi->pram->tbptr);
390*ddaec4e4SChristophe Leroy 	iowrite16be(0, &mspi->pram->tbc);
391*ddaec4e4SChristophe Leroy 	iowrite32be(0, &mspi->pram->txtmp);
392e8beacbbSAndreas Larsson 
393e8beacbbSAndreas Larsson 	return 0;
394e8beacbbSAndreas Larsson 
395e8beacbbSAndreas Larsson err_dummy_rx:
396e8beacbbSAndreas Larsson 	dma_unmap_single(dev, mspi->dma_dummy_tx, PAGE_SIZE, DMA_TO_DEVICE);
397e8beacbbSAndreas Larsson err_dummy_tx:
398e8beacbbSAndreas Larsson 	cpm_muram_free(bds_ofs);
399e8beacbbSAndreas Larsson err_bds:
400575bec53SChristophe Leroy 	if (!(mspi->flags & SPI_CPM1))
401575bec53SChristophe Leroy 		cpm_muram_free(cpm_muram_offset(mspi->pram));
402e8beacbbSAndreas Larsson err_pram:
403e8beacbbSAndreas Larsson 	fsl_spi_free_dummy_rx();
404e8beacbbSAndreas Larsson 	return -ENOMEM;
405e8beacbbSAndreas Larsson }
40638455d7aSEsben Haabendal EXPORT_SYMBOL_GPL(fsl_spi_cpm_init);
407e8beacbbSAndreas Larsson 
fsl_spi_cpm_free(struct mpc8xxx_spi * mspi)408e8beacbbSAndreas Larsson void fsl_spi_cpm_free(struct mpc8xxx_spi *mspi)
409e8beacbbSAndreas Larsson {
410e8beacbbSAndreas Larsson 	struct device *dev = mspi->dev;
411e8beacbbSAndreas Larsson 
412e8beacbbSAndreas Larsson 	if (!(mspi->flags & SPI_CPM_MODE))
413e8beacbbSAndreas Larsson 		return;
414e8beacbbSAndreas Larsson 
415e8beacbbSAndreas Larsson 	dma_unmap_single(dev, mspi->dma_dummy_rx, SPI_MRBLR, DMA_FROM_DEVICE);
416e8beacbbSAndreas Larsson 	dma_unmap_single(dev, mspi->dma_dummy_tx, PAGE_SIZE, DMA_TO_DEVICE);
417e8beacbbSAndreas Larsson 	cpm_muram_free(cpm_muram_offset(mspi->tx_bd));
418c5923243SLinus Walleij 	if (!(mspi->flags & SPI_CPM1))
419e8beacbbSAndreas Larsson 		cpm_muram_free(cpm_muram_offset(mspi->pram));
420e8beacbbSAndreas Larsson 	fsl_spi_free_dummy_rx();
421e8beacbbSAndreas Larsson }
42238455d7aSEsben Haabendal EXPORT_SYMBOL_GPL(fsl_spi_cpm_free);
42338455d7aSEsben Haabendal 
42438455d7aSEsben Haabendal MODULE_LICENSE("GPL");
425