xref: /linux/drivers/spi/spi-geni-qcom.c (revision 690d8b917bbe64772cb0b652311bcd50908aea6b)
1561de45fSGirish Mahadevan // SPDX-License-Identifier: GPL-2.0
2561de45fSGirish Mahadevan // Copyright (c) 2017-2018, The Linux foundation. All rights reserved.
3561de45fSGirish Mahadevan 
4561de45fSGirish Mahadevan #include <linux/clk.h>
5561de45fSGirish Mahadevan #include <linux/interrupt.h>
6561de45fSGirish Mahadevan #include <linux/io.h>
7561de45fSGirish Mahadevan #include <linux/log2.h>
8561de45fSGirish Mahadevan #include <linux/module.h>
9561de45fSGirish Mahadevan #include <linux/platform_device.h>
101a9e489eSRajendra Nayak #include <linux/pm_opp.h>
11561de45fSGirish Mahadevan #include <linux/pm_runtime.h>
12561de45fSGirish Mahadevan #include <linux/qcom-geni-se.h>
13561de45fSGirish Mahadevan #include <linux/spi/spi.h>
14561de45fSGirish Mahadevan #include <linux/spinlock.h>
15561de45fSGirish Mahadevan 
16561de45fSGirish Mahadevan /* SPI SE specific registers and respective register fields */
17561de45fSGirish Mahadevan #define SE_SPI_CPHA		0x224
18561de45fSGirish Mahadevan #define CPHA			BIT(0)
19561de45fSGirish Mahadevan 
20561de45fSGirish Mahadevan #define SE_SPI_LOOPBACK		0x22c
21561de45fSGirish Mahadevan #define LOOPBACK_ENABLE		0x1
22561de45fSGirish Mahadevan #define NORMAL_MODE		0x0
23561de45fSGirish Mahadevan #define LOOPBACK_MSK		GENMASK(1, 0)
24561de45fSGirish Mahadevan 
25561de45fSGirish Mahadevan #define SE_SPI_CPOL		0x230
26561de45fSGirish Mahadevan #define CPOL			BIT(2)
27561de45fSGirish Mahadevan 
28561de45fSGirish Mahadevan #define SE_SPI_DEMUX_OUTPUT_INV	0x24c
29561de45fSGirish Mahadevan #define CS_DEMUX_OUTPUT_INV_MSK	GENMASK(3, 0)
30561de45fSGirish Mahadevan 
31561de45fSGirish Mahadevan #define SE_SPI_DEMUX_SEL	0x250
32561de45fSGirish Mahadevan #define CS_DEMUX_OUTPUT_SEL	GENMASK(3, 0)
33561de45fSGirish Mahadevan 
34561de45fSGirish Mahadevan #define SE_SPI_TRANS_CFG	0x25c
35561de45fSGirish Mahadevan #define CS_TOGGLE		BIT(0)
36561de45fSGirish Mahadevan 
37561de45fSGirish Mahadevan #define SE_SPI_WORD_LEN		0x268
38561de45fSGirish Mahadevan #define WORD_LEN_MSK		GENMASK(9, 0)
39561de45fSGirish Mahadevan #define MIN_WORD_LEN		4
40561de45fSGirish Mahadevan 
41561de45fSGirish Mahadevan #define SE_SPI_TX_TRANS_LEN	0x26c
42561de45fSGirish Mahadevan #define SE_SPI_RX_TRANS_LEN	0x270
43561de45fSGirish Mahadevan #define TRANS_LEN_MSK		GENMASK(23, 0)
44561de45fSGirish Mahadevan 
45561de45fSGirish Mahadevan #define SE_SPI_PRE_POST_CMD_DLY	0x274
46561de45fSGirish Mahadevan 
47561de45fSGirish Mahadevan #define SE_SPI_DELAY_COUNTERS	0x278
48561de45fSGirish Mahadevan #define SPI_INTER_WORDS_DELAY_MSK	GENMASK(9, 0)
49561de45fSGirish Mahadevan #define SPI_CS_CLK_DELAY_MSK		GENMASK(19, 10)
50561de45fSGirish Mahadevan #define SPI_CS_CLK_DELAY_SHFT		10
51561de45fSGirish Mahadevan 
52561de45fSGirish Mahadevan /* M_CMD OP codes for SPI */
53561de45fSGirish Mahadevan #define SPI_TX_ONLY		1
54561de45fSGirish Mahadevan #define SPI_RX_ONLY		2
55561de45fSGirish Mahadevan #define SPI_TX_RX		7
56561de45fSGirish Mahadevan #define SPI_CS_ASSERT		8
57561de45fSGirish Mahadevan #define SPI_CS_DEASSERT		9
58561de45fSGirish Mahadevan #define SPI_SCK_ONLY		10
59561de45fSGirish Mahadevan /* M_CMD params for SPI */
60561de45fSGirish Mahadevan #define SPI_PRE_CMD_DELAY	BIT(0)
61561de45fSGirish Mahadevan #define TIMESTAMP_BEFORE	BIT(1)
62561de45fSGirish Mahadevan #define FRAGMENTATION		BIT(2)
63561de45fSGirish Mahadevan #define TIMESTAMP_AFTER		BIT(3)
64561de45fSGirish Mahadevan #define POST_CMD_DELAY		BIT(4)
65561de45fSGirish Mahadevan 
66561de45fSGirish Mahadevan struct spi_geni_master {
67561de45fSGirish Mahadevan 	struct geni_se se;
68561de45fSGirish Mahadevan 	struct device *dev;
69561de45fSGirish Mahadevan 	u32 tx_fifo_depth;
70561de45fSGirish Mahadevan 	u32 fifo_width_bits;
71561de45fSGirish Mahadevan 	u32 tx_wm;
72da48dc8cSDouglas Anderson 	u32 last_mode;
73561de45fSGirish Mahadevan 	unsigned long cur_speed_hz;
745f219524SDouglas Anderson 	unsigned long cur_sclk_hz;
75561de45fSGirish Mahadevan 	unsigned int cur_bits_per_word;
76561de45fSGirish Mahadevan 	unsigned int tx_rem_bytes;
77561de45fSGirish Mahadevan 	unsigned int rx_rem_bytes;
78561de45fSGirish Mahadevan 	const struct spi_transfer *cur_xfer;
797ba9bdcbSDouglas Anderson 	struct completion cs_done;
807ba9bdcbSDouglas Anderson 	struct completion cancel_done;
817ba9bdcbSDouglas Anderson 	struct completion abort_done;
82561de45fSGirish Mahadevan 	unsigned int oversampling;
83561de45fSGirish Mahadevan 	spinlock_t lock;
84561de45fSGirish Mahadevan 	int irq;
85638d8488SDouglas Anderson 	bool cs_flag;
86*690d8b91SDouglas Anderson 	bool abort_failed;
87561de45fSGirish Mahadevan };
88561de45fSGirish Mahadevan 
89561de45fSGirish Mahadevan static int get_spi_clk_cfg(unsigned int speed_hz,
90561de45fSGirish Mahadevan 			struct spi_geni_master *mas,
91561de45fSGirish Mahadevan 			unsigned int *clk_idx,
92561de45fSGirish Mahadevan 			unsigned int *clk_div)
93561de45fSGirish Mahadevan {
94561de45fSGirish Mahadevan 	unsigned long sclk_freq;
95561de45fSGirish Mahadevan 	unsigned int actual_hz;
96561de45fSGirish Mahadevan 	int ret;
97561de45fSGirish Mahadevan 
98561de45fSGirish Mahadevan 	ret = geni_se_clk_freq_match(&mas->se,
99561de45fSGirish Mahadevan 				speed_hz * mas->oversampling,
100561de45fSGirish Mahadevan 				clk_idx, &sclk_freq, false);
101561de45fSGirish Mahadevan 	if (ret) {
102561de45fSGirish Mahadevan 		dev_err(mas->dev, "Failed(%d) to find src clk for %dHz\n",
103561de45fSGirish Mahadevan 							ret, speed_hz);
104561de45fSGirish Mahadevan 		return ret;
105561de45fSGirish Mahadevan 	}
106561de45fSGirish Mahadevan 
107561de45fSGirish Mahadevan 	*clk_div = DIV_ROUND_UP(sclk_freq, mas->oversampling * speed_hz);
108561de45fSGirish Mahadevan 	actual_hz = sclk_freq / (mas->oversampling * *clk_div);
109561de45fSGirish Mahadevan 
110561de45fSGirish Mahadevan 	dev_dbg(mas->dev, "req %u=>%u sclk %lu, idx %d, div %d\n", speed_hz,
111561de45fSGirish Mahadevan 				actual_hz, sclk_freq, *clk_idx, *clk_div);
1121a9e489eSRajendra Nayak 	ret = dev_pm_opp_set_rate(mas->dev, sclk_freq);
113561de45fSGirish Mahadevan 	if (ret)
1141a9e489eSRajendra Nayak 		dev_err(mas->dev, "dev_pm_opp_set_rate failed %d\n", ret);
1155f219524SDouglas Anderson 	else
1165f219524SDouglas Anderson 		mas->cur_sclk_hz = sclk_freq;
1175f219524SDouglas Anderson 
118561de45fSGirish Mahadevan 	return ret;
119561de45fSGirish Mahadevan }
120561de45fSGirish Mahadevan 
121de43affeSStephen Boyd static void handle_fifo_timeout(struct spi_master *spi,
122de43affeSStephen Boyd 				struct spi_message *msg)
123de43affeSStephen Boyd {
124de43affeSStephen Boyd 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
125539afdf9SDouglas Anderson 	unsigned long time_left;
126de43affeSStephen Boyd 	struct geni_se *se = &mas->se;
127de43affeSStephen Boyd 
128539afdf9SDouglas Anderson 	spin_lock_irq(&mas->lock);
1297ba9bdcbSDouglas Anderson 	reinit_completion(&mas->cancel_done);
130de43affeSStephen Boyd 	writel(0, se->base + SE_GENI_TX_WATERMARK_REG);
1317ba9bdcbSDouglas Anderson 	mas->cur_xfer = NULL;
1327ba9bdcbSDouglas Anderson 	geni_se_cancel_m_cmd(se);
133539afdf9SDouglas Anderson 	spin_unlock_irq(&mas->lock);
1347ba9bdcbSDouglas Anderson 
1357ba9bdcbSDouglas Anderson 	time_left = wait_for_completion_timeout(&mas->cancel_done, HZ);
136de43affeSStephen Boyd 	if (time_left)
137de43affeSStephen Boyd 		return;
138de43affeSStephen Boyd 
139539afdf9SDouglas Anderson 	spin_lock_irq(&mas->lock);
1407ba9bdcbSDouglas Anderson 	reinit_completion(&mas->abort_done);
141de43affeSStephen Boyd 	geni_se_abort_m_cmd(se);
142539afdf9SDouglas Anderson 	spin_unlock_irq(&mas->lock);
1437ba9bdcbSDouglas Anderson 
1447ba9bdcbSDouglas Anderson 	time_left = wait_for_completion_timeout(&mas->abort_done, HZ);
145*690d8b91SDouglas Anderson 	if (!time_left) {
146de43affeSStephen Boyd 		dev_err(mas->dev, "Failed to cancel/abort m_cmd\n");
147*690d8b91SDouglas Anderson 
148*690d8b91SDouglas Anderson 		/*
149*690d8b91SDouglas Anderson 		 * No need for a lock since SPI core has a lock and we never
150*690d8b91SDouglas Anderson 		 * access this from an interrupt.
151*690d8b91SDouglas Anderson 		 */
152*690d8b91SDouglas Anderson 		mas->abort_failed = true;
153*690d8b91SDouglas Anderson 	}
154*690d8b91SDouglas Anderson }
155*690d8b91SDouglas Anderson 
156*690d8b91SDouglas Anderson static bool spi_geni_is_abort_still_pending(struct spi_geni_master *mas)
157*690d8b91SDouglas Anderson {
158*690d8b91SDouglas Anderson 	struct geni_se *se = &mas->se;
159*690d8b91SDouglas Anderson 	u32 m_irq, m_irq_en;
160*690d8b91SDouglas Anderson 
161*690d8b91SDouglas Anderson 	if (!mas->abort_failed)
162*690d8b91SDouglas Anderson 		return false;
163*690d8b91SDouglas Anderson 
164*690d8b91SDouglas Anderson 	/*
165*690d8b91SDouglas Anderson 	 * The only known case where a transfer times out and then a cancel
166*690d8b91SDouglas Anderson 	 * times out then an abort times out is if something is blocking our
167*690d8b91SDouglas Anderson 	 * interrupt handler from running.  Avoid starting any new transfers
168*690d8b91SDouglas Anderson 	 * until that sorts itself out.
169*690d8b91SDouglas Anderson 	 */
170*690d8b91SDouglas Anderson 	spin_lock_irq(&mas->lock);
171*690d8b91SDouglas Anderson 	m_irq = readl(se->base + SE_GENI_M_IRQ_STATUS);
172*690d8b91SDouglas Anderson 	m_irq_en = readl(se->base + SE_GENI_M_IRQ_EN);
173*690d8b91SDouglas Anderson 	spin_unlock_irq(&mas->lock);
174*690d8b91SDouglas Anderson 
175*690d8b91SDouglas Anderson 	if (m_irq & m_irq_en) {
176*690d8b91SDouglas Anderson 		dev_err(mas->dev, "Interrupts pending after abort: %#010x\n",
177*690d8b91SDouglas Anderson 			m_irq & m_irq_en);
178*690d8b91SDouglas Anderson 		return true;
179*690d8b91SDouglas Anderson 	}
180*690d8b91SDouglas Anderson 
181*690d8b91SDouglas Anderson 	/*
182*690d8b91SDouglas Anderson 	 * If we're here the problem resolved itself so no need to check more
183*690d8b91SDouglas Anderson 	 * on future transfers.
184*690d8b91SDouglas Anderson 	 */
185*690d8b91SDouglas Anderson 	mas->abort_failed = false;
186*690d8b91SDouglas Anderson 
187*690d8b91SDouglas Anderson 	return false;
188de43affeSStephen Boyd }
189de43affeSStephen Boyd 
190561de45fSGirish Mahadevan static void spi_geni_set_cs(struct spi_device *slv, bool set_flag)
191561de45fSGirish Mahadevan {
192561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(slv->master);
193561de45fSGirish Mahadevan 	struct spi_master *spi = dev_get_drvdata(mas->dev);
194561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
1950dccff3cSAlok Chauhan 	unsigned long time_left;
196561de45fSGirish Mahadevan 
197561de45fSGirish Mahadevan 	if (!(slv->mode & SPI_CS_HIGH))
198561de45fSGirish Mahadevan 		set_flag = !set_flag;
199561de45fSGirish Mahadevan 
200638d8488SDouglas Anderson 	if (set_flag == mas->cs_flag)
201638d8488SDouglas Anderson 		return;
202638d8488SDouglas Anderson 
203*690d8b91SDouglas Anderson 	pm_runtime_get_sync(mas->dev);
204*690d8b91SDouglas Anderson 
205*690d8b91SDouglas Anderson 	if (spi_geni_is_abort_still_pending(mas)) {
206*690d8b91SDouglas Anderson 		dev_err(mas->dev, "Can't set chip select\n");
207*690d8b91SDouglas Anderson 		goto exit;
208*690d8b91SDouglas Anderson 	}
209*690d8b91SDouglas Anderson 
210638d8488SDouglas Anderson 	mas->cs_flag = set_flag;
211638d8488SDouglas Anderson 
2122ee471a1SDouglas Anderson 	spin_lock_irq(&mas->lock);
2137ba9bdcbSDouglas Anderson 	reinit_completion(&mas->cs_done);
214561de45fSGirish Mahadevan 	if (set_flag)
215561de45fSGirish Mahadevan 		geni_se_setup_m_cmd(se, SPI_CS_ASSERT, 0);
216561de45fSGirish Mahadevan 	else
217561de45fSGirish Mahadevan 		geni_se_setup_m_cmd(se, SPI_CS_DEASSERT, 0);
2182ee471a1SDouglas Anderson 	spin_unlock_irq(&mas->lock);
219561de45fSGirish Mahadevan 
2207ba9bdcbSDouglas Anderson 	time_left = wait_for_completion_timeout(&mas->cs_done, HZ);
2210dccff3cSAlok Chauhan 	if (!time_left)
222561de45fSGirish Mahadevan 		handle_fifo_timeout(spi, NULL);
223561de45fSGirish Mahadevan 
224*690d8b91SDouglas Anderson exit:
225561de45fSGirish Mahadevan 	pm_runtime_put(mas->dev);
226561de45fSGirish Mahadevan }
227561de45fSGirish Mahadevan 
228561de45fSGirish Mahadevan static void spi_setup_word_len(struct spi_geni_master *mas, u16 mode,
229561de45fSGirish Mahadevan 					unsigned int bits_per_word)
230561de45fSGirish Mahadevan {
231561de45fSGirish Mahadevan 	unsigned int pack_words;
232561de45fSGirish Mahadevan 	bool msb_first = (mode & SPI_LSB_FIRST) ? false : true;
233561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
234561de45fSGirish Mahadevan 	u32 word_len;
235561de45fSGirish Mahadevan 
236561de45fSGirish Mahadevan 	/*
237561de45fSGirish Mahadevan 	 * If bits_per_word isn't a byte aligned value, set the packing to be
238561de45fSGirish Mahadevan 	 * 1 SPI word per FIFO word.
239561de45fSGirish Mahadevan 	 */
240561de45fSGirish Mahadevan 	if (!(mas->fifo_width_bits % bits_per_word))
241561de45fSGirish Mahadevan 		pack_words = mas->fifo_width_bits / bits_per_word;
242561de45fSGirish Mahadevan 	else
243561de45fSGirish Mahadevan 		pack_words = 1;
244561de45fSGirish Mahadevan 	geni_se_config_packing(&mas->se, bits_per_word, pack_words, msb_first,
245561de45fSGirish Mahadevan 								true, true);
246da48dc8cSDouglas Anderson 	word_len = (bits_per_word - MIN_WORD_LEN) & WORD_LEN_MSK;
247561de45fSGirish Mahadevan 	writel(word_len, se->base + SE_SPI_WORD_LEN);
248561de45fSGirish Mahadevan }
249561de45fSGirish Mahadevan 
2500e3b8a81SAkash Asthana static int geni_spi_set_clock_and_bw(struct spi_geni_master *mas,
2510e3b8a81SAkash Asthana 					unsigned long clk_hz)
252e68b6624SDouglas Anderson {
253e68b6624SDouglas Anderson 	u32 clk_sel, m_clk_cfg, idx, div;
254e68b6624SDouglas Anderson 	struct geni_se *se = &mas->se;
255e68b6624SDouglas Anderson 	int ret;
256e68b6624SDouglas Anderson 
25768890e20SDouglas Anderson 	if (clk_hz == mas->cur_speed_hz)
25868890e20SDouglas Anderson 		return 0;
25968890e20SDouglas Anderson 
260e68b6624SDouglas Anderson 	ret = get_spi_clk_cfg(clk_hz, mas, &idx, &div);
261e68b6624SDouglas Anderson 	if (ret) {
262e68b6624SDouglas Anderson 		dev_err(mas->dev, "Err setting clk to %lu: %d\n", clk_hz, ret);
263e68b6624SDouglas Anderson 		return ret;
264e68b6624SDouglas Anderson 	}
265e68b6624SDouglas Anderson 
266e68b6624SDouglas Anderson 	/*
267e68b6624SDouglas Anderson 	 * SPI core clock gets configured with the requested frequency
268e68b6624SDouglas Anderson 	 * or the frequency closer to the requested frequency.
269e68b6624SDouglas Anderson 	 * For that reason requested frequency is stored in the
270e68b6624SDouglas Anderson 	 * cur_speed_hz and referred in the consecutive transfer instead
271e68b6624SDouglas Anderson 	 * of calling clk_get_rate() API.
272e68b6624SDouglas Anderson 	 */
273e68b6624SDouglas Anderson 	mas->cur_speed_hz = clk_hz;
274e68b6624SDouglas Anderson 
275e68b6624SDouglas Anderson 	clk_sel = idx & CLK_SEL_MSK;
276e68b6624SDouglas Anderson 	m_clk_cfg = (div << CLK_DIV_SHFT) | SER_CLK_EN;
277e68b6624SDouglas Anderson 	writel(clk_sel, se->base + SE_GENI_CLK_SEL);
278e68b6624SDouglas Anderson 	writel(m_clk_cfg, se->base + GENI_SER_M_CLK_CFG);
279e68b6624SDouglas Anderson 
2800e3b8a81SAkash Asthana 	/* Set BW quota for CPU as driver supports FIFO mode only. */
2810e3b8a81SAkash Asthana 	se->icc_paths[CPU_TO_GENI].avg_bw = Bps_to_icc(mas->cur_speed_hz);
2820e3b8a81SAkash Asthana 	ret = geni_icc_set_bw(se);
2830e3b8a81SAkash Asthana 	if (ret)
2840e3b8a81SAkash Asthana 		return ret;
2850e3b8a81SAkash Asthana 
286e68b6624SDouglas Anderson 	return 0;
287e68b6624SDouglas Anderson }
288e68b6624SDouglas Anderson 
289561de45fSGirish Mahadevan static int setup_fifo_params(struct spi_device *spi_slv,
290561de45fSGirish Mahadevan 					struct spi_master *spi)
291561de45fSGirish Mahadevan {
292561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
293561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
294da48dc8cSDouglas Anderson 	u32 loopback_cfg = 0, cpol = 0, cpha = 0, demux_output_inv = 0;
295e68b6624SDouglas Anderson 	u32 demux_sel;
296561de45fSGirish Mahadevan 
297da48dc8cSDouglas Anderson 	if (mas->last_mode != spi_slv->mode) {
298561de45fSGirish Mahadevan 		if (spi_slv->mode & SPI_LOOP)
299da48dc8cSDouglas Anderson 			loopback_cfg = LOOPBACK_ENABLE;
300561de45fSGirish Mahadevan 
301561de45fSGirish Mahadevan 		if (spi_slv->mode & SPI_CPOL)
302da48dc8cSDouglas Anderson 			cpol = CPOL;
303561de45fSGirish Mahadevan 
304561de45fSGirish Mahadevan 		if (spi_slv->mode & SPI_CPHA)
305da48dc8cSDouglas Anderson 			cpha = CPHA;
306561de45fSGirish Mahadevan 
307561de45fSGirish Mahadevan 		if (spi_slv->mode & SPI_CS_HIGH)
308561de45fSGirish Mahadevan 			demux_output_inv = BIT(spi_slv->chip_select);
309561de45fSGirish Mahadevan 
310561de45fSGirish Mahadevan 		demux_sel = spi_slv->chip_select;
311561de45fSGirish Mahadevan 		mas->cur_bits_per_word = spi_slv->bits_per_word;
312561de45fSGirish Mahadevan 
313561de45fSGirish Mahadevan 		spi_setup_word_len(mas, spi_slv->mode, spi_slv->bits_per_word);
314561de45fSGirish Mahadevan 		writel(loopback_cfg, se->base + SE_SPI_LOOPBACK);
315561de45fSGirish Mahadevan 		writel(demux_sel, se->base + SE_SPI_DEMUX_SEL);
316561de45fSGirish Mahadevan 		writel(cpha, se->base + SE_SPI_CPHA);
317561de45fSGirish Mahadevan 		writel(cpol, se->base + SE_SPI_CPOL);
318561de45fSGirish Mahadevan 		writel(demux_output_inv, se->base + SE_SPI_DEMUX_OUTPUT_INV);
319e68b6624SDouglas Anderson 
320da48dc8cSDouglas Anderson 		mas->last_mode = spi_slv->mode;
321da48dc8cSDouglas Anderson 	}
322da48dc8cSDouglas Anderson 
3230e3b8a81SAkash Asthana 	return geni_spi_set_clock_and_bw(mas, spi_slv->max_speed_hz);
324561de45fSGirish Mahadevan }
325561de45fSGirish Mahadevan 
326561de45fSGirish Mahadevan static int spi_geni_prepare_message(struct spi_master *spi,
327561de45fSGirish Mahadevan 					struct spi_message *spi_msg)
328561de45fSGirish Mahadevan {
329561de45fSGirish Mahadevan 	int ret;
330561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
331561de45fSGirish Mahadevan 
332*690d8b91SDouglas Anderson 	if (spi_geni_is_abort_still_pending(mas))
333*690d8b91SDouglas Anderson 		return -EBUSY;
334*690d8b91SDouglas Anderson 
335561de45fSGirish Mahadevan 	ret = setup_fifo_params(spi_msg->spi, spi);
336561de45fSGirish Mahadevan 	if (ret)
337561de45fSGirish Mahadevan 		dev_err(mas->dev, "Couldn't select mode %d\n", ret);
338561de45fSGirish Mahadevan 	return ret;
339561de45fSGirish Mahadevan }
340561de45fSGirish Mahadevan 
341561de45fSGirish Mahadevan static int spi_geni_init(struct spi_geni_master *mas)
342561de45fSGirish Mahadevan {
343561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
344561de45fSGirish Mahadevan 	unsigned int proto, major, minor, ver;
34514ac4e04SDouglas Anderson 	u32 spi_tx_cfg;
346561de45fSGirish Mahadevan 
347561de45fSGirish Mahadevan 	pm_runtime_get_sync(mas->dev);
348561de45fSGirish Mahadevan 
349561de45fSGirish Mahadevan 	proto = geni_se_read_proto(se);
350561de45fSGirish Mahadevan 	if (proto != GENI_SE_SPI) {
351561de45fSGirish Mahadevan 		dev_err(mas->dev, "Invalid proto %d\n", proto);
352561de45fSGirish Mahadevan 		pm_runtime_put(mas->dev);
353561de45fSGirish Mahadevan 		return -ENXIO;
354561de45fSGirish Mahadevan 	}
355561de45fSGirish Mahadevan 	mas->tx_fifo_depth = geni_se_get_tx_fifo_depth(se);
356561de45fSGirish Mahadevan 
357561de45fSGirish Mahadevan 	/* Width of Tx and Rx FIFO is same */
358561de45fSGirish Mahadevan 	mas->fifo_width_bits = geni_se_get_tx_fifo_width(se);
359561de45fSGirish Mahadevan 
360561de45fSGirish Mahadevan 	/*
361561de45fSGirish Mahadevan 	 * Hardware programming guide suggests to configure
362561de45fSGirish Mahadevan 	 * RX FIFO RFR level to fifo_depth-2.
363561de45fSGirish Mahadevan 	 */
364fc129a43SDouglas Anderson 	geni_se_init(se, mas->tx_fifo_depth - 3, mas->tx_fifo_depth - 2);
365561de45fSGirish Mahadevan 	/* Transmit an entire FIFO worth of data per IRQ */
366561de45fSGirish Mahadevan 	mas->tx_wm = 1;
367561de45fSGirish Mahadevan 	ver = geni_se_get_qup_hw_version(se);
368561de45fSGirish Mahadevan 	major = GENI_SE_VERSION_MAJOR(ver);
369561de45fSGirish Mahadevan 	minor = GENI_SE_VERSION_MINOR(ver);
370561de45fSGirish Mahadevan 
371561de45fSGirish Mahadevan 	if (major == 1 && minor == 0)
372561de45fSGirish Mahadevan 		mas->oversampling = 2;
373561de45fSGirish Mahadevan 	else
374561de45fSGirish Mahadevan 		mas->oversampling = 1;
375561de45fSGirish Mahadevan 
376da48dc8cSDouglas Anderson 	geni_se_select_mode(se, GENI_SE_FIFO);
377da48dc8cSDouglas Anderson 
37814ac4e04SDouglas Anderson 	/* We always control CS manually */
37914ac4e04SDouglas Anderson 	spi_tx_cfg = readl(se->base + SE_SPI_TRANS_CFG);
38014ac4e04SDouglas Anderson 	spi_tx_cfg &= ~CS_TOGGLE;
38114ac4e04SDouglas Anderson 	writel(spi_tx_cfg, se->base + SE_SPI_TRANS_CFG);
38214ac4e04SDouglas Anderson 
383561de45fSGirish Mahadevan 	pm_runtime_put(mas->dev);
384561de45fSGirish Mahadevan 	return 0;
385561de45fSGirish Mahadevan }
386561de45fSGirish Mahadevan 
3876d66507dSDouglas Anderson static unsigned int geni_byte_per_fifo_word(struct spi_geni_master *mas)
3886d66507dSDouglas Anderson {
3896d66507dSDouglas Anderson 	/*
3906d66507dSDouglas Anderson 	 * Calculate how many bytes we'll put in each FIFO word.  If the
3916d66507dSDouglas Anderson 	 * transfer words don't pack cleanly into a FIFO word we'll just put
3926d66507dSDouglas Anderson 	 * one transfer word in each FIFO word.  If they do pack we'll pack 'em.
3936d66507dSDouglas Anderson 	 */
3946d66507dSDouglas Anderson 	if (mas->fifo_width_bits % mas->cur_bits_per_word)
3956d66507dSDouglas Anderson 		return roundup_pow_of_two(DIV_ROUND_UP(mas->cur_bits_per_word,
3966d66507dSDouglas Anderson 						       BITS_PER_BYTE));
3976d66507dSDouglas Anderson 
3986d66507dSDouglas Anderson 	return mas->fifo_width_bits / BITS_PER_BYTE;
3996d66507dSDouglas Anderson }
4006d66507dSDouglas Anderson 
4016d66507dSDouglas Anderson static bool geni_spi_handle_tx(struct spi_geni_master *mas)
4026d66507dSDouglas Anderson {
4036d66507dSDouglas Anderson 	struct geni_se *se = &mas->se;
4046d66507dSDouglas Anderson 	unsigned int max_bytes;
4056d66507dSDouglas Anderson 	const u8 *tx_buf;
4066d66507dSDouglas Anderson 	unsigned int bytes_per_fifo_word = geni_byte_per_fifo_word(mas);
4076d66507dSDouglas Anderson 	unsigned int i = 0;
4086d66507dSDouglas Anderson 
4094aa1464aSDouglas Anderson 	/* Stop the watermark IRQ if nothing to send */
4104aa1464aSDouglas Anderson 	if (!mas->cur_xfer) {
4114aa1464aSDouglas Anderson 		writel(0, se->base + SE_GENI_TX_WATERMARK_REG);
4124aa1464aSDouglas Anderson 		return false;
4134aa1464aSDouglas Anderson 	}
4144aa1464aSDouglas Anderson 
4156d66507dSDouglas Anderson 	max_bytes = (mas->tx_fifo_depth - mas->tx_wm) * bytes_per_fifo_word;
4166d66507dSDouglas Anderson 	if (mas->tx_rem_bytes < max_bytes)
4176d66507dSDouglas Anderson 		max_bytes = mas->tx_rem_bytes;
4186d66507dSDouglas Anderson 
4196d66507dSDouglas Anderson 	tx_buf = mas->cur_xfer->tx_buf + mas->cur_xfer->len - mas->tx_rem_bytes;
4206d66507dSDouglas Anderson 	while (i < max_bytes) {
4216d66507dSDouglas Anderson 		unsigned int j;
4226d66507dSDouglas Anderson 		unsigned int bytes_to_write;
4236d66507dSDouglas Anderson 		u32 fifo_word = 0;
4246d66507dSDouglas Anderson 		u8 *fifo_byte = (u8 *)&fifo_word;
4256d66507dSDouglas Anderson 
4266d66507dSDouglas Anderson 		bytes_to_write = min(bytes_per_fifo_word, max_bytes - i);
4276d66507dSDouglas Anderson 		for (j = 0; j < bytes_to_write; j++)
4286d66507dSDouglas Anderson 			fifo_byte[j] = tx_buf[i++];
4296d66507dSDouglas Anderson 		iowrite32_rep(se->base + SE_GENI_TX_FIFOn, &fifo_word, 1);
4306d66507dSDouglas Anderson 	}
4316d66507dSDouglas Anderson 	mas->tx_rem_bytes -= max_bytes;
4326d66507dSDouglas Anderson 	if (!mas->tx_rem_bytes) {
4336d66507dSDouglas Anderson 		writel(0, se->base + SE_GENI_TX_WATERMARK_REG);
4346d66507dSDouglas Anderson 		return false;
4356d66507dSDouglas Anderson 	}
4366d66507dSDouglas Anderson 	return true;
4376d66507dSDouglas Anderson }
4386d66507dSDouglas Anderson 
4396d66507dSDouglas Anderson static void geni_spi_handle_rx(struct spi_geni_master *mas)
4406d66507dSDouglas Anderson {
4416d66507dSDouglas Anderson 	struct geni_se *se = &mas->se;
4426d66507dSDouglas Anderson 	u32 rx_fifo_status;
4436d66507dSDouglas Anderson 	unsigned int rx_bytes;
4446d66507dSDouglas Anderson 	unsigned int rx_last_byte_valid;
4456d66507dSDouglas Anderson 	u8 *rx_buf;
4466d66507dSDouglas Anderson 	unsigned int bytes_per_fifo_word = geni_byte_per_fifo_word(mas);
4476d66507dSDouglas Anderson 	unsigned int i = 0;
4486d66507dSDouglas Anderson 
4496d66507dSDouglas Anderson 	rx_fifo_status = readl(se->base + SE_GENI_RX_FIFO_STATUS);
4506d66507dSDouglas Anderson 	rx_bytes = (rx_fifo_status & RX_FIFO_WC_MSK) * bytes_per_fifo_word;
4516d66507dSDouglas Anderson 	if (rx_fifo_status & RX_LAST) {
4526d66507dSDouglas Anderson 		rx_last_byte_valid = rx_fifo_status & RX_LAST_BYTE_VALID_MSK;
4536d66507dSDouglas Anderson 		rx_last_byte_valid >>= RX_LAST_BYTE_VALID_SHFT;
4546d66507dSDouglas Anderson 		if (rx_last_byte_valid && rx_last_byte_valid < 4)
4556d66507dSDouglas Anderson 			rx_bytes -= bytes_per_fifo_word - rx_last_byte_valid;
4566d66507dSDouglas Anderson 	}
4574aa1464aSDouglas Anderson 
4584aa1464aSDouglas Anderson 	/* Clear out the FIFO and bail if nowhere to put it */
4594aa1464aSDouglas Anderson 	if (!mas->cur_xfer) {
4604aa1464aSDouglas Anderson 		for (i = 0; i < DIV_ROUND_UP(rx_bytes, bytes_per_fifo_word); i++)
4614aa1464aSDouglas Anderson 			readl(se->base + SE_GENI_RX_FIFOn);
4624aa1464aSDouglas Anderson 		return;
4634aa1464aSDouglas Anderson 	}
4644aa1464aSDouglas Anderson 
4656d66507dSDouglas Anderson 	if (mas->rx_rem_bytes < rx_bytes)
4666d66507dSDouglas Anderson 		rx_bytes = mas->rx_rem_bytes;
4676d66507dSDouglas Anderson 
4686d66507dSDouglas Anderson 	rx_buf = mas->cur_xfer->rx_buf + mas->cur_xfer->len - mas->rx_rem_bytes;
4696d66507dSDouglas Anderson 	while (i < rx_bytes) {
4706d66507dSDouglas Anderson 		u32 fifo_word = 0;
4716d66507dSDouglas Anderson 		u8 *fifo_byte = (u8 *)&fifo_word;
4726d66507dSDouglas Anderson 		unsigned int bytes_to_read;
4736d66507dSDouglas Anderson 		unsigned int j;
4746d66507dSDouglas Anderson 
4756d66507dSDouglas Anderson 		bytes_to_read = min(bytes_per_fifo_word, rx_bytes - i);
4766d66507dSDouglas Anderson 		ioread32_rep(se->base + SE_GENI_RX_FIFOn, &fifo_word, 1);
4776d66507dSDouglas Anderson 		for (j = 0; j < bytes_to_read; j++)
4786d66507dSDouglas Anderson 			rx_buf[i++] = fifo_byte[j];
4796d66507dSDouglas Anderson 	}
4806d66507dSDouglas Anderson 	mas->rx_rem_bytes -= rx_bytes;
4816d66507dSDouglas Anderson }
4826d66507dSDouglas Anderson 
483561de45fSGirish Mahadevan static void setup_fifo_xfer(struct spi_transfer *xfer,
484561de45fSGirish Mahadevan 				struct spi_geni_master *mas,
485561de45fSGirish Mahadevan 				u16 mode, struct spi_master *spi)
486561de45fSGirish Mahadevan {
487561de45fSGirish Mahadevan 	u32 m_cmd = 0;
48814ac4e04SDouglas Anderson 	u32 len;
489561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
490e68b6624SDouglas Anderson 	int ret;
491561de45fSGirish Mahadevan 
4922ee471a1SDouglas Anderson 	/*
4932ee471a1SDouglas Anderson 	 * Ensure that our interrupt handler isn't still running from some
4942ee471a1SDouglas Anderson 	 * prior command before we start messing with the hardware behind
4952ee471a1SDouglas Anderson 	 * its back.  We don't need to _keep_ the lock here since we're only
4962ee471a1SDouglas Anderson 	 * worried about racing with out interrupt handler.  The SPI core
4972ee471a1SDouglas Anderson 	 * already handles making sure that we're not trying to do two
4982ee471a1SDouglas Anderson 	 * transfers at once or setting a chip select and doing a transfer
4992ee471a1SDouglas Anderson 	 * concurrently.
5002ee471a1SDouglas Anderson 	 *
5012ee471a1SDouglas Anderson 	 * NOTE: we actually _can't_ hold the lock here because possibly we
5022ee471a1SDouglas Anderson 	 * might call clk_set_rate() which needs to be able to sleep.
5032ee471a1SDouglas Anderson 	 */
5042ee471a1SDouglas Anderson 	spin_lock_irq(&mas->lock);
5052ee471a1SDouglas Anderson 	spin_unlock_irq(&mas->lock);
5062ee471a1SDouglas Anderson 
507561de45fSGirish Mahadevan 	if (xfer->bits_per_word != mas->cur_bits_per_word) {
508561de45fSGirish Mahadevan 		spi_setup_word_len(mas, mode, xfer->bits_per_word);
509561de45fSGirish Mahadevan 		mas->cur_bits_per_word = xfer->bits_per_word;
510561de45fSGirish Mahadevan 	}
511561de45fSGirish Mahadevan 
512561de45fSGirish Mahadevan 	/* Speed and bits per word can be overridden per transfer */
5130e3b8a81SAkash Asthana 	ret = geni_spi_set_clock_and_bw(mas, xfer->speed_hz);
514e68b6624SDouglas Anderson 	if (ret)
515561de45fSGirish Mahadevan 		return;
516561de45fSGirish Mahadevan 
517561de45fSGirish Mahadevan 	mas->tx_rem_bytes = 0;
518561de45fSGirish Mahadevan 	mas->rx_rem_bytes = 0;
519561de45fSGirish Mahadevan 
520561de45fSGirish Mahadevan 	if (!(mas->cur_bits_per_word % MIN_WORD_LEN))
521561de45fSGirish Mahadevan 		len = xfer->len * BITS_PER_BYTE / mas->cur_bits_per_word;
522561de45fSGirish Mahadevan 	else
523561de45fSGirish Mahadevan 		len = xfer->len / (mas->cur_bits_per_word / BITS_PER_BYTE + 1);
524561de45fSGirish Mahadevan 	len &= TRANS_LEN_MSK;
525561de45fSGirish Mahadevan 
526561de45fSGirish Mahadevan 	mas->cur_xfer = xfer;
52719ea3275SStephen Boyd 	if (xfer->tx_buf) {
52819ea3275SStephen Boyd 		m_cmd |= SPI_TX_ONLY;
529561de45fSGirish Mahadevan 		mas->tx_rem_bytes = xfer->len;
530561de45fSGirish Mahadevan 		writel(len, se->base + SE_SPI_TX_TRANS_LEN);
531561de45fSGirish Mahadevan 	}
532561de45fSGirish Mahadevan 
53319ea3275SStephen Boyd 	if (xfer->rx_buf) {
53419ea3275SStephen Boyd 		m_cmd |= SPI_RX_ONLY;
535561de45fSGirish Mahadevan 		writel(len, se->base + SE_SPI_RX_TRANS_LEN);
536561de45fSGirish Mahadevan 		mas->rx_rem_bytes = xfer->len;
537561de45fSGirish Mahadevan 	}
5382ee471a1SDouglas Anderson 
5392ee471a1SDouglas Anderson 	/*
5402ee471a1SDouglas Anderson 	 * Lock around right before we start the transfer since our
5412ee471a1SDouglas Anderson 	 * interrupt could come in at any time now.
5422ee471a1SDouglas Anderson 	 */
5432ee471a1SDouglas Anderson 	spin_lock_irq(&mas->lock);
544561de45fSGirish Mahadevan 	geni_se_setup_m_cmd(se, m_cmd, FRAGMENTATION);
545561de45fSGirish Mahadevan 
546561de45fSGirish Mahadevan 	/*
547561de45fSGirish Mahadevan 	 * TX_WATERMARK_REG should be set after SPI configuration and
548561de45fSGirish Mahadevan 	 * setting up GENI SE engine, as driver starts data transfer
549561de45fSGirish Mahadevan 	 * for the watermark interrupt.
550561de45fSGirish Mahadevan 	 */
5516d66507dSDouglas Anderson 	if (m_cmd & SPI_TX_ONLY) {
5526d66507dSDouglas Anderson 		if (geni_spi_handle_tx(mas))
553561de45fSGirish Mahadevan 			writel(mas->tx_wm, se->base + SE_GENI_TX_WATERMARK_REG);
5546d66507dSDouglas Anderson 	}
5552ee471a1SDouglas Anderson 	spin_unlock_irq(&mas->lock);
556561de45fSGirish Mahadevan }
557561de45fSGirish Mahadevan 
558561de45fSGirish Mahadevan static int spi_geni_transfer_one(struct spi_master *spi,
559561de45fSGirish Mahadevan 				struct spi_device *slv,
560561de45fSGirish Mahadevan 				struct spi_transfer *xfer)
561561de45fSGirish Mahadevan {
562561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
563561de45fSGirish Mahadevan 
564*690d8b91SDouglas Anderson 	if (spi_geni_is_abort_still_pending(mas))
565*690d8b91SDouglas Anderson 		return -EBUSY;
566*690d8b91SDouglas Anderson 
567561de45fSGirish Mahadevan 	/* Terminate and return success for 0 byte length transfer */
568561de45fSGirish Mahadevan 	if (!xfer->len)
569561de45fSGirish Mahadevan 		return 0;
570561de45fSGirish Mahadevan 
571561de45fSGirish Mahadevan 	setup_fifo_xfer(xfer, mas, slv->mode, spi);
572561de45fSGirish Mahadevan 	return 1;
573561de45fSGirish Mahadevan }
574561de45fSGirish Mahadevan 
575561de45fSGirish Mahadevan static irqreturn_t geni_spi_isr(int irq, void *data)
576561de45fSGirish Mahadevan {
577561de45fSGirish Mahadevan 	struct spi_master *spi = data;
578561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
579561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
580561de45fSGirish Mahadevan 	u32 m_irq;
581561de45fSGirish Mahadevan 
5822ee471a1SDouglas Anderson 	m_irq = readl(se->base + SE_GENI_M_IRQ_STATUS);
5832ee471a1SDouglas Anderson 	if (!m_irq)
584561de45fSGirish Mahadevan 		return IRQ_NONE;
585561de45fSGirish Mahadevan 
586e191a082SDouglas Anderson 	if (m_irq & (M_CMD_OVERRUN_EN | M_ILLEGAL_CMD_EN | M_CMD_FAILURE_EN |
587e191a082SDouglas Anderson 		     M_RX_FIFO_RD_ERR_EN | M_RX_FIFO_WR_ERR_EN |
588e191a082SDouglas Anderson 		     M_TX_FIFO_RD_ERR_EN | M_TX_FIFO_WR_ERR_EN))
589e191a082SDouglas Anderson 		dev_warn(mas->dev, "Unexpected IRQ err status %#010x\n", m_irq);
590e191a082SDouglas Anderson 
591539afdf9SDouglas Anderson 	spin_lock(&mas->lock);
592561de45fSGirish Mahadevan 
593561de45fSGirish Mahadevan 	if ((m_irq & M_RX_FIFO_WATERMARK_EN) || (m_irq & M_RX_FIFO_LAST_EN))
594561de45fSGirish Mahadevan 		geni_spi_handle_rx(mas);
595561de45fSGirish Mahadevan 
596561de45fSGirish Mahadevan 	if (m_irq & M_TX_FIFO_WATERMARK_EN)
597561de45fSGirish Mahadevan 		geni_spi_handle_tx(mas);
598561de45fSGirish Mahadevan 
599561de45fSGirish Mahadevan 	if (m_irq & M_CMD_DONE_EN) {
6007ba9bdcbSDouglas Anderson 		if (mas->cur_xfer) {
601561de45fSGirish Mahadevan 			spi_finalize_current_transfer(spi);
6027ba9bdcbSDouglas Anderson 			mas->cur_xfer = NULL;
603561de45fSGirish Mahadevan 			/*
60459ab0fa0SStephen Boyd 			 * If this happens, then a CMD_DONE came before all the
60559ab0fa0SStephen Boyd 			 * Tx buffer bytes were sent out. This is unusual, log
60659ab0fa0SStephen Boyd 			 * this condition and disable the WM interrupt to
60759ab0fa0SStephen Boyd 			 * prevent the system from stalling due an interrupt
60859ab0fa0SStephen Boyd 			 * storm.
60959ab0fa0SStephen Boyd 			 *
61059ab0fa0SStephen Boyd 			 * If this happens when all Rx bytes haven't been
61159ab0fa0SStephen Boyd 			 * received, log the condition. The only known time
61259ab0fa0SStephen Boyd 			 * this can happen is if bits_per_word != 8 and some
61359ab0fa0SStephen Boyd 			 * registers that expect xfer lengths in num spi_words
614561de45fSGirish Mahadevan 			 * weren't written correctly.
615561de45fSGirish Mahadevan 			 */
616561de45fSGirish Mahadevan 			if (mas->tx_rem_bytes) {
617561de45fSGirish Mahadevan 				writel(0, se->base + SE_GENI_TX_WATERMARK_REG);
618561de45fSGirish Mahadevan 				dev_err(mas->dev, "Premature done. tx_rem = %d bpw%d\n",
619561de45fSGirish Mahadevan 					mas->tx_rem_bytes, mas->cur_bits_per_word);
620561de45fSGirish Mahadevan 			}
621561de45fSGirish Mahadevan 			if (mas->rx_rem_bytes)
622561de45fSGirish Mahadevan 				dev_err(mas->dev, "Premature done. rx_rem = %d bpw%d\n",
623561de45fSGirish Mahadevan 					mas->rx_rem_bytes, mas->cur_bits_per_word);
62459ab0fa0SStephen Boyd 		} else {
62559ab0fa0SStephen Boyd 			complete(&mas->cs_done);
62659ab0fa0SStephen Boyd 		}
627561de45fSGirish Mahadevan 	}
628561de45fSGirish Mahadevan 
6297ba9bdcbSDouglas Anderson 	if (m_irq & M_CMD_CANCEL_EN)
6307ba9bdcbSDouglas Anderson 		complete(&mas->cancel_done);
6317ba9bdcbSDouglas Anderson 	if (m_irq & M_CMD_ABORT_EN)
6327ba9bdcbSDouglas Anderson 		complete(&mas->abort_done);
633561de45fSGirish Mahadevan 
6342ee471a1SDouglas Anderson 	/*
6352ee471a1SDouglas Anderson 	 * It's safe or a good idea to Ack all of our our interrupts at the
6362ee471a1SDouglas Anderson 	 * end of the function. Specifically:
6372ee471a1SDouglas Anderson 	 * - M_CMD_DONE_EN / M_RX_FIFO_LAST_EN: Edge triggered interrupts and
6382ee471a1SDouglas Anderson 	 *   clearing Acks. Clearing at the end relies on nobody else having
6392ee471a1SDouglas Anderson 	 *   started a new transfer yet or else we could be clearing _their_
6402ee471a1SDouglas Anderson 	 *   done bit, but everyone grabs the spinlock before starting a new
6412ee471a1SDouglas Anderson 	 *   transfer.
6422ee471a1SDouglas Anderson 	 * - M_RX_FIFO_WATERMARK_EN / M_TX_FIFO_WATERMARK_EN: These appear
6432ee471a1SDouglas Anderson 	 *   to be "latched level" interrupts so it's important to clear them
6442ee471a1SDouglas Anderson 	 *   _after_ you've handled the condition and always safe to do so
6452ee471a1SDouglas Anderson 	 *   since they'll re-assert if they're still happening.
6462ee471a1SDouglas Anderson 	 */
647561de45fSGirish Mahadevan 	writel(m_irq, se->base + SE_GENI_M_IRQ_CLEAR);
6482ee471a1SDouglas Anderson 
649539afdf9SDouglas Anderson 	spin_unlock(&mas->lock);
6502ee471a1SDouglas Anderson 
6510dccff3cSAlok Chauhan 	return IRQ_HANDLED;
652561de45fSGirish Mahadevan }
653561de45fSGirish Mahadevan 
654561de45fSGirish Mahadevan static int spi_geni_probe(struct platform_device *pdev)
655561de45fSGirish Mahadevan {
6566a34e285SAlok Chauhan 	int ret, irq;
657561de45fSGirish Mahadevan 	struct spi_master *spi;
658561de45fSGirish Mahadevan 	struct spi_geni_master *mas;
6596a34e285SAlok Chauhan 	void __iomem *base;
6606a34e285SAlok Chauhan 	struct clk *clk;
661ea1e5b33SStephen Boyd 	struct device *dev = &pdev->dev;
6626a34e285SAlok Chauhan 
6636a34e285SAlok Chauhan 	irq = platform_get_irq(pdev, 0);
6646b8ac10eSStephen Boyd 	if (irq < 0)
6656a34e285SAlok Chauhan 		return irq;
6666a34e285SAlok Chauhan 
667d8e477abSYueHaibing 	base = devm_platform_ioremap_resource(pdev, 0);
6686a34e285SAlok Chauhan 	if (IS_ERR(base))
6696a34e285SAlok Chauhan 		return PTR_ERR(base);
6706a34e285SAlok Chauhan 
671ea1e5b33SStephen Boyd 	clk = devm_clk_get(dev, "se");
672ea1e5b33SStephen Boyd 	if (IS_ERR(clk))
6736a34e285SAlok Chauhan 		return PTR_ERR(clk);
674561de45fSGirish Mahadevan 
6758f96c434SLukas Wunner 	spi = devm_spi_alloc_master(dev, sizeof(*mas));
676561de45fSGirish Mahadevan 	if (!spi)
677561de45fSGirish Mahadevan 		return -ENOMEM;
678561de45fSGirish Mahadevan 
679561de45fSGirish Mahadevan 	platform_set_drvdata(pdev, spi);
680561de45fSGirish Mahadevan 	mas = spi_master_get_devdata(spi);
6816a34e285SAlok Chauhan 	mas->irq = irq;
682ea1e5b33SStephen Boyd 	mas->dev = dev;
683ea1e5b33SStephen Boyd 	mas->se.dev = dev;
684ea1e5b33SStephen Boyd 	mas->se.wrapper = dev_get_drvdata(dev->parent);
6856a34e285SAlok Chauhan 	mas->se.base = base;
6866a34e285SAlok Chauhan 	mas->se.clk = clk;
6871a9e489eSRajendra Nayak 	mas->se.opp_table = dev_pm_opp_set_clkname(&pdev->dev, "se");
6881a9e489eSRajendra Nayak 	if (IS_ERR(mas->se.opp_table))
6891a9e489eSRajendra Nayak 		return PTR_ERR(mas->se.opp_table);
6901a9e489eSRajendra Nayak 	/* OPP table is optional */
6911a9e489eSRajendra Nayak 	ret = dev_pm_opp_of_add_table(&pdev->dev);
6927d568edfSViresh Kumar 	if (ret && ret != -ENODEV) {
6931a9e489eSRajendra Nayak 		dev_err(&pdev->dev, "invalid OPP table in device tree\n");
6947d568edfSViresh Kumar 		goto put_clkname;
6951a9e489eSRajendra Nayak 	}
696561de45fSGirish Mahadevan 
697561de45fSGirish Mahadevan 	spi->bus_num = -1;
698ea1e5b33SStephen Boyd 	spi->dev.of_node = dev->of_node;
699561de45fSGirish Mahadevan 	spi->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP | SPI_CS_HIGH;
700561de45fSGirish Mahadevan 	spi->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
701561de45fSGirish Mahadevan 	spi->num_chipselect = 4;
702561de45fSGirish Mahadevan 	spi->max_speed_hz = 50000000;
703561de45fSGirish Mahadevan 	spi->prepare_message = spi_geni_prepare_message;
704561de45fSGirish Mahadevan 	spi->transfer_one = spi_geni_transfer_one;
705561de45fSGirish Mahadevan 	spi->auto_runtime_pm = true;
706561de45fSGirish Mahadevan 	spi->handle_err = handle_fifo_timeout;
707561de45fSGirish Mahadevan 	spi->set_cs = spi_geni_set_cs;
7083b25f337SStephen Boyd 	spi->use_gpio_descriptors = true;
709561de45fSGirish Mahadevan 
7107ba9bdcbSDouglas Anderson 	init_completion(&mas->cs_done);
7117ba9bdcbSDouglas Anderson 	init_completion(&mas->cancel_done);
7127ba9bdcbSDouglas Anderson 	init_completion(&mas->abort_done);
713561de45fSGirish Mahadevan 	spin_lock_init(&mas->lock);
714cfdab2cdSDouglas Anderson 	pm_runtime_use_autosuspend(&pdev->dev);
715cfdab2cdSDouglas Anderson 	pm_runtime_set_autosuspend_delay(&pdev->dev, 250);
716ea1e5b33SStephen Boyd 	pm_runtime_enable(dev);
717561de45fSGirish Mahadevan 
7180e3b8a81SAkash Asthana 	ret = geni_icc_get(&mas->se, NULL);
7190e3b8a81SAkash Asthana 	if (ret)
7200e3b8a81SAkash Asthana 		goto spi_geni_probe_runtime_disable;
7210e3b8a81SAkash Asthana 	/* Set the bus quota to a reasonable value for register access */
7220e3b8a81SAkash Asthana 	mas->se.icc_paths[GENI_TO_CORE].avg_bw = Bps_to_icc(CORE_2X_50_MHZ);
7230e3b8a81SAkash Asthana 	mas->se.icc_paths[CPU_TO_GENI].avg_bw = GENI_DEFAULT_BW;
7240e3b8a81SAkash Asthana 
7250e3b8a81SAkash Asthana 	ret = geni_icc_set_bw(&mas->se);
7260e3b8a81SAkash Asthana 	if (ret)
7270e3b8a81SAkash Asthana 		goto spi_geni_probe_runtime_disable;
7280e3b8a81SAkash Asthana 
729561de45fSGirish Mahadevan 	ret = spi_geni_init(mas);
730561de45fSGirish Mahadevan 	if (ret)
731561de45fSGirish Mahadevan 		goto spi_geni_probe_runtime_disable;
732561de45fSGirish Mahadevan 
733ea1e5b33SStephen Boyd 	ret = request_irq(mas->irq, geni_spi_isr, 0, dev_name(dev), spi);
734561de45fSGirish Mahadevan 	if (ret)
735561de45fSGirish Mahadevan 		goto spi_geni_probe_runtime_disable;
736561de45fSGirish Mahadevan 
737561de45fSGirish Mahadevan 	ret = spi_register_master(spi);
738561de45fSGirish Mahadevan 	if (ret)
739561de45fSGirish Mahadevan 		goto spi_geni_probe_free_irq;
740561de45fSGirish Mahadevan 
741561de45fSGirish Mahadevan 	return 0;
742561de45fSGirish Mahadevan spi_geni_probe_free_irq:
743561de45fSGirish Mahadevan 	free_irq(mas->irq, spi);
744561de45fSGirish Mahadevan spi_geni_probe_runtime_disable:
745ea1e5b33SStephen Boyd 	pm_runtime_disable(dev);
7461a9e489eSRajendra Nayak 	dev_pm_opp_of_remove_table(&pdev->dev);
7477d568edfSViresh Kumar put_clkname:
7481a9e489eSRajendra Nayak 	dev_pm_opp_put_clkname(mas->se.opp_table);
749561de45fSGirish Mahadevan 	return ret;
750561de45fSGirish Mahadevan }
751561de45fSGirish Mahadevan 
752561de45fSGirish Mahadevan static int spi_geni_remove(struct platform_device *pdev)
753561de45fSGirish Mahadevan {
754561de45fSGirish Mahadevan 	struct spi_master *spi = platform_get_drvdata(pdev);
755561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
756561de45fSGirish Mahadevan 
757561de45fSGirish Mahadevan 	/* Unregister _before_ disabling pm_runtime() so we stop transfers */
758561de45fSGirish Mahadevan 	spi_unregister_master(spi);
759561de45fSGirish Mahadevan 
760561de45fSGirish Mahadevan 	free_irq(mas->irq, spi);
761561de45fSGirish Mahadevan 	pm_runtime_disable(&pdev->dev);
7621a9e489eSRajendra Nayak 	dev_pm_opp_of_remove_table(&pdev->dev);
7631a9e489eSRajendra Nayak 	dev_pm_opp_put_clkname(mas->se.opp_table);
764561de45fSGirish Mahadevan 	return 0;
765561de45fSGirish Mahadevan }
766561de45fSGirish Mahadevan 
767561de45fSGirish Mahadevan static int __maybe_unused spi_geni_runtime_suspend(struct device *dev)
768561de45fSGirish Mahadevan {
769561de45fSGirish Mahadevan 	struct spi_master *spi = dev_get_drvdata(dev);
770561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
7710e3b8a81SAkash Asthana 	int ret;
772561de45fSGirish Mahadevan 
7731a9e489eSRajendra Nayak 	/* Drop the performance state vote */
7741a9e489eSRajendra Nayak 	dev_pm_opp_set_rate(dev, 0);
7751a9e489eSRajendra Nayak 
7760e3b8a81SAkash Asthana 	ret = geni_se_resources_off(&mas->se);
7770e3b8a81SAkash Asthana 	if (ret)
7780e3b8a81SAkash Asthana 		return ret;
7790e3b8a81SAkash Asthana 
7800e3b8a81SAkash Asthana 	return geni_icc_disable(&mas->se);
781561de45fSGirish Mahadevan }
782561de45fSGirish Mahadevan 
783561de45fSGirish Mahadevan static int __maybe_unused spi_geni_runtime_resume(struct device *dev)
784561de45fSGirish Mahadevan {
785561de45fSGirish Mahadevan 	struct spi_master *spi = dev_get_drvdata(dev);
786561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
7870e3b8a81SAkash Asthana 	int ret;
7880e3b8a81SAkash Asthana 
7890e3b8a81SAkash Asthana 	ret = geni_icc_enable(&mas->se);
7900e3b8a81SAkash Asthana 	if (ret)
7910e3b8a81SAkash Asthana 		return ret;
792561de45fSGirish Mahadevan 
7935f219524SDouglas Anderson 	ret = geni_se_resources_on(&mas->se);
7945f219524SDouglas Anderson 	if (ret)
7955f219524SDouglas Anderson 		return ret;
7965f219524SDouglas Anderson 
7975f219524SDouglas Anderson 	return dev_pm_opp_set_rate(mas->dev, mas->cur_sclk_hz);
798561de45fSGirish Mahadevan }
799561de45fSGirish Mahadevan 
800561de45fSGirish Mahadevan static int __maybe_unused spi_geni_suspend(struct device *dev)
801561de45fSGirish Mahadevan {
802561de45fSGirish Mahadevan 	struct spi_master *spi = dev_get_drvdata(dev);
803561de45fSGirish Mahadevan 	int ret;
804561de45fSGirish Mahadevan 
805561de45fSGirish Mahadevan 	ret = spi_master_suspend(spi);
806561de45fSGirish Mahadevan 	if (ret)
807561de45fSGirish Mahadevan 		return ret;
808561de45fSGirish Mahadevan 
809561de45fSGirish Mahadevan 	ret = pm_runtime_force_suspend(dev);
810561de45fSGirish Mahadevan 	if (ret)
811561de45fSGirish Mahadevan 		spi_master_resume(spi);
812561de45fSGirish Mahadevan 
813561de45fSGirish Mahadevan 	return ret;
814561de45fSGirish Mahadevan }
815561de45fSGirish Mahadevan 
816561de45fSGirish Mahadevan static int __maybe_unused spi_geni_resume(struct device *dev)
817561de45fSGirish Mahadevan {
818561de45fSGirish Mahadevan 	struct spi_master *spi = dev_get_drvdata(dev);
819561de45fSGirish Mahadevan 	int ret;
820561de45fSGirish Mahadevan 
821561de45fSGirish Mahadevan 	ret = pm_runtime_force_resume(dev);
822561de45fSGirish Mahadevan 	if (ret)
823561de45fSGirish Mahadevan 		return ret;
824561de45fSGirish Mahadevan 
825561de45fSGirish Mahadevan 	ret = spi_master_resume(spi);
826561de45fSGirish Mahadevan 	if (ret)
827561de45fSGirish Mahadevan 		pm_runtime_force_suspend(dev);
828561de45fSGirish Mahadevan 
829561de45fSGirish Mahadevan 	return ret;
830561de45fSGirish Mahadevan }
831561de45fSGirish Mahadevan 
832561de45fSGirish Mahadevan static const struct dev_pm_ops spi_geni_pm_ops = {
833561de45fSGirish Mahadevan 	SET_RUNTIME_PM_OPS(spi_geni_runtime_suspend,
834561de45fSGirish Mahadevan 					spi_geni_runtime_resume, NULL)
835561de45fSGirish Mahadevan 	SET_SYSTEM_SLEEP_PM_OPS(spi_geni_suspend, spi_geni_resume)
836561de45fSGirish Mahadevan };
837561de45fSGirish Mahadevan 
838561de45fSGirish Mahadevan static const struct of_device_id spi_geni_dt_match[] = {
839561de45fSGirish Mahadevan 	{ .compatible = "qcom,geni-spi" },
840561de45fSGirish Mahadevan 	{}
841561de45fSGirish Mahadevan };
842561de45fSGirish Mahadevan MODULE_DEVICE_TABLE(of, spi_geni_dt_match);
843561de45fSGirish Mahadevan 
844561de45fSGirish Mahadevan static struct platform_driver spi_geni_driver = {
845561de45fSGirish Mahadevan 	.probe  = spi_geni_probe,
846561de45fSGirish Mahadevan 	.remove = spi_geni_remove,
847561de45fSGirish Mahadevan 	.driver = {
848561de45fSGirish Mahadevan 		.name = "geni_spi",
849561de45fSGirish Mahadevan 		.pm = &spi_geni_pm_ops,
850561de45fSGirish Mahadevan 		.of_match_table = spi_geni_dt_match,
851561de45fSGirish Mahadevan 	},
852561de45fSGirish Mahadevan };
853561de45fSGirish Mahadevan module_platform_driver(spi_geni_driver);
854561de45fSGirish Mahadevan 
855561de45fSGirish Mahadevan MODULE_DESCRIPTION("SPI driver for GENI based QUP cores");
856561de45fSGirish Mahadevan MODULE_LICENSE("GPL v2");
857