xref: /linux/drivers/spi/spi-geni-qcom.c (revision db56d03049524114696aa7158560d8f0e064c487)
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;
86690d8b91SDouglas 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);
145690d8b91SDouglas Anderson 	if (!time_left) {
146de43affeSStephen Boyd 		dev_err(mas->dev, "Failed to cancel/abort m_cmd\n");
147690d8b91SDouglas Anderson 
148690d8b91SDouglas Anderson 		/*
149690d8b91SDouglas Anderson 		 * No need for a lock since SPI core has a lock and we never
150690d8b91SDouglas Anderson 		 * access this from an interrupt.
151690d8b91SDouglas Anderson 		 */
152690d8b91SDouglas Anderson 		mas->abort_failed = true;
153690d8b91SDouglas Anderson 	}
154690d8b91SDouglas Anderson }
155690d8b91SDouglas Anderson 
156690d8b91SDouglas Anderson static bool spi_geni_is_abort_still_pending(struct spi_geni_master *mas)
157690d8b91SDouglas Anderson {
158690d8b91SDouglas Anderson 	struct geni_se *se = &mas->se;
159690d8b91SDouglas Anderson 	u32 m_irq, m_irq_en;
160690d8b91SDouglas Anderson 
161690d8b91SDouglas Anderson 	if (!mas->abort_failed)
162690d8b91SDouglas Anderson 		return false;
163690d8b91SDouglas Anderson 
164690d8b91SDouglas Anderson 	/*
165690d8b91SDouglas Anderson 	 * The only known case where a transfer times out and then a cancel
166690d8b91SDouglas Anderson 	 * times out then an abort times out is if something is blocking our
167690d8b91SDouglas Anderson 	 * interrupt handler from running.  Avoid starting any new transfers
168690d8b91SDouglas Anderson 	 * until that sorts itself out.
169690d8b91SDouglas Anderson 	 */
170690d8b91SDouglas Anderson 	spin_lock_irq(&mas->lock);
171690d8b91SDouglas Anderson 	m_irq = readl(se->base + SE_GENI_M_IRQ_STATUS);
172690d8b91SDouglas Anderson 	m_irq_en = readl(se->base + SE_GENI_M_IRQ_EN);
173690d8b91SDouglas Anderson 	spin_unlock_irq(&mas->lock);
174690d8b91SDouglas Anderson 
175690d8b91SDouglas Anderson 	if (m_irq & m_irq_en) {
176690d8b91SDouglas Anderson 		dev_err(mas->dev, "Interrupts pending after abort: %#010x\n",
177690d8b91SDouglas Anderson 			m_irq & m_irq_en);
178690d8b91SDouglas Anderson 		return true;
179690d8b91SDouglas Anderson 	}
180690d8b91SDouglas Anderson 
181690d8b91SDouglas Anderson 	/*
182690d8b91SDouglas Anderson 	 * If we're here the problem resolved itself so no need to check more
183690d8b91SDouglas Anderson 	 * on future transfers.
184690d8b91SDouglas Anderson 	 */
185690d8b91SDouglas Anderson 	mas->abort_failed = false;
186690d8b91SDouglas Anderson 
187690d8b91SDouglas 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 
203690d8b91SDouglas Anderson 	pm_runtime_get_sync(mas->dev);
204690d8b91SDouglas Anderson 
205690d8b91SDouglas Anderson 	if (spi_geni_is_abort_still_pending(mas)) {
206690d8b91SDouglas Anderson 		dev_err(mas->dev, "Can't set chip select\n");
207690d8b91SDouglas Anderson 		goto exit;
208690d8b91SDouglas Anderson 	}
209690d8b91SDouglas Anderson 
2102ee471a1SDouglas Anderson 	spin_lock_irq(&mas->lock);
2113d7d916fSDouglas Anderson 	if (mas->cur_xfer) {
2123d7d916fSDouglas Anderson 		dev_err(mas->dev, "Can't set CS when prev xfer running\n");
2133d7d916fSDouglas Anderson 		spin_unlock_irq(&mas->lock);
2143d7d916fSDouglas Anderson 		goto exit;
2153d7d916fSDouglas Anderson 	}
2163d7d916fSDouglas Anderson 
2173d7d916fSDouglas Anderson 	mas->cs_flag = set_flag;
2187ba9bdcbSDouglas Anderson 	reinit_completion(&mas->cs_done);
219561de45fSGirish Mahadevan 	if (set_flag)
220561de45fSGirish Mahadevan 		geni_se_setup_m_cmd(se, SPI_CS_ASSERT, 0);
221561de45fSGirish Mahadevan 	else
222561de45fSGirish Mahadevan 		geni_se_setup_m_cmd(se, SPI_CS_DEASSERT, 0);
2232ee471a1SDouglas Anderson 	spin_unlock_irq(&mas->lock);
224561de45fSGirish Mahadevan 
2257ba9bdcbSDouglas Anderson 	time_left = wait_for_completion_timeout(&mas->cs_done, HZ);
22617fa81aaSDouglas Anderson 	if (!time_left) {
22717fa81aaSDouglas Anderson 		dev_warn(mas->dev, "Timeout setting chip select\n");
228561de45fSGirish Mahadevan 		handle_fifo_timeout(spi, NULL);
22917fa81aaSDouglas Anderson 	}
230561de45fSGirish Mahadevan 
231690d8b91SDouglas Anderson exit:
232561de45fSGirish Mahadevan 	pm_runtime_put(mas->dev);
233561de45fSGirish Mahadevan }
234561de45fSGirish Mahadevan 
235561de45fSGirish Mahadevan static void spi_setup_word_len(struct spi_geni_master *mas, u16 mode,
236561de45fSGirish Mahadevan 					unsigned int bits_per_word)
237561de45fSGirish Mahadevan {
238561de45fSGirish Mahadevan 	unsigned int pack_words;
239561de45fSGirish Mahadevan 	bool msb_first = (mode & SPI_LSB_FIRST) ? false : true;
240561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
241561de45fSGirish Mahadevan 	u32 word_len;
242561de45fSGirish Mahadevan 
243561de45fSGirish Mahadevan 	/*
244561de45fSGirish Mahadevan 	 * If bits_per_word isn't a byte aligned value, set the packing to be
245561de45fSGirish Mahadevan 	 * 1 SPI word per FIFO word.
246561de45fSGirish Mahadevan 	 */
247561de45fSGirish Mahadevan 	if (!(mas->fifo_width_bits % bits_per_word))
248561de45fSGirish Mahadevan 		pack_words = mas->fifo_width_bits / bits_per_word;
249561de45fSGirish Mahadevan 	else
250561de45fSGirish Mahadevan 		pack_words = 1;
251561de45fSGirish Mahadevan 	geni_se_config_packing(&mas->se, bits_per_word, pack_words, msb_first,
252561de45fSGirish Mahadevan 								true, true);
253da48dc8cSDouglas Anderson 	word_len = (bits_per_word - MIN_WORD_LEN) & WORD_LEN_MSK;
254561de45fSGirish Mahadevan 	writel(word_len, se->base + SE_SPI_WORD_LEN);
255561de45fSGirish Mahadevan }
256561de45fSGirish Mahadevan 
2570e3b8a81SAkash Asthana static int geni_spi_set_clock_and_bw(struct spi_geni_master *mas,
2580e3b8a81SAkash Asthana 					unsigned long clk_hz)
259e68b6624SDouglas Anderson {
260e68b6624SDouglas Anderson 	u32 clk_sel, m_clk_cfg, idx, div;
261e68b6624SDouglas Anderson 	struct geni_se *se = &mas->se;
262e68b6624SDouglas Anderson 	int ret;
263e68b6624SDouglas Anderson 
26468890e20SDouglas Anderson 	if (clk_hz == mas->cur_speed_hz)
26568890e20SDouglas Anderson 		return 0;
26668890e20SDouglas Anderson 
267e68b6624SDouglas Anderson 	ret = get_spi_clk_cfg(clk_hz, mas, &idx, &div);
268e68b6624SDouglas Anderson 	if (ret) {
269e68b6624SDouglas Anderson 		dev_err(mas->dev, "Err setting clk to %lu: %d\n", clk_hz, ret);
270e68b6624SDouglas Anderson 		return ret;
271e68b6624SDouglas Anderson 	}
272e68b6624SDouglas Anderson 
273e68b6624SDouglas Anderson 	/*
274e68b6624SDouglas Anderson 	 * SPI core clock gets configured with the requested frequency
275e68b6624SDouglas Anderson 	 * or the frequency closer to the requested frequency.
276e68b6624SDouglas Anderson 	 * For that reason requested frequency is stored in the
277e68b6624SDouglas Anderson 	 * cur_speed_hz and referred in the consecutive transfer instead
278e68b6624SDouglas Anderson 	 * of calling clk_get_rate() API.
279e68b6624SDouglas Anderson 	 */
280e68b6624SDouglas Anderson 	mas->cur_speed_hz = clk_hz;
281e68b6624SDouglas Anderson 
282e68b6624SDouglas Anderson 	clk_sel = idx & CLK_SEL_MSK;
283e68b6624SDouglas Anderson 	m_clk_cfg = (div << CLK_DIV_SHFT) | SER_CLK_EN;
284e68b6624SDouglas Anderson 	writel(clk_sel, se->base + SE_GENI_CLK_SEL);
285e68b6624SDouglas Anderson 	writel(m_clk_cfg, se->base + GENI_SER_M_CLK_CFG);
286e68b6624SDouglas Anderson 
2870e3b8a81SAkash Asthana 	/* Set BW quota for CPU as driver supports FIFO mode only. */
2880e3b8a81SAkash Asthana 	se->icc_paths[CPU_TO_GENI].avg_bw = Bps_to_icc(mas->cur_speed_hz);
2890e3b8a81SAkash Asthana 	ret = geni_icc_set_bw(se);
2900e3b8a81SAkash Asthana 	if (ret)
2910e3b8a81SAkash Asthana 		return ret;
2920e3b8a81SAkash Asthana 
293e68b6624SDouglas Anderson 	return 0;
294e68b6624SDouglas Anderson }
295e68b6624SDouglas Anderson 
296561de45fSGirish Mahadevan static int setup_fifo_params(struct spi_device *spi_slv,
297561de45fSGirish Mahadevan 					struct spi_master *spi)
298561de45fSGirish Mahadevan {
299561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
300561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
301da48dc8cSDouglas Anderson 	u32 loopback_cfg = 0, cpol = 0, cpha = 0, demux_output_inv = 0;
302e68b6624SDouglas Anderson 	u32 demux_sel;
303561de45fSGirish Mahadevan 
304da48dc8cSDouglas Anderson 	if (mas->last_mode != spi_slv->mode) {
305561de45fSGirish Mahadevan 		if (spi_slv->mode & SPI_LOOP)
306da48dc8cSDouglas Anderson 			loopback_cfg = LOOPBACK_ENABLE;
307561de45fSGirish Mahadevan 
308561de45fSGirish Mahadevan 		if (spi_slv->mode & SPI_CPOL)
309da48dc8cSDouglas Anderson 			cpol = CPOL;
310561de45fSGirish Mahadevan 
311561de45fSGirish Mahadevan 		if (spi_slv->mode & SPI_CPHA)
312da48dc8cSDouglas Anderson 			cpha = CPHA;
313561de45fSGirish Mahadevan 
314561de45fSGirish Mahadevan 		if (spi_slv->mode & SPI_CS_HIGH)
315561de45fSGirish Mahadevan 			demux_output_inv = BIT(spi_slv->chip_select);
316561de45fSGirish Mahadevan 
317561de45fSGirish Mahadevan 		demux_sel = spi_slv->chip_select;
318561de45fSGirish Mahadevan 		mas->cur_bits_per_word = spi_slv->bits_per_word;
319561de45fSGirish Mahadevan 
320561de45fSGirish Mahadevan 		spi_setup_word_len(mas, spi_slv->mode, spi_slv->bits_per_word);
321561de45fSGirish Mahadevan 		writel(loopback_cfg, se->base + SE_SPI_LOOPBACK);
322561de45fSGirish Mahadevan 		writel(demux_sel, se->base + SE_SPI_DEMUX_SEL);
323561de45fSGirish Mahadevan 		writel(cpha, se->base + SE_SPI_CPHA);
324561de45fSGirish Mahadevan 		writel(cpol, se->base + SE_SPI_CPOL);
325561de45fSGirish Mahadevan 		writel(demux_output_inv, se->base + SE_SPI_DEMUX_OUTPUT_INV);
326e68b6624SDouglas Anderson 
327da48dc8cSDouglas Anderson 		mas->last_mode = spi_slv->mode;
328da48dc8cSDouglas Anderson 	}
329da48dc8cSDouglas Anderson 
3300e3b8a81SAkash Asthana 	return geni_spi_set_clock_and_bw(mas, spi_slv->max_speed_hz);
331561de45fSGirish Mahadevan }
332561de45fSGirish Mahadevan 
333561de45fSGirish Mahadevan static int spi_geni_prepare_message(struct spi_master *spi,
334561de45fSGirish Mahadevan 					struct spi_message *spi_msg)
335561de45fSGirish Mahadevan {
336561de45fSGirish Mahadevan 	int ret;
337561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
338561de45fSGirish Mahadevan 
339690d8b91SDouglas Anderson 	if (spi_geni_is_abort_still_pending(mas))
340690d8b91SDouglas Anderson 		return -EBUSY;
341690d8b91SDouglas Anderson 
342561de45fSGirish Mahadevan 	ret = setup_fifo_params(spi_msg->spi, spi);
343561de45fSGirish Mahadevan 	if (ret)
344561de45fSGirish Mahadevan 		dev_err(mas->dev, "Couldn't select mode %d\n", ret);
345561de45fSGirish Mahadevan 	return ret;
346561de45fSGirish Mahadevan }
347561de45fSGirish Mahadevan 
348561de45fSGirish Mahadevan static int spi_geni_init(struct spi_geni_master *mas)
349561de45fSGirish Mahadevan {
350561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
351561de45fSGirish Mahadevan 	unsigned int proto, major, minor, ver;
35214ac4e04SDouglas Anderson 	u32 spi_tx_cfg;
353561de45fSGirish Mahadevan 
354561de45fSGirish Mahadevan 	pm_runtime_get_sync(mas->dev);
355561de45fSGirish Mahadevan 
356561de45fSGirish Mahadevan 	proto = geni_se_read_proto(se);
357561de45fSGirish Mahadevan 	if (proto != GENI_SE_SPI) {
358561de45fSGirish Mahadevan 		dev_err(mas->dev, "Invalid proto %d\n", proto);
359561de45fSGirish Mahadevan 		pm_runtime_put(mas->dev);
360561de45fSGirish Mahadevan 		return -ENXIO;
361561de45fSGirish Mahadevan 	}
362561de45fSGirish Mahadevan 	mas->tx_fifo_depth = geni_se_get_tx_fifo_depth(se);
363561de45fSGirish Mahadevan 
364561de45fSGirish Mahadevan 	/* Width of Tx and Rx FIFO is same */
365561de45fSGirish Mahadevan 	mas->fifo_width_bits = geni_se_get_tx_fifo_width(se);
366561de45fSGirish Mahadevan 
367561de45fSGirish Mahadevan 	/*
368561de45fSGirish Mahadevan 	 * Hardware programming guide suggests to configure
369561de45fSGirish Mahadevan 	 * RX FIFO RFR level to fifo_depth-2.
370561de45fSGirish Mahadevan 	 */
371fc129a43SDouglas Anderson 	geni_se_init(se, mas->tx_fifo_depth - 3, mas->tx_fifo_depth - 2);
372561de45fSGirish Mahadevan 	/* Transmit an entire FIFO worth of data per IRQ */
373561de45fSGirish Mahadevan 	mas->tx_wm = 1;
374561de45fSGirish Mahadevan 	ver = geni_se_get_qup_hw_version(se);
375561de45fSGirish Mahadevan 	major = GENI_SE_VERSION_MAJOR(ver);
376561de45fSGirish Mahadevan 	minor = GENI_SE_VERSION_MINOR(ver);
377561de45fSGirish Mahadevan 
378561de45fSGirish Mahadevan 	if (major == 1 && minor == 0)
379561de45fSGirish Mahadevan 		mas->oversampling = 2;
380561de45fSGirish Mahadevan 	else
381561de45fSGirish Mahadevan 		mas->oversampling = 1;
382561de45fSGirish Mahadevan 
383da48dc8cSDouglas Anderson 	geni_se_select_mode(se, GENI_SE_FIFO);
384da48dc8cSDouglas Anderson 
38514ac4e04SDouglas Anderson 	/* We always control CS manually */
38614ac4e04SDouglas Anderson 	spi_tx_cfg = readl(se->base + SE_SPI_TRANS_CFG);
38714ac4e04SDouglas Anderson 	spi_tx_cfg &= ~CS_TOGGLE;
38814ac4e04SDouglas Anderson 	writel(spi_tx_cfg, se->base + SE_SPI_TRANS_CFG);
38914ac4e04SDouglas Anderson 
390561de45fSGirish Mahadevan 	pm_runtime_put(mas->dev);
391561de45fSGirish Mahadevan 	return 0;
392561de45fSGirish Mahadevan }
393561de45fSGirish Mahadevan 
3946d66507dSDouglas Anderson static unsigned int geni_byte_per_fifo_word(struct spi_geni_master *mas)
3956d66507dSDouglas Anderson {
3966d66507dSDouglas Anderson 	/*
3976d66507dSDouglas Anderson 	 * Calculate how many bytes we'll put in each FIFO word.  If the
3986d66507dSDouglas Anderson 	 * transfer words don't pack cleanly into a FIFO word we'll just put
3996d66507dSDouglas Anderson 	 * one transfer word in each FIFO word.  If they do pack we'll pack 'em.
4006d66507dSDouglas Anderson 	 */
4016d66507dSDouglas Anderson 	if (mas->fifo_width_bits % mas->cur_bits_per_word)
4026d66507dSDouglas Anderson 		return roundup_pow_of_two(DIV_ROUND_UP(mas->cur_bits_per_word,
4036d66507dSDouglas Anderson 						       BITS_PER_BYTE));
4046d66507dSDouglas Anderson 
4056d66507dSDouglas Anderson 	return mas->fifo_width_bits / BITS_PER_BYTE;
4066d66507dSDouglas Anderson }
4076d66507dSDouglas Anderson 
4086d66507dSDouglas Anderson static bool geni_spi_handle_tx(struct spi_geni_master *mas)
4096d66507dSDouglas Anderson {
4106d66507dSDouglas Anderson 	struct geni_se *se = &mas->se;
4116d66507dSDouglas Anderson 	unsigned int max_bytes;
4126d66507dSDouglas Anderson 	const u8 *tx_buf;
4136d66507dSDouglas Anderson 	unsigned int bytes_per_fifo_word = geni_byte_per_fifo_word(mas);
4146d66507dSDouglas Anderson 	unsigned int i = 0;
4156d66507dSDouglas Anderson 
4164aa1464aSDouglas Anderson 	/* Stop the watermark IRQ if nothing to send */
4174aa1464aSDouglas Anderson 	if (!mas->cur_xfer) {
4184aa1464aSDouglas Anderson 		writel(0, se->base + SE_GENI_TX_WATERMARK_REG);
4194aa1464aSDouglas Anderson 		return false;
4204aa1464aSDouglas Anderson 	}
4214aa1464aSDouglas Anderson 
4226d66507dSDouglas Anderson 	max_bytes = (mas->tx_fifo_depth - mas->tx_wm) * bytes_per_fifo_word;
4236d66507dSDouglas Anderson 	if (mas->tx_rem_bytes < max_bytes)
4246d66507dSDouglas Anderson 		max_bytes = mas->tx_rem_bytes;
4256d66507dSDouglas Anderson 
4266d66507dSDouglas Anderson 	tx_buf = mas->cur_xfer->tx_buf + mas->cur_xfer->len - mas->tx_rem_bytes;
4276d66507dSDouglas Anderson 	while (i < max_bytes) {
4286d66507dSDouglas Anderson 		unsigned int j;
4296d66507dSDouglas Anderson 		unsigned int bytes_to_write;
4306d66507dSDouglas Anderson 		u32 fifo_word = 0;
4316d66507dSDouglas Anderson 		u8 *fifo_byte = (u8 *)&fifo_word;
4326d66507dSDouglas Anderson 
4336d66507dSDouglas Anderson 		bytes_to_write = min(bytes_per_fifo_word, max_bytes - i);
4346d66507dSDouglas Anderson 		for (j = 0; j < bytes_to_write; j++)
4356d66507dSDouglas Anderson 			fifo_byte[j] = tx_buf[i++];
4366d66507dSDouglas Anderson 		iowrite32_rep(se->base + SE_GENI_TX_FIFOn, &fifo_word, 1);
4376d66507dSDouglas Anderson 	}
4386d66507dSDouglas Anderson 	mas->tx_rem_bytes -= max_bytes;
4396d66507dSDouglas Anderson 	if (!mas->tx_rem_bytes) {
4406d66507dSDouglas Anderson 		writel(0, se->base + SE_GENI_TX_WATERMARK_REG);
4416d66507dSDouglas Anderson 		return false;
4426d66507dSDouglas Anderson 	}
4436d66507dSDouglas Anderson 	return true;
4446d66507dSDouglas Anderson }
4456d66507dSDouglas Anderson 
4466d66507dSDouglas Anderson static void geni_spi_handle_rx(struct spi_geni_master *mas)
4476d66507dSDouglas Anderson {
4486d66507dSDouglas Anderson 	struct geni_se *se = &mas->se;
4496d66507dSDouglas Anderson 	u32 rx_fifo_status;
4506d66507dSDouglas Anderson 	unsigned int rx_bytes;
4516d66507dSDouglas Anderson 	unsigned int rx_last_byte_valid;
4526d66507dSDouglas Anderson 	u8 *rx_buf;
4536d66507dSDouglas Anderson 	unsigned int bytes_per_fifo_word = geni_byte_per_fifo_word(mas);
4546d66507dSDouglas Anderson 	unsigned int i = 0;
4556d66507dSDouglas Anderson 
4566d66507dSDouglas Anderson 	rx_fifo_status = readl(se->base + SE_GENI_RX_FIFO_STATUS);
4576d66507dSDouglas Anderson 	rx_bytes = (rx_fifo_status & RX_FIFO_WC_MSK) * bytes_per_fifo_word;
4586d66507dSDouglas Anderson 	if (rx_fifo_status & RX_LAST) {
4596d66507dSDouglas Anderson 		rx_last_byte_valid = rx_fifo_status & RX_LAST_BYTE_VALID_MSK;
4606d66507dSDouglas Anderson 		rx_last_byte_valid >>= RX_LAST_BYTE_VALID_SHFT;
4616d66507dSDouglas Anderson 		if (rx_last_byte_valid && rx_last_byte_valid < 4)
4626d66507dSDouglas Anderson 			rx_bytes -= bytes_per_fifo_word - rx_last_byte_valid;
4636d66507dSDouglas Anderson 	}
4644aa1464aSDouglas Anderson 
4654aa1464aSDouglas Anderson 	/* Clear out the FIFO and bail if nowhere to put it */
4664aa1464aSDouglas Anderson 	if (!mas->cur_xfer) {
4674aa1464aSDouglas Anderson 		for (i = 0; i < DIV_ROUND_UP(rx_bytes, bytes_per_fifo_word); i++)
4684aa1464aSDouglas Anderson 			readl(se->base + SE_GENI_RX_FIFOn);
4694aa1464aSDouglas Anderson 		return;
4704aa1464aSDouglas Anderson 	}
4714aa1464aSDouglas Anderson 
4726d66507dSDouglas Anderson 	if (mas->rx_rem_bytes < rx_bytes)
4736d66507dSDouglas Anderson 		rx_bytes = mas->rx_rem_bytes;
4746d66507dSDouglas Anderson 
4756d66507dSDouglas Anderson 	rx_buf = mas->cur_xfer->rx_buf + mas->cur_xfer->len - mas->rx_rem_bytes;
4766d66507dSDouglas Anderson 	while (i < rx_bytes) {
4776d66507dSDouglas Anderson 		u32 fifo_word = 0;
4786d66507dSDouglas Anderson 		u8 *fifo_byte = (u8 *)&fifo_word;
4796d66507dSDouglas Anderson 		unsigned int bytes_to_read;
4806d66507dSDouglas Anderson 		unsigned int j;
4816d66507dSDouglas Anderson 
4826d66507dSDouglas Anderson 		bytes_to_read = min(bytes_per_fifo_word, rx_bytes - i);
4836d66507dSDouglas Anderson 		ioread32_rep(se->base + SE_GENI_RX_FIFOn, &fifo_word, 1);
4846d66507dSDouglas Anderson 		for (j = 0; j < bytes_to_read; j++)
4856d66507dSDouglas Anderson 			rx_buf[i++] = fifo_byte[j];
4866d66507dSDouglas Anderson 	}
4876d66507dSDouglas Anderson 	mas->rx_rem_bytes -= rx_bytes;
4886d66507dSDouglas Anderson }
4896d66507dSDouglas Anderson 
490561de45fSGirish Mahadevan static void setup_fifo_xfer(struct spi_transfer *xfer,
491561de45fSGirish Mahadevan 				struct spi_geni_master *mas,
492561de45fSGirish Mahadevan 				u16 mode, struct spi_master *spi)
493561de45fSGirish Mahadevan {
494561de45fSGirish Mahadevan 	u32 m_cmd = 0;
49514ac4e04SDouglas Anderson 	u32 len;
496561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
497e68b6624SDouglas Anderson 	int ret;
498561de45fSGirish Mahadevan 
4992ee471a1SDouglas Anderson 	/*
5002ee471a1SDouglas Anderson 	 * Ensure that our interrupt handler isn't still running from some
5012ee471a1SDouglas Anderson 	 * prior command before we start messing with the hardware behind
5022ee471a1SDouglas Anderson 	 * its back.  We don't need to _keep_ the lock here since we're only
5032ee471a1SDouglas Anderson 	 * worried about racing with out interrupt handler.  The SPI core
5042ee471a1SDouglas Anderson 	 * already handles making sure that we're not trying to do two
5052ee471a1SDouglas Anderson 	 * transfers at once or setting a chip select and doing a transfer
5062ee471a1SDouglas Anderson 	 * concurrently.
5072ee471a1SDouglas Anderson 	 *
5082ee471a1SDouglas Anderson 	 * NOTE: we actually _can't_ hold the lock here because possibly we
5092ee471a1SDouglas Anderson 	 * might call clk_set_rate() which needs to be able to sleep.
5102ee471a1SDouglas Anderson 	 */
5112ee471a1SDouglas Anderson 	spin_lock_irq(&mas->lock);
5122ee471a1SDouglas Anderson 	spin_unlock_irq(&mas->lock);
5132ee471a1SDouglas Anderson 
514561de45fSGirish Mahadevan 	if (xfer->bits_per_word != mas->cur_bits_per_word) {
515561de45fSGirish Mahadevan 		spi_setup_word_len(mas, mode, xfer->bits_per_word);
516561de45fSGirish Mahadevan 		mas->cur_bits_per_word = xfer->bits_per_word;
517561de45fSGirish Mahadevan 	}
518561de45fSGirish Mahadevan 
519561de45fSGirish Mahadevan 	/* Speed and bits per word can be overridden per transfer */
5200e3b8a81SAkash Asthana 	ret = geni_spi_set_clock_and_bw(mas, xfer->speed_hz);
521e68b6624SDouglas Anderson 	if (ret)
522561de45fSGirish Mahadevan 		return;
523561de45fSGirish Mahadevan 
524561de45fSGirish Mahadevan 	mas->tx_rem_bytes = 0;
525561de45fSGirish Mahadevan 	mas->rx_rem_bytes = 0;
526561de45fSGirish Mahadevan 
527561de45fSGirish Mahadevan 	if (!(mas->cur_bits_per_word % MIN_WORD_LEN))
528561de45fSGirish Mahadevan 		len = xfer->len * BITS_PER_BYTE / mas->cur_bits_per_word;
529561de45fSGirish Mahadevan 	else
530561de45fSGirish Mahadevan 		len = xfer->len / (mas->cur_bits_per_word / BITS_PER_BYTE + 1);
531561de45fSGirish Mahadevan 	len &= TRANS_LEN_MSK;
532561de45fSGirish Mahadevan 
533561de45fSGirish Mahadevan 	mas->cur_xfer = xfer;
53419ea3275SStephen Boyd 	if (xfer->tx_buf) {
53519ea3275SStephen Boyd 		m_cmd |= SPI_TX_ONLY;
536561de45fSGirish Mahadevan 		mas->tx_rem_bytes = xfer->len;
537561de45fSGirish Mahadevan 		writel(len, se->base + SE_SPI_TX_TRANS_LEN);
538561de45fSGirish Mahadevan 	}
539561de45fSGirish Mahadevan 
54019ea3275SStephen Boyd 	if (xfer->rx_buf) {
54119ea3275SStephen Boyd 		m_cmd |= SPI_RX_ONLY;
542561de45fSGirish Mahadevan 		writel(len, se->base + SE_SPI_RX_TRANS_LEN);
543561de45fSGirish Mahadevan 		mas->rx_rem_bytes = xfer->len;
544561de45fSGirish Mahadevan 	}
5452ee471a1SDouglas Anderson 
5462ee471a1SDouglas Anderson 	/*
5472ee471a1SDouglas Anderson 	 * Lock around right before we start the transfer since our
5482ee471a1SDouglas Anderson 	 * interrupt could come in at any time now.
5492ee471a1SDouglas Anderson 	 */
5502ee471a1SDouglas Anderson 	spin_lock_irq(&mas->lock);
551561de45fSGirish Mahadevan 	geni_se_setup_m_cmd(se, m_cmd, FRAGMENTATION);
552561de45fSGirish Mahadevan 
553561de45fSGirish Mahadevan 	/*
554561de45fSGirish Mahadevan 	 * TX_WATERMARK_REG should be set after SPI configuration and
555561de45fSGirish Mahadevan 	 * setting up GENI SE engine, as driver starts data transfer
556561de45fSGirish Mahadevan 	 * for the watermark interrupt.
557561de45fSGirish Mahadevan 	 */
5586d66507dSDouglas Anderson 	if (m_cmd & SPI_TX_ONLY) {
5596d66507dSDouglas Anderson 		if (geni_spi_handle_tx(mas))
560561de45fSGirish Mahadevan 			writel(mas->tx_wm, se->base + SE_GENI_TX_WATERMARK_REG);
5616d66507dSDouglas Anderson 	}
5622ee471a1SDouglas Anderson 	spin_unlock_irq(&mas->lock);
563561de45fSGirish Mahadevan }
564561de45fSGirish Mahadevan 
565561de45fSGirish Mahadevan static int spi_geni_transfer_one(struct spi_master *spi,
566561de45fSGirish Mahadevan 				struct spi_device *slv,
567561de45fSGirish Mahadevan 				struct spi_transfer *xfer)
568561de45fSGirish Mahadevan {
569561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
570561de45fSGirish Mahadevan 
571690d8b91SDouglas Anderson 	if (spi_geni_is_abort_still_pending(mas))
572690d8b91SDouglas Anderson 		return -EBUSY;
573690d8b91SDouglas Anderson 
574561de45fSGirish Mahadevan 	/* Terminate and return success for 0 byte length transfer */
575561de45fSGirish Mahadevan 	if (!xfer->len)
576561de45fSGirish Mahadevan 		return 0;
577561de45fSGirish Mahadevan 
578561de45fSGirish Mahadevan 	setup_fifo_xfer(xfer, mas, slv->mode, spi);
579561de45fSGirish Mahadevan 	return 1;
580561de45fSGirish Mahadevan }
581561de45fSGirish Mahadevan 
582561de45fSGirish Mahadevan static irqreturn_t geni_spi_isr(int irq, void *data)
583561de45fSGirish Mahadevan {
584561de45fSGirish Mahadevan 	struct spi_master *spi = data;
585561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
586561de45fSGirish Mahadevan 	struct geni_se *se = &mas->se;
587561de45fSGirish Mahadevan 	u32 m_irq;
588561de45fSGirish Mahadevan 
5892ee471a1SDouglas Anderson 	m_irq = readl(se->base + SE_GENI_M_IRQ_STATUS);
5902ee471a1SDouglas Anderson 	if (!m_irq)
591561de45fSGirish Mahadevan 		return IRQ_NONE;
592561de45fSGirish Mahadevan 
593e191a082SDouglas Anderson 	if (m_irq & (M_CMD_OVERRUN_EN | M_ILLEGAL_CMD_EN | M_CMD_FAILURE_EN |
594e191a082SDouglas Anderson 		     M_RX_FIFO_RD_ERR_EN | M_RX_FIFO_WR_ERR_EN |
595e191a082SDouglas Anderson 		     M_TX_FIFO_RD_ERR_EN | M_TX_FIFO_WR_ERR_EN))
596e191a082SDouglas Anderson 		dev_warn(mas->dev, "Unexpected IRQ err status %#010x\n", m_irq);
597e191a082SDouglas Anderson 
598539afdf9SDouglas Anderson 	spin_lock(&mas->lock);
599561de45fSGirish Mahadevan 
600561de45fSGirish Mahadevan 	if ((m_irq & M_RX_FIFO_WATERMARK_EN) || (m_irq & M_RX_FIFO_LAST_EN))
601561de45fSGirish Mahadevan 		geni_spi_handle_rx(mas);
602561de45fSGirish Mahadevan 
603561de45fSGirish Mahadevan 	if (m_irq & M_TX_FIFO_WATERMARK_EN)
604561de45fSGirish Mahadevan 		geni_spi_handle_tx(mas);
605561de45fSGirish Mahadevan 
606561de45fSGirish Mahadevan 	if (m_irq & M_CMD_DONE_EN) {
6077ba9bdcbSDouglas Anderson 		if (mas->cur_xfer) {
608561de45fSGirish Mahadevan 			spi_finalize_current_transfer(spi);
6097ba9bdcbSDouglas Anderson 			mas->cur_xfer = NULL;
610561de45fSGirish Mahadevan 			/*
61159ab0fa0SStephen Boyd 			 * If this happens, then a CMD_DONE came before all the
61259ab0fa0SStephen Boyd 			 * Tx buffer bytes were sent out. This is unusual, log
61359ab0fa0SStephen Boyd 			 * this condition and disable the WM interrupt to
61459ab0fa0SStephen Boyd 			 * prevent the system from stalling due an interrupt
61559ab0fa0SStephen Boyd 			 * storm.
61659ab0fa0SStephen Boyd 			 *
61759ab0fa0SStephen Boyd 			 * If this happens when all Rx bytes haven't been
61859ab0fa0SStephen Boyd 			 * received, log the condition. The only known time
61959ab0fa0SStephen Boyd 			 * this can happen is if bits_per_word != 8 and some
62059ab0fa0SStephen Boyd 			 * registers that expect xfer lengths in num spi_words
621561de45fSGirish Mahadevan 			 * weren't written correctly.
622561de45fSGirish Mahadevan 			 */
623561de45fSGirish Mahadevan 			if (mas->tx_rem_bytes) {
624561de45fSGirish Mahadevan 				writel(0, se->base + SE_GENI_TX_WATERMARK_REG);
625561de45fSGirish Mahadevan 				dev_err(mas->dev, "Premature done. tx_rem = %d bpw%d\n",
626561de45fSGirish Mahadevan 					mas->tx_rem_bytes, mas->cur_bits_per_word);
627561de45fSGirish Mahadevan 			}
628561de45fSGirish Mahadevan 			if (mas->rx_rem_bytes)
629561de45fSGirish Mahadevan 				dev_err(mas->dev, "Premature done. rx_rem = %d bpw%d\n",
630561de45fSGirish Mahadevan 					mas->rx_rem_bytes, mas->cur_bits_per_word);
63159ab0fa0SStephen Boyd 		} else {
63259ab0fa0SStephen Boyd 			complete(&mas->cs_done);
63359ab0fa0SStephen Boyd 		}
634561de45fSGirish Mahadevan 	}
635561de45fSGirish Mahadevan 
6367ba9bdcbSDouglas Anderson 	if (m_irq & M_CMD_CANCEL_EN)
6377ba9bdcbSDouglas Anderson 		complete(&mas->cancel_done);
6387ba9bdcbSDouglas Anderson 	if (m_irq & M_CMD_ABORT_EN)
6397ba9bdcbSDouglas Anderson 		complete(&mas->abort_done);
640561de45fSGirish Mahadevan 
6412ee471a1SDouglas Anderson 	/*
642*db56d030SJay Fang 	 * It's safe or a good idea to Ack all of our interrupts at the end
643*db56d030SJay Fang 	 * of the function. Specifically:
6442ee471a1SDouglas Anderson 	 * - M_CMD_DONE_EN / M_RX_FIFO_LAST_EN: Edge triggered interrupts and
6452ee471a1SDouglas Anderson 	 *   clearing Acks. Clearing at the end relies on nobody else having
6462ee471a1SDouglas Anderson 	 *   started a new transfer yet or else we could be clearing _their_
6472ee471a1SDouglas Anderson 	 *   done bit, but everyone grabs the spinlock before starting a new
6482ee471a1SDouglas Anderson 	 *   transfer.
6492ee471a1SDouglas Anderson 	 * - M_RX_FIFO_WATERMARK_EN / M_TX_FIFO_WATERMARK_EN: These appear
6502ee471a1SDouglas Anderson 	 *   to be "latched level" interrupts so it's important to clear them
6512ee471a1SDouglas Anderson 	 *   _after_ you've handled the condition and always safe to do so
6522ee471a1SDouglas Anderson 	 *   since they'll re-assert if they're still happening.
6532ee471a1SDouglas Anderson 	 */
654561de45fSGirish Mahadevan 	writel(m_irq, se->base + SE_GENI_M_IRQ_CLEAR);
6552ee471a1SDouglas Anderson 
656539afdf9SDouglas Anderson 	spin_unlock(&mas->lock);
6572ee471a1SDouglas Anderson 
6580dccff3cSAlok Chauhan 	return IRQ_HANDLED;
659561de45fSGirish Mahadevan }
660561de45fSGirish Mahadevan 
661561de45fSGirish Mahadevan static int spi_geni_probe(struct platform_device *pdev)
662561de45fSGirish Mahadevan {
6636a34e285SAlok Chauhan 	int ret, irq;
664561de45fSGirish Mahadevan 	struct spi_master *spi;
665561de45fSGirish Mahadevan 	struct spi_geni_master *mas;
6666a34e285SAlok Chauhan 	void __iomem *base;
6676a34e285SAlok Chauhan 	struct clk *clk;
668ea1e5b33SStephen Boyd 	struct device *dev = &pdev->dev;
6696a34e285SAlok Chauhan 
6706a34e285SAlok Chauhan 	irq = platform_get_irq(pdev, 0);
6716b8ac10eSStephen Boyd 	if (irq < 0)
6726a34e285SAlok Chauhan 		return irq;
6736a34e285SAlok Chauhan 
674d8e477abSYueHaibing 	base = devm_platform_ioremap_resource(pdev, 0);
6756a34e285SAlok Chauhan 	if (IS_ERR(base))
6766a34e285SAlok Chauhan 		return PTR_ERR(base);
6776a34e285SAlok Chauhan 
678ea1e5b33SStephen Boyd 	clk = devm_clk_get(dev, "se");
679ea1e5b33SStephen Boyd 	if (IS_ERR(clk))
6806a34e285SAlok Chauhan 		return PTR_ERR(clk);
681561de45fSGirish Mahadevan 
6828f96c434SLukas Wunner 	spi = devm_spi_alloc_master(dev, sizeof(*mas));
683561de45fSGirish Mahadevan 	if (!spi)
684561de45fSGirish Mahadevan 		return -ENOMEM;
685561de45fSGirish Mahadevan 
686561de45fSGirish Mahadevan 	platform_set_drvdata(pdev, spi);
687561de45fSGirish Mahadevan 	mas = spi_master_get_devdata(spi);
6886a34e285SAlok Chauhan 	mas->irq = irq;
689ea1e5b33SStephen Boyd 	mas->dev = dev;
690ea1e5b33SStephen Boyd 	mas->se.dev = dev;
691ea1e5b33SStephen Boyd 	mas->se.wrapper = dev_get_drvdata(dev->parent);
6926a34e285SAlok Chauhan 	mas->se.base = base;
6936a34e285SAlok Chauhan 	mas->se.clk = clk;
694cfb12911SYangtao Li 
695cfb12911SYangtao Li 	ret = devm_pm_opp_set_clkname(&pdev->dev, "se");
696cfb12911SYangtao Li 	if (ret)
697cfb12911SYangtao Li 		return ret;
6981a9e489eSRajendra Nayak 	/* OPP table is optional */
699cfb12911SYangtao Li 	ret = devm_pm_opp_of_add_table(&pdev->dev);
7007d568edfSViresh Kumar 	if (ret && ret != -ENODEV) {
7011a9e489eSRajendra Nayak 		dev_err(&pdev->dev, "invalid OPP table in device tree\n");
702cfb12911SYangtao Li 		return ret;
7031a9e489eSRajendra Nayak 	}
704561de45fSGirish Mahadevan 
705561de45fSGirish Mahadevan 	spi->bus_num = -1;
706ea1e5b33SStephen Boyd 	spi->dev.of_node = dev->of_node;
707561de45fSGirish Mahadevan 	spi->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP | SPI_CS_HIGH;
708561de45fSGirish Mahadevan 	spi->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 32);
709561de45fSGirish Mahadevan 	spi->num_chipselect = 4;
710561de45fSGirish Mahadevan 	spi->max_speed_hz = 50000000;
711561de45fSGirish Mahadevan 	spi->prepare_message = spi_geni_prepare_message;
712561de45fSGirish Mahadevan 	spi->transfer_one = spi_geni_transfer_one;
713561de45fSGirish Mahadevan 	spi->auto_runtime_pm = true;
714561de45fSGirish Mahadevan 	spi->handle_err = handle_fifo_timeout;
715561de45fSGirish Mahadevan 	spi->set_cs = spi_geni_set_cs;
7163b25f337SStephen Boyd 	spi->use_gpio_descriptors = true;
717561de45fSGirish Mahadevan 
7187ba9bdcbSDouglas Anderson 	init_completion(&mas->cs_done);
7197ba9bdcbSDouglas Anderson 	init_completion(&mas->cancel_done);
7207ba9bdcbSDouglas Anderson 	init_completion(&mas->abort_done);
721561de45fSGirish Mahadevan 	spin_lock_init(&mas->lock);
722cfdab2cdSDouglas Anderson 	pm_runtime_use_autosuspend(&pdev->dev);
723cfdab2cdSDouglas Anderson 	pm_runtime_set_autosuspend_delay(&pdev->dev, 250);
724ea1e5b33SStephen Boyd 	pm_runtime_enable(dev);
725561de45fSGirish Mahadevan 
7260e3b8a81SAkash Asthana 	ret = geni_icc_get(&mas->se, NULL);
7270e3b8a81SAkash Asthana 	if (ret)
7280e3b8a81SAkash Asthana 		goto spi_geni_probe_runtime_disable;
7290e3b8a81SAkash Asthana 	/* Set the bus quota to a reasonable value for register access */
7300e3b8a81SAkash Asthana 	mas->se.icc_paths[GENI_TO_CORE].avg_bw = Bps_to_icc(CORE_2X_50_MHZ);
7310e3b8a81SAkash Asthana 	mas->se.icc_paths[CPU_TO_GENI].avg_bw = GENI_DEFAULT_BW;
7320e3b8a81SAkash Asthana 
7330e3b8a81SAkash Asthana 	ret = geni_icc_set_bw(&mas->se);
7340e3b8a81SAkash Asthana 	if (ret)
7350e3b8a81SAkash Asthana 		goto spi_geni_probe_runtime_disable;
7360e3b8a81SAkash Asthana 
737561de45fSGirish Mahadevan 	ret = spi_geni_init(mas);
738561de45fSGirish Mahadevan 	if (ret)
739561de45fSGirish Mahadevan 		goto spi_geni_probe_runtime_disable;
740561de45fSGirish Mahadevan 
741ea1e5b33SStephen Boyd 	ret = request_irq(mas->irq, geni_spi_isr, 0, dev_name(dev), spi);
742561de45fSGirish Mahadevan 	if (ret)
743561de45fSGirish Mahadevan 		goto spi_geni_probe_runtime_disable;
744561de45fSGirish Mahadevan 
745561de45fSGirish Mahadevan 	ret = spi_register_master(spi);
746561de45fSGirish Mahadevan 	if (ret)
747561de45fSGirish Mahadevan 		goto spi_geni_probe_free_irq;
748561de45fSGirish Mahadevan 
749561de45fSGirish Mahadevan 	return 0;
750561de45fSGirish Mahadevan spi_geni_probe_free_irq:
751561de45fSGirish Mahadevan 	free_irq(mas->irq, spi);
752561de45fSGirish Mahadevan spi_geni_probe_runtime_disable:
753ea1e5b33SStephen Boyd 	pm_runtime_disable(dev);
754561de45fSGirish Mahadevan 	return ret;
755561de45fSGirish Mahadevan }
756561de45fSGirish Mahadevan 
757561de45fSGirish Mahadevan static int spi_geni_remove(struct platform_device *pdev)
758561de45fSGirish Mahadevan {
759561de45fSGirish Mahadevan 	struct spi_master *spi = platform_get_drvdata(pdev);
760561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
761561de45fSGirish Mahadevan 
762561de45fSGirish Mahadevan 	/* Unregister _before_ disabling pm_runtime() so we stop transfers */
763561de45fSGirish Mahadevan 	spi_unregister_master(spi);
764561de45fSGirish Mahadevan 
765561de45fSGirish Mahadevan 	free_irq(mas->irq, spi);
766561de45fSGirish Mahadevan 	pm_runtime_disable(&pdev->dev);
767561de45fSGirish Mahadevan 	return 0;
768561de45fSGirish Mahadevan }
769561de45fSGirish Mahadevan 
770561de45fSGirish Mahadevan static int __maybe_unused spi_geni_runtime_suspend(struct device *dev)
771561de45fSGirish Mahadevan {
772561de45fSGirish Mahadevan 	struct spi_master *spi = dev_get_drvdata(dev);
773561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
7740e3b8a81SAkash Asthana 	int ret;
775561de45fSGirish Mahadevan 
7761a9e489eSRajendra Nayak 	/* Drop the performance state vote */
7771a9e489eSRajendra Nayak 	dev_pm_opp_set_rate(dev, 0);
7781a9e489eSRajendra Nayak 
7790e3b8a81SAkash Asthana 	ret = geni_se_resources_off(&mas->se);
7800e3b8a81SAkash Asthana 	if (ret)
7810e3b8a81SAkash Asthana 		return ret;
7820e3b8a81SAkash Asthana 
7830e3b8a81SAkash Asthana 	return geni_icc_disable(&mas->se);
784561de45fSGirish Mahadevan }
785561de45fSGirish Mahadevan 
786561de45fSGirish Mahadevan static int __maybe_unused spi_geni_runtime_resume(struct device *dev)
787561de45fSGirish Mahadevan {
788561de45fSGirish Mahadevan 	struct spi_master *spi = dev_get_drvdata(dev);
789561de45fSGirish Mahadevan 	struct spi_geni_master *mas = spi_master_get_devdata(spi);
7900e3b8a81SAkash Asthana 	int ret;
7910e3b8a81SAkash Asthana 
7920e3b8a81SAkash Asthana 	ret = geni_icc_enable(&mas->se);
7930e3b8a81SAkash Asthana 	if (ret)
7940e3b8a81SAkash Asthana 		return ret;
795561de45fSGirish Mahadevan 
7965f219524SDouglas Anderson 	ret = geni_se_resources_on(&mas->se);
7975f219524SDouglas Anderson 	if (ret)
7985f219524SDouglas Anderson 		return ret;
7995f219524SDouglas Anderson 
8005f219524SDouglas Anderson 	return dev_pm_opp_set_rate(mas->dev, mas->cur_sclk_hz);
801561de45fSGirish Mahadevan }
802561de45fSGirish Mahadevan 
803561de45fSGirish Mahadevan static int __maybe_unused spi_geni_suspend(struct device *dev)
804561de45fSGirish Mahadevan {
805561de45fSGirish Mahadevan 	struct spi_master *spi = dev_get_drvdata(dev);
806561de45fSGirish Mahadevan 	int ret;
807561de45fSGirish Mahadevan 
808561de45fSGirish Mahadevan 	ret = spi_master_suspend(spi);
809561de45fSGirish Mahadevan 	if (ret)
810561de45fSGirish Mahadevan 		return ret;
811561de45fSGirish Mahadevan 
812561de45fSGirish Mahadevan 	ret = pm_runtime_force_suspend(dev);
813561de45fSGirish Mahadevan 	if (ret)
814561de45fSGirish Mahadevan 		spi_master_resume(spi);
815561de45fSGirish Mahadevan 
816561de45fSGirish Mahadevan 	return ret;
817561de45fSGirish Mahadevan }
818561de45fSGirish Mahadevan 
819561de45fSGirish Mahadevan static int __maybe_unused spi_geni_resume(struct device *dev)
820561de45fSGirish Mahadevan {
821561de45fSGirish Mahadevan 	struct spi_master *spi = dev_get_drvdata(dev);
822561de45fSGirish Mahadevan 	int ret;
823561de45fSGirish Mahadevan 
824561de45fSGirish Mahadevan 	ret = pm_runtime_force_resume(dev);
825561de45fSGirish Mahadevan 	if (ret)
826561de45fSGirish Mahadevan 		return ret;
827561de45fSGirish Mahadevan 
828561de45fSGirish Mahadevan 	ret = spi_master_resume(spi);
829561de45fSGirish Mahadevan 	if (ret)
830561de45fSGirish Mahadevan 		pm_runtime_force_suspend(dev);
831561de45fSGirish Mahadevan 
832561de45fSGirish Mahadevan 	return ret;
833561de45fSGirish Mahadevan }
834561de45fSGirish Mahadevan 
835561de45fSGirish Mahadevan static const struct dev_pm_ops spi_geni_pm_ops = {
836561de45fSGirish Mahadevan 	SET_RUNTIME_PM_OPS(spi_geni_runtime_suspend,
837561de45fSGirish Mahadevan 					spi_geni_runtime_resume, NULL)
838561de45fSGirish Mahadevan 	SET_SYSTEM_SLEEP_PM_OPS(spi_geni_suspend, spi_geni_resume)
839561de45fSGirish Mahadevan };
840561de45fSGirish Mahadevan 
841561de45fSGirish Mahadevan static const struct of_device_id spi_geni_dt_match[] = {
842561de45fSGirish Mahadevan 	{ .compatible = "qcom,geni-spi" },
843561de45fSGirish Mahadevan 	{}
844561de45fSGirish Mahadevan };
845561de45fSGirish Mahadevan MODULE_DEVICE_TABLE(of, spi_geni_dt_match);
846561de45fSGirish Mahadevan 
847561de45fSGirish Mahadevan static struct platform_driver spi_geni_driver = {
848561de45fSGirish Mahadevan 	.probe  = spi_geni_probe,
849561de45fSGirish Mahadevan 	.remove = spi_geni_remove,
850561de45fSGirish Mahadevan 	.driver = {
851561de45fSGirish Mahadevan 		.name = "geni_spi",
852561de45fSGirish Mahadevan 		.pm = &spi_geni_pm_ops,
853561de45fSGirish Mahadevan 		.of_match_table = spi_geni_dt_match,
854561de45fSGirish Mahadevan 	},
855561de45fSGirish Mahadevan };
856561de45fSGirish Mahadevan module_platform_driver(spi_geni_driver);
857561de45fSGirish Mahadevan 
858561de45fSGirish Mahadevan MODULE_DESCRIPTION("SPI driver for GENI based QUP cores");
859561de45fSGirish Mahadevan MODULE_LICENSE("GPL v2");
860