blob: 57600dc5bb2effacfbaefa62d74c345d5875f8d3 [file] [log] [blame]
xf.li2f424182024-08-20 00:47:34 -07001/*******************************************************************************
2* Copyright (C) 2016-2021, ZTE Corporation.
3*
4* File Name:spi-zx29.c
5* File Mark:
6* Description:
7* Others:
8* Version: 1.0
9* Author: ZTE
10* Date:
11* modify
12********************************************************************************/
13
14/****************************************************************************
15* Include files
16****************************************************************************/
17#include <linux/kernel.h>
18#include <linux/init.h>
19#include <linux/interrupt.h>
20#include <linux/module.h>
21#include <linux/device.h>
22#include <linux/delay.h>
23#include <linux/platform_device.h>
24#include <linux/err.h>
25#include <linux/clk.h>
26#include <linux/io.h>
27#include <linux/gpio.h>
28#include <linux/slab.h>
29#include <linux/dmaengine.h>
30#include <linux/dma-mapping.h>
31#include <linux/scatterlist.h>
32#include <linux/pm_runtime.h>
33#include <linux/semaphore.h>
34//#include <linux/wakelock.h> //qhf
35#include <linux/of.h>
36#include <linux/of_device.h>
37#include <linux/of_gpio.h>
38#include <linux/dma/zx-dma.h>
39#include <linux/dma-direct.h>
40#include <asm/memory.h>
41#include <linux/debugfs.h>
42#include <linux/spi/spi.h>
43//#include <linux/soc/zte/pm/drv_idle.h>
44
45#include "spi-zx29.h"
46#include "pub_debug_info.h"
47/* yu.dong@20240521 [T106BUG-616] SPI set to slave mode for read will get stuck start */
48#include <linux/wait.h>
49#include <linux/suspend.h>
50/* yu.dong@20240521 [T106BUG-616] SPI set to slave mode for read will get stuck end */
51
52struct zx29_ssp_device_of_data {
53 enum zx29_ssp_device_mode mode;
54};
55static const struct of_device_id zx29_spi_of_match[];
56
57/****************************************************************************
58* Local Macros
59****************************************************************************/
60
61#define CONFIG_SPI_DMA_ENGINE
62#define SPI_PSM_CONTROL (0) //(1)//qhf
63
64/*
65 * This macro is used to define some register default values.
66 * reg is masked with mask, the OR:ed with an (again masked)
67 * val shifted sb steps to the left.
68 */
69#define SPI_WRITE_BITS(reg, val, mask, sb) \
70 ((reg) = (((reg) & ~(mask)) | (((val)<<(sb)) & (mask))))
71
72/*
73 * This macro is also used to define some default values.
74 * It will just shift val by sb steps to the left and mask
75 * the result with mask.
76 */
77#define GEN_MASK_BITS(val, mask, sb) \
78 (((val)<<(sb)) & (mask))
79
80
81#define SPI_GPIO_HIGH 1
82#define SPI_GPIO_LOW 0
83
84#define ZX29_CS_ACTIVE 1 /* normally nCS, active low */
85#define ZX29_CS_INACTIVE 0
86
87#define DRIVE_TX 0
88#define DO_NOT_DRIVE_TX 1
89
90#define DO_NOT_QUEUE_DMA 0
91#define QUEUE_DMA 1
92
93#define RX_TRANSFER BIT(0)
94#define TX_TRANSFER BIT(1)
95
96/* registers */
97#define SPI_VER_REG_OFFSET (0x00)
98#define SPI_COM_CTRL_OFFSET (0x04)
99#define SPI_FMT_CTRL_OFFSET (0x08)
100#define SPI_DR_OFFSET (0x0C)
101#define SPI_FIFO_CTRL_OFFSET (0x10)
102#define SPI_FIFO_SR_OFFSET (0x14)
103#define SPI_INTR_EN_OFFSET (0x18)
104#define SPI_INTR_SR_OFFSET (0x1C)
105#define SPI_TIMING_OFFSET (0x20)
106
107/*
108 * SPI Version Register - SPI_VER_REG
109 */
110#define SPI_VER_REG_MASK_Y (0xFFUL << 16)
111#define SPI_VER_REG_MASK_X (0xFFUL << 24)
112
113/*
114 * SPI Common Control Register - SPI_COM_CTRL
115 */
116#define SPI_COM_CTRL_MASK_LBM (0x1UL << 0)
117#define SPI_COM_CTRL_MASK_SSPE (0x1UL << 1)
118#define SPI_COM_CTRL_MASK_MS (0x1UL << 2)
119#define SPI_COM_CTRL_MASK_SOD (0x1UL << 3)
120#define SPI_COM_CTRL_MASK_SSPE_BACK (0x1UL << 4)
121
122/*
123 * SPI Format Control Register - SPI_FMT_CTRL
124 */
125#define SPI_FMT_CTRL_MASK_FRF (0x3UL << 0)
126#define SPI_FMT_CTRL_MASK_POL (0x1UL << 2)
127#define SPI_FMT_CTRL_MASK_PHA (0x1UL << 3)
128#define SPI_FMT_CTRL_MASK_DSS (0x1FUL << 4)
129
130/*
131 * SPI FIFO Control Register - SPI_FIFO_CTRL
132 */
133#define SPI_FIFO_CTRL_MASK_RX_DMA_EN (0x1UL << 2)
134#define SPI_FIFO_CTRL_MASK_TX_DMA_EN (0x1UL << 3)
135#define SPI_FIFO_CTRL_MASK_RX_FIFO_THRES (0xFUL << 4)
136#define SPI_FIFO_CTRL_MASK_TX_FIFO_THRES (0xFUL << 8)
137/*
138 * SPI FIFO Status Register - SPI_FIFO_SR
139 */
140
141#define SPI_FIFO_SR_MASK_RX_BEYOND_THRES (0x1UL << 0)
142#define SPI_FIFO_SR_MASK_TX_BEYOND_THRES (0x1UL << 1)
143#define SPI_FIFO_SR_MASK_RX_FIFO_FULL (0x1UL << 2)
144#define SPI_FIFO_SR_MASK_TX_FIFO_EMPTY (0x1UL << 3)
145#define SPI_FIFO_SR_MASK_BUSY (0x1UL << 4)
146#define SPI_FIFO_SR_SHIFT_RX_CNT 5
147
148#define SPI_FIFO_SR_MASK_RX_FIFO_CNTR (0x1fUL << SPI_FIFO_SR_SHIFT_RX_CNT)
149#define SPI_FIFO_SR_SHIFT_TX_CNT 10
150#define SPI_FIFO_SR_MASK_TX_FIFO_CNTR (0x1fUL << SPI_FIFO_SR_SHIFT_TX_CNT)
151
152/*
153 * SPI Interrupt Enable Register - SPI_INTR_EN
154 */
155#define SPI_INTR_EN_MASK_RX_OVERRUN_IE (0x1UL << 0)
156#define SPI_INTR_EN_MASK_TX_UNDERRUN_IE (0x1UL << 1)
157#define SPI_INTR_EN_MASK_RX_FULL_IE (0x1UL << 2)
158#define SPI_INTR_EN_MASK_TX_EMPTY_IE (0x1UL << 3)
159#define SPI_INTR_EN_MASK_RX_THRES_IE (0x1UL << 4)
160#define SPI_INTR_EN_MASK_TX_THRES_IE (0x1UL << 5)
xf.li2f424182024-08-20 00:47:34 -0700161
162/*
163 * SPI Interrupt Status Register OR Interrupt Clear Register - SPI_INTR_SR_SCLR
164 */
165
166#define SPI_INTR_SR_SCLR_MASK_RX_OVERRUN_INTR (0x1UL << 0)
167#define SPI_INTR_SR_SCLR_MASK_TX_UNDERRUN_INTR (0x1UL << 1)
168#define SPI_INTR_SR_SCLR_MASK_RX_FULL_INTR (0x1UL << 2)
169#define SPI_INTR_SR_SCLR_MASK_TX_EMPTY_INTR (0x1UL << 3)
170#define SPI_INTR_SR_SCLR_MASK_RX_THRES_INTR (0x1UL << 4)
171#define SPI_INTR_SR_SCLR_MASK_TX_THRES_INTR (0x1UL << 5)
172
173/*
174 * SPI TIMING Register
175 */
176#define SPI_TIMING_MASK_T_CS_DESEL 0xFUL
177
178/* SPI WCLK Freqency */
179#define SPI_SPICLK_FREQ_26M (26*1000*1000)
180#define SPI_SPICLK_FREQ_104M (104*1000*1000)
181#define SPI_SPICLK_FREQ_156M (156*1000*1000)
182
183#define CLEAR_ALL_INTERRUPTS 0x3FUL
184#define ENABLE_ALL_INTERRUPTS 0x3FUL
185#define ENABLE_INTERRUPTS 0x03UL
186#define DISABLE_ALL_INTERRUPTS 0x0UL
187/*
188 * Message State
189 * we use the spi_message.state (void *) pointer to
190 * hold a single state value, that's why all this
191 * (void *) casting is done here.
192 */
193
194enum zx29_spi_state {
195 STATE_START,
196 STATE_RUNNING,
197 STATE_DONE,
198 STATE_ERROR
199};
200
201/*
202 * SPI State - Whether Enabled or Disabled
203 */
204#define SPI_DISABLED (0)
205#define SPI_ENABLED (1)
206
207/*
208 * SPI DMA State - Whether DMA Enabled or Disabled
209 */
210#define SPI_DMA_DISABLED (0)
211#define SPI_DMA_ENABLED (1)
212
213/*
214 * SPI SOD State - Whether SOD Enabled or Disabled
215 */
216#define SPI_SOD_DISABLED (1)
217#define SPI_SOD_ENABLED (0)
218#define SPI_SLAVE_MODE (1)
219#define SPI_MASTER_MODE (0)
220
221
222/*
223 * SPI TRANSFER DELAY CFG
224 * DELAY TIME ≈ (1 / Buad_Rate) * 8 + (SPI_TIMING_T_CS_DESEL + 1) / Buad_Rate;
225 */
226
227
228enum spi_fifo_threshold_level {
229 SPI_FIFO_THRES_1,
230 SPI_FIFO_THRES_2,
231 SPI_FIFO_THRES_3,
232 SPI_FIFO_THRES_4,
233 SPI_FIFO_THRES_5,
234 SPI_FIFO_THRES_6,
235 SPI_FIFO_THRES_7,
236 SPI_FIFO_THRES_8,
237 SPI_FIFO_THRES_9,
238 SPI_FIFO_THRES_10,
239 SPI_FIFO_THRES_11,
240 SPI_FIFO_THRES_12,
241 SPI_FIFO_THRES_13,
242 SPI_FIFO_THRES_14,
243 SPI_FIFO_THRES_15,
244 SPI_FIFO_THRES_16
245
246};
247
248/*
249 * SPI Clock Parameter ranges
250 */
251#define DIV_MIN 0x00
252#define DIV_MAX 0x0F
253
254#define SPI_POLLING_TIMEOUT 1000
255
256/*
257 * The type of reading going on on this chip
258 */
259enum spi_reading {
260 READING_NULL,
261 READING_U8,
262 READING_U16,
263 READING_U32
264};
265
266/**
267 * The type of writing going on on this chip
268 */
269enum spi_writing {
270 WRITING_NULL,
271 WRITING_U8,
272 WRITING_U16,
273 WRITING_U32
274};
275
276/**
277 * struct vendor_data - vendor-specific config parameters
278 * for PL022 derivates
279 * @fifodepth: depth of FIFOs (both)
280 * @max_bpw: maximum number of bits per word
281 * @unidir: supports unidirection transfers
282 * @extended_cr: 32 bit wide control register 0 with extra
283 * features and extra features in CR1 as found in the ST variants
284 * @pl023: supports a subset of the ST extensions called "PL023"
285 */
286struct vendor_data {
287 int fifodepth;
288 int max_bpw;
289 bool loopback;
290};
291
292struct zx29_ssp_pins
293{
294 struct device *dev;
295
296 struct pinctrl *pctrl;
297 struct pinctrl_state *pfunc;
298 struct pinctrl_state *pgpio;
299 struct pinctrl_state *pcs_gpio_active;
300 struct pinctrl_state *pcs_func;
301 struct pinctrl_state *pcs_gpio_sleep;
302 int gpio_cs;
303 int gpio_clk;
304 int gpio_tx;
305 int gpio_rx;
306};
307struct zx29_ssp_pins ssp_pins[4];
308
309/**
310 * struct spi-zx29 - This is the private SSP driver data structure
311 * @adev: AMBA device model hookup
312 * @vendor: vendor data for the IP block
313 * @phybase: the physical memory where the SSP device resides
314 * @virtbase: the virtual memory where the SSP is mapped
315 * @clk: outgoing clock "SPICLK" for the SPI bus
316 * @master: SPI framework hookup
317 * @master_info: controller-specific data from machine setup
318 * @kworker: thread struct for message pump
319 * @kworker_task: pointer to task for message pump kworker thread
320 * @pump_messages: work struct for scheduling work to the message pump
321 * @queue_lock: spinlock to syncronise access to message queue
322 * @queue: message queue
323 * @busy: message pump is busy
324 * @running: message pump is running
325 * @pump_transfers: Tasklet used in Interrupt Transfer mode
326 * @cur_msg: Pointer to current spi_message being processed
327 * @cur_transfer: Pointer to current spi_transfer
328 * @cur_chip: pointer to current clients chip(assigned from controller_state)
329 * @next_msg_cs_active: the next message in the queue has been examined
330 * and it was found that it uses the same chip select as the previous
331 * message, so we left it active after the previous transfer, and it's
332 * active already.
333 * @tx: current position in TX buffer to be read
334 * @tx_end: end position in TX buffer to be read
335 * @rx: current position in RX buffer to be written
336 * @rx_end: end position in RX buffer to be written
337 * @read: the type of read currently going on
338 * @write: the type of write currently going on
339 * @exp_fifo_level: expected FIFO level
340 * @dma_rx_channel: optional channel for RX DMA
341 * @dma_tx_channel: optional channel for TX DMA
342 * @sgt_rx: scattertable for the RX transfer
343 * @sgt_tx: scattertable for the TX transfer
344 * @dummypage: a dummy page used for driving data on the bus with DMA
345 */
346struct zx29_spi {
347 char name[16];
348 struct platform_device *pdev;
349 struct vendor_data *vendor;
350 resource_size_t phybase;
351 void __iomem *virtbase;
352 unsigned int irq;
353 struct clk *pclk;/* spi controller work clock */
354 struct clk *spi_clk;/* spi clk line clock */
355 u32 clkfreq;
356 struct spi_master *master;
357 struct zx29_spi_controller *master_info;
358 /* Message per-transfer pump */
359 struct tasklet_struct pump_transfers;
360 struct spi_message *cur_msg;
361 struct spi_transfer *cur_transfer;
362 struct chip_data *cur_chip;
363 bool next_msg_cs_active;
364 void *tx;
365 void *tx_end;
366 void *rx;
367 void *rx_end;
368 enum spi_reading read;
369 enum spi_writing write;
370 u32 exp_fifo_level;
371 enum spi_rx_level_trig rx_lev_trig;
372 enum spi_tx_level_trig tx_lev_trig;
373 /* DMA settings */
374#ifdef CONFIG_SPI_DMA_ENGINE
375 struct dma_chan *dma_rx_channel;
376 struct dma_chan *dma_tx_channel;
377 struct sg_table sgt_rx;
378 struct sg_table sgt_tx;
379 char *dummypage;
380 unsigned int dma_running;
381// struct mutex spi_lock;
382#endif
383#if defined(CONFIG_DEBUG_FS)
384 struct dentry * spi_root;
385 struct debugfs_regset32 spi_regset;
386 u32 spi_poll_cnt;
387 u32 spi_dma_cnt;
388#endif
389#if SPI_PSM_CONTROL
390 struct wake_lock psm_lock;
391#endif
392 struct semaphore sema_dma;
393/* yu.dong@20240521 [T106BUG-616] SPI set to slave mode for read will get stuck start */
394 wait_queue_head_t wait;
395 int trans_done;
396/* yu.dong@20240521 [T106BUG-616] SPI set to slave mode for read will get stuck end */
397
398 u8 iface_mode;
399#define SPI_MOTO_FORMAT 0x00
400#define SPI_TI_FORMAT 0x01
401#define SPI_ISI_FORMAT 0x02
402 enum zx29_ssp_device_mode mode;
403 int (*zx29_flush_rxfifo) (struct zx29_spi *zx29spi,void *buf);
404
405};
406
407/**
408 * struct chip_data - To maintain runtime state of SSP for each client chip
409 * @cr0: Value of control register CR0 of SSP - on later ST variants this
410 * register is 32 bits wide rather than just 16
411 * @cr1: Value of control register CR1 of SSP
412 * @dmacr: Value of DMA control Register of SSP
413 * @cpsr: Value of Clock prescale register
414 * @cs: Value of cs register
415 * @n_bytes: how many bytes(power of 2) reqd for a given data width of client
416 * @enable_dma: Whether to enable DMA or not
417 * @read: function ptr to be used to read when doing xfer for this chip
418 * @write: function ptr to be used to write when doing xfer for this chip
419 * @cs_control: chip select callback provided by chip
420 * @xfer_type: polling/interrupt/DMA
421 *
422 * Runtime state of the SSP controller, maintained per chip,
423 * This would be set according to the current message that would be served
424 */
425struct chip_data {
426 u32 ver_reg;
427 u32 com_ctrl;
428 u32 fmt_ctrl;
429 u32 fifo_ctrl;
430 u32 timing;
431// u32 intr_en;
432 u8 n_bytes;
433 u8 clk_div;/* spi clk divider */
434 bool enable_dma;
435 bool enable_trans_gap;
436 enum spi_reading read;
437 enum spi_writing write;
438 void (*cs_control) (int dev_id,u32 command);
439 int xfer_type;
440};
441//struct semaphore g_SpiTransferSemaphore;
442
443struct zx29_spi *g_zx29_spi[4];
444
445#if SPI_PSM_CONTROL
446static volatile unsigned int spi_active_count = 0;
447
448static void zx29_spi_set_active(struct wake_lock *lock)
449{
450 unsigned long flags;
451
452 local_irq_save(flags);
453
454 if(spi_active_count == 0)
455 {
456 zx_cpuidle_set_busy(IDLE_FLAG_SPI);
457 }
458 spi_active_count++;
459
460 local_irq_restore(flags);
461
462 wake_lock(lock);
463}
464
465static void zx29_spi_set_idle(struct wake_lock *lock)
466{
467 unsigned long flags;
468
469 local_irq_save(flags);
470#if 0 //qhf
471 spi_active_count--;
472 if(spi_active_count == 0)
473 {
474 zx_cpuidle_set_free(IDLE_FLAG_SPI);
475 }
476#endif
477 local_irq_restore(flags);
478
479 wake_unlock(lock);
480}
481#endif
482
483static int zx29_do_interrupt_dma_transfer(struct zx29_spi *zx29spi);
484/**
485 * default_cs_control - Dummy chip select function
486 * @command: select/delect the chip
487 *
488 * If no chip select function is provided by client this is used as dummy
489 * chip select
490 */
491
492static void default_cs_control(int dev_id,u32 command)
493{
494 gpio_set_value(ssp_pins[dev_id].gpio_cs, !command);
495}
496
497/**
498 * flush - flush the FIFO to reach a clean state
499 * SSP driver private data structure
500 */
501static int flush(struct zx29_spi *zx29spi)
502{
503 unsigned long limit = loops_per_jiffy << 1;
504 uint32_t rx_fifo_cnt_msk = SPI_FIFO_SR_MASK_RX_FIFO_CNTR;
505
506 dev_dbg(&zx29spi->pdev->dev, "flush\n");
507 /* Flushing FIFO by software cannot clear RX DMA Request. */
508 do {
509 if(0 == strcmp(zx29spi->pdev->name,"140a000.ssp")) {
510 while (readl((SPI_FIFO_SR_OFFSET+zx29spi->virtbase)) & rx_fifo_cnt_msk)
511 readl((SPI_DR_OFFSET+zx29spi->virtbase));
512 }else {
513 while ((readl((SPI_FIFO_SR_OFFSET+zx29spi->virtbase))>>SPI_FIFO_SR_SHIFT_RX_CNT)&0x7F)
514 readl((SPI_DR_OFFSET+zx29spi->virtbase));
515 }
516 } while ((readl((SPI_FIFO_SR_OFFSET+zx29spi->virtbase)) & SPI_FIFO_SR_MASK_BUSY) && limit--);
517
518 zx29spi->exp_fifo_level = 0;
519
520 return limit;
521}
522
523/**
524 * restore_state - Load configuration of current chip
525 * SSP driver private data structure
526 */
527static void restore_state(struct zx29_spi *zx29spi)
528{
529 struct chip_data *chip = zx29spi->cur_chip;
530
531 /* disable all interrupts */
532 writel(ENABLE_INTERRUPTS, (SPI_INTR_EN_OFFSET+zx29spi->virtbase));
533 writel(CLEAR_ALL_INTERRUPTS, (SPI_INTR_SR_OFFSET+zx29spi->virtbase));
534
535 writel(chip->fmt_ctrl, (SPI_FMT_CTRL_OFFSET+zx29spi->virtbase));
536 writel(chip->fifo_ctrl, (SPI_FIFO_CTRL_OFFSET+zx29spi->virtbase));
537// writel(chip->intr_en, SPI_INTR_EN(zx297520v2spi->virtbase));
538 if(zx29spi->mode == ZX29_SSP_SLAVE_TYPE)
539 chip->com_ctrl |= GEN_MASK_BITS(SPI_SLAVE, SPI_COM_CTRL_MASK_MS, 2)|GEN_MASK_BITS(SPI_ENABLED, SPI_COM_CTRL_MASK_SSPE, 1);
540 writel(chip->com_ctrl, (SPI_COM_CTRL_OFFSET + zx29spi->virtbase));
541 writel(chip->timing, (SPI_TIMING_OFFSET + zx29spi->virtbase));
542}
543
544/*
545 * Default spi Register Values
546 */
547#define DEFAULT_SPI_COM_CTRL ( \
548 GEN_MASK_BITS(LOOPBACK_DISABLED, SPI_COM_CTRL_MASK_LBM, 0) | \
549 GEN_MASK_BITS(SPI_DISABLED, SPI_COM_CTRL_MASK_SSPE, 1) | \
550 GEN_MASK_BITS(SPI_MASTER, SPI_COM_CTRL_MASK_MS, 2) \
551)
552
553/*
554 * Default spi Register Values
555 */
556#define DEFAULT_SPI_SLAVE_COM_CTRL ( \
557 GEN_MASK_BITS(LOOPBACK_DISABLED, SPI_COM_CTRL_MASK_LBM, 0) | \
558 GEN_MASK_BITS(SPI_DISABLED, SPI_COM_CTRL_MASK_SSPE, 1) | \
559 GEN_MASK_BITS(SPI_SLAVE, SPI_COM_CTRL_MASK_MS, 2) \
560)
561
562
563#define DEFAULT_SPI_FMT_CTRL ( \
564 GEN_MASK_BITS(SPI_INTERFACE_MOTOROLA_SPI, SPI_FMT_CTRL_MASK_FRF, 0) | \
565 GEN_MASK_BITS(SPI_CLK_POL_IDLE_LOW, SPI_FMT_CTRL_MASK_POL, 2) | \
566 GEN_MASK_BITS(SPI_CLK_FIRST_EDGE, SPI_FMT_CTRL_MASK_PHA, 3) | \
567 GEN_MASK_BITS(SPI_DATA_BITS_8, SPI_FMT_CTRL_MASK_DSS, 4) \
568)
569
570#define DEFAULT_SPI_FIFO_CTRL ( \
571 GEN_MASK_BITS(SPI_DMA_DISABLED, SPI_FIFO_CTRL_MASK_RX_DMA_EN, 2) | \
572 GEN_MASK_BITS(SPI_DMA_DISABLED, SPI_FIFO_CTRL_MASK_TX_DMA_EN, 3) | \
573 GEN_MASK_BITS(SPI_FIFO_THRES_8, SPI_FIFO_CTRL_MASK_RX_FIFO_THRES, 4) | \
574 GEN_MASK_BITS(SPI_FIFO_THRES_8, SPI_FIFO_CTRL_MASK_TX_FIFO_THRES, 8) \
575)
576
577
578/**
579 * load_ssp_default_config - Load default configuration for SSP
580 * SSP driver private data structure
581 */
582static void load_spi_default_config(struct zx29_spi *zx29spi)
583{
584 writel(CLEAR_ALL_INTERRUPTS, (SPI_INTR_SR_OFFSET+zx29spi->virtbase));
585 writel(ENABLE_INTERRUPTS, (SPI_INTR_EN_OFFSET+zx29spi->virtbase));
586
587 writel(DEFAULT_SPI_FMT_CTRL, (SPI_FMT_CTRL_OFFSET+zx29spi->virtbase));
588 writel(DEFAULT_SPI_FIFO_CTRL, (SPI_FIFO_CTRL_OFFSET+zx29spi->virtbase));
589
590 if(zx29spi->mode == ZX29_SSP_MASTER_TYPE) {
591 writel(DEFAULT_SPI_COM_CTRL, (SPI_COM_CTRL_OFFSET+zx29spi->virtbase));
592 }
593 else {
594 writel(DEFAULT_SPI_SLAVE_COM_CTRL, (SPI_COM_CTRL_OFFSET+zx29spi->virtbase));
595 }
596}
597
598
599static unsigned reader(struct zx29_spi *zx29spi)
600{
601 uint32_t fifo_sr = 0,rd_max = 0;
602 unsigned len = 0;
603 uint32_t rx_fifo_cnt_msk = SPI_FIFO_SR_MASK_RX_FIFO_CNTR;
604 /*
605 * The FIFO depth is different between primecell variants.
606 * I believe filling in too much in the FIFO might cause
607 * errons in 8bit wide transfers on ARM variants (just 8 words
608 * FIFO, means only 8x8 = 64 bits in FIFO) at least.
609 *
610 * To prevent this issue, the TX FIFO is only filled to the
611 * unused RX FIFO fill length, regardless of what the TX
612 * FIFO status flag indicates.
613 */
614
615 fifo_sr = readl((SPI_FIFO_SR_OFFSET+zx29spi->virtbase));
616 if(!strcmp(zx29spi->pdev->name,"140a000.ssp")) {
617 rd_max = (fifo_sr & rx_fifo_cnt_msk) >> SPI_FIFO_SR_SHIFT_RX_CNT;
618 }else {
619 rd_max = (fifo_sr>>SPI_FIFO_SR_SHIFT_RX_CNT)&0x7f;
620 }
621 //read rx fifo to empty first
622 while ((zx29spi->rx < zx29spi->rx_end) && rd_max--) {
623 switch (zx29spi->read) {
624 case READING_NULL:
625 readw((SPI_DR_OFFSET+zx29spi->virtbase));
626 break;
627 case READING_U8:
628 *(u8 *) (zx29spi->rx) =
629 readw((SPI_DR_OFFSET+zx29spi->virtbase)) & 0xFFU;
630 break;
631 case READING_U16:
632 *(u16 *) (zx29spi->rx) =
633 (u16) readw((SPI_DR_OFFSET+zx29spi->virtbase));
634 break;
635 case READING_U32:
636 *(u32 *) (zx29spi->rx) =
637 readl((SPI_DR_OFFSET+zx29spi->virtbase));
638 break;
639 }
640 len += zx29spi->cur_chip->n_bytes;
641 zx29spi->rx += (zx29spi->cur_chip->n_bytes);
642 zx29spi->exp_fifo_level--;
643 }
644 return len;
645
646}
647
648static unsigned writer(struct zx29_spi *zx29spi)
649{
650 uint32_t fifo_sr;
651 uint32_t wr_max;
652 uint32_t tx_fifo_cnt_msk = SPI_FIFO_SR_MASK_TX_FIFO_CNTR;
653 uint32_t tx_fifo_cnt_pos = SPI_FIFO_SR_SHIFT_TX_CNT;
654 unsigned len = 0;
655 /*
656 * The FIFO depth is different between primecell variants.
657 * I believe filling in too much in the FIFO might cause
658 * errons in 8bit wide transfers on ARM variants (just 8 words
659 * FIFO, means only 8x8 = 64 bits in FIFO) at least.
660 *
661 * To prevent this issue, the TX FIFO is only filled to the
662 * unused RX FIFO fill length, regardless of what the TX
663 * FIFO status flag indicates.
664 */
665
666 fifo_sr = readl((SPI_FIFO_SR_OFFSET+zx29spi->virtbase));
667 if(!strcmp(zx29spi->pdev->name,"140a000.ssp")) {
668 wr_max = (fifo_sr & tx_fifo_cnt_msk) >> tx_fifo_cnt_pos;
669 }else {
670 wr_max = (fifo_sr>>12)&0x1f;
671 }
672
673 if ((fifo_sr & SPI_FIFO_SR_MASK_BUSY) && wr_max) {
674 wr_max--;
675 }
676
677 while ((zx29spi->tx < zx29spi->tx_end) && wr_max--) {
678 switch (zx29spi->write) {
679 case WRITING_NULL:
680 writew(0x0, (SPI_DR_OFFSET+zx29spi->virtbase));
681 break;
682 case WRITING_U8:
683 writew(*(u8 *) (zx29spi->tx), (SPI_DR_OFFSET+zx29spi->virtbase));
684 break;
685 case WRITING_U16:
686 writew((*(u16 *) (zx29spi->tx)), (SPI_DR_OFFSET+zx29spi->virtbase));
687 break;
688 case WRITING_U32:
689 writel(*(u32 *) (zx29spi->tx), (SPI_DR_OFFSET+zx29spi->virtbase));
690 break;
691 }
692 len += zx29spi->cur_chip->n_bytes;
693 zx29spi->tx += (zx29spi->cur_chip->n_bytes);
694 zx29spi->exp_fifo_level++;
695 }
696 return len;
697}
698
699
700/**
701 * This will write to TX and read from RX according to the parameters.
702 */
703
704static void readwriter(struct zx29_spi *zx29spi)
705{
706 uint32_t fifo_sr;
707 uint32_t rd_max, wr_max;
708 uint32_t rx_fifo_cnt_msk;
709 uint32_t tx_fifo_cnt_msk;
710 uint32_t tx_fifo_cnt_pos;
711 ktime_t k_time_start = 0;
712 ktime_t diff_ns = 0;
713
714 rx_fifo_cnt_msk = SPI_FIFO_SR_MASK_RX_FIFO_CNTR;
715 tx_fifo_cnt_msk = SPI_FIFO_SR_MASK_TX_FIFO_CNTR;
716 tx_fifo_cnt_pos = SPI_FIFO_SR_SHIFT_TX_CNT;
717
718 /*
719 * The FIFO depth is different between primecell variants.
720 * I believe filling in too much in the FIFO might cause
721 * errons in 8bit wide transfers on ARM variants (just 8 words
722 * FIFO, means only 8x8 = 64 bits in FIFO) at least.
723 *
724 * To prevent this issue, the TX FIFO is only filled to the
725 * unused RX FIFO fill length, regardless of what the TX
726 * FIFO status flag indicates.
727 */
728
729 fifo_sr = readl((SPI_FIFO_SR_OFFSET+zx29spi->virtbase));
730 if(!strcmp(zx29spi->pdev->name,"140a000.ssp")) {
731 rd_max = (fifo_sr & rx_fifo_cnt_msk) >> SPI_FIFO_SR_SHIFT_RX_CNT;
732 wr_max = (fifo_sr & tx_fifo_cnt_msk) >> tx_fifo_cnt_pos;
733 }else {
734 rd_max = (fifo_sr>>SPI_FIFO_SR_SHIFT_RX_CNT)&0x7f;
735 wr_max = (fifo_sr>>12)&0x1f;
736 }
737
738 if ((fifo_sr & SPI_FIFO_SR_MASK_BUSY) && wr_max) {
739 wr_max--;
740 }
741 //read rx fifo to empty first
742 while ((zx29spi->rx < zx29spi->rx_end) && rd_max--) {
743 switch (zx29spi->read) {
744 case READING_NULL:
745 readw((SPI_DR_OFFSET+zx29spi->virtbase));
746 break;
747 case READING_U8:
748 *(u8 *) (zx29spi->rx) =
749 readw((SPI_DR_OFFSET+zx29spi->virtbase)) & 0xFFU;
750 break;
751 case READING_U16:
752 *(u16 *) (zx29spi->rx) =
753 (u16) readw((SPI_DR_OFFSET+zx29spi->virtbase));
754 break;
755 case READING_U32:
756 *(u32 *) (zx29spi->rx) =
757 readl((SPI_DR_OFFSET+zx29spi->virtbase));
758 break;
759 }
760 zx29spi->rx += (zx29spi->cur_chip->n_bytes);
761 zx29spi->exp_fifo_level--;
762 }
763
764 //write
765 while ((zx29spi->tx < zx29spi->tx_end) && wr_max--) {
766 switch (zx29spi->write) {
767 case WRITING_NULL:
768 writew(0x0, (SPI_DR_OFFSET+zx29spi->virtbase));
769 break;
770 case WRITING_U8:
771 writew(*(u8 *) (zx29spi->tx), (SPI_DR_OFFSET+zx29spi->virtbase));
772 break;
773 case WRITING_U16:
774 writew((*(u16 *) (zx29spi->tx)), (SPI_DR_OFFSET+zx29spi->virtbase));
775 break;
776 case WRITING_U32:
777 writel(*(u32 *) (zx29spi->tx), (SPI_DR_OFFSET+zx29spi->virtbase));
778 break;
779 }
780 zx29spi->tx += (zx29spi->cur_chip->n_bytes);
781 zx29spi->exp_fifo_level++;
782
783 if(zx29spi->cur_chip->enable_trans_gap) {
784
785 k_time_start = ktime_get();
786 do {
787 diff_ns = ktime_sub(ktime_get(),k_time_start);
788 fifo_sr = readl((SPI_FIFO_SR_OFFSET+zx29spi->virtbase)) & SPI_FIFO_SR_MASK_BUSY;
789 cpu_relax();
790 }
791 while (fifo_sr && diff_ns < 10000000); //10ms
792 if(diff_ns >= 10000000) {
793 fifo_sr = readl((SPI_FIFO_SR_OFFSET+zx29spi->virtbase)) & SPI_FIFO_SR_MASK_BUSY;
794 if(fifo_sr)
795 dev_info(&zx29spi->pdev->dev, "bus busy time_start=%lld diff_ns=%lld \n",k_time_start,diff_ns);
796 }
797 }
798 }
799
800 if(!zx29spi->cur_chip->enable_trans_gap) {
801
802 k_time_start = ktime_get();
803 do {
804 diff_ns = ktime_sub(ktime_get(),k_time_start);
805 fifo_sr = readl((SPI_FIFO_SR_OFFSET+zx29spi->virtbase)) & SPI_FIFO_SR_MASK_BUSY;
806 cpu_relax();
807 }while (fifo_sr && diff_ns < 100000000); //100ms
808 if(diff_ns >= 100000000) {
809 fifo_sr = readl((SPI_FIFO_SR_OFFSET+zx29spi->virtbase)) & SPI_FIFO_SR_MASK_BUSY;
810 if(fifo_sr)
811 dev_info(&zx29spi->pdev->dev, "bus busy.. time_start=%lld diff_ns=%lld \n",k_time_start,diff_ns);
812 }
813 }
814 /*
815 * When we exit here the TX FIFO should be full and the RX FIFO
816 * should be empty
817 */
818}
819
820/*
821 * This DMA functionality is only compiled in if we have
822 * access to the generic DMA devices/DMA engine.
823 */
824#ifdef CONFIG_SPI_DMA_ENGINE
825
826static void zx29_fill_txfifo(struct zx29_spi *zx29spi)
827{
828 uint32_t fifo_sr;
829 int32_t rd_max, wr_max;
830 uint32_t rx_fifo_cnt_msk;
831 uint32_t tx_fifo_cnt_msk;
832 uint32_t tx_fifo_cnt_pos;
833 unsigned cur_transfer_len;
834 rx_fifo_cnt_msk = SPI_FIFO_SR_MASK_RX_FIFO_CNTR;
835 tx_fifo_cnt_msk = SPI_FIFO_SR_MASK_TX_FIFO_CNTR;
836 tx_fifo_cnt_pos = SPI_FIFO_SR_SHIFT_TX_CNT;
837
838 if(!zx29spi) {
839 printk("zx29spi err! \r\n");
840 return;
841 }
842 cur_transfer_len = zx29spi->cur_transfer->len;
843 while (zx29spi->tx < zx29spi->tx_end && cur_transfer_len) {
844 fifo_sr = readl((SPI_FIFO_SR_OFFSET+zx29spi->virtbase));
845#if 0
846 rd_max = (fifo_sr & rx_fifo_cnt_msk) >> SPI_FIFO_SR_SHIFT_RX_CNT;
847 wr_max = (fifo_sr & tx_fifo_cnt_msk) >> tx_fifo_cnt_pos;
848#else
849 if(!strcmp(zx29spi->pdev->name,"140a000.ssp")) {
850 rd_max = (fifo_sr & rx_fifo_cnt_msk) >> SPI_FIFO_SR_SHIFT_RX_CNT;
851 wr_max = (fifo_sr & tx_fifo_cnt_msk) >> tx_fifo_cnt_pos;
852 }else {
853 rd_max = (fifo_sr>>SPI_FIFO_SR_SHIFT_RX_CNT)&0x7f;
854 wr_max = (fifo_sr>>12)&0x1f;
855 }
856#endif
857 if (fifo_sr & SPI_FIFO_SR_MASK_BUSY) {
858 wr_max--;
859 }
860 wr_max -= rd_max;
861 wr_max = (wr_max > 0) ? wr_max : 0;
862
863 //write
864 while ((zx29spi->tx < zx29spi->tx_end) && wr_max--) {
865 writew(0x0, (SPI_DR_OFFSET+zx29spi->virtbase));
866 zx29spi->tx += (zx29spi->cur_chip->n_bytes);
867 cur_transfer_len -= zx29spi->cur_chip->n_bytes;
868 }
869
870 cpu_relax();
871 }
872}
873
874static void dma_callback(void *data)
875{
876 struct zx29_spi *zx29spi = (struct zx29_spi *)data;
877 //printk(KERN_INFO "spi:dma transfer complete. %X-%X-%x\n", zx29spi->dma_running, readl((SPI_INTR_SR_OFFSET+zx29spi->virtbase)),readl((SPI_FIFO_SR_OFFSET+zx29spi->virtbase)));
878 /* yu.dong@20240521 [T106BUG-616] SPI set to slave mode for read will get stuck start */
879 //up(&zx29spi->sema_dma);
880 if(zx29spi->master->slave == true){
881 wake_up(&zx29spi->wait);
882 zx29spi->trans_done = true;
883 }else{
884 up(&zx29spi->sema_dma);
885 }
886 /* yu.dong@20240521 [T106BUG-616] SPI set to slave mode for read will get stuck end */
887}
888
889/*
890static void dma_callback_tx(void *data)
891{
892 struct zx29_spi *zx29spi = (struct zx29_spi *)data;
893 // printk(KERN_INFO "spi:dma transfer complete tx\n");
894 printk("%s",__func__);
895 printk("COM=0x%x,FMT=0x%x,FIFO_CTL=0x%x,FIFO_SR=0x%x\n",readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase)),readl((SPI_FMT_CTRL_OFFSET+zx29spi->virtbase)),readl((SPI_FIFO_CTRL_OFFSET+zx29spi->virtbase)),readl((SPI_FIFO_SR_OFFSET+zx29spi->virtbase)));
896
897 //up(&g_SpiTransferSemaphore);
898}
899*/
900
901/**
902 * configure_dma - configures the channels for the next transfer
903 * SSP driver's private data structure
904 */
905
906static int configure_dma(struct zx29_spi *zx29spi)
907{
908// unsigned int pages;
909// int ret;
910// int rx_sglen, tx_sglen;
911 dma_channel_def rx_conf;
912 dma_channel_def tx_conf;
913 struct dma_chan *rxchan = zx29spi->dma_rx_channel;
914 struct dma_chan *txchan = zx29spi->dma_tx_channel;
915 struct dma_async_tx_descriptor *rxdesc;
916 struct dma_async_tx_descriptor *txdesc;
917 struct spi_transfer *transfer = zx29spi->cur_transfer;
918
919 rx_conf.src_addr = (SPI_DR_OFFSET+zx29spi->phybase);
920 rx_conf.dest_addr = (unsigned int)zx29spi->rx;
921 rx_conf.dma_control.tran_mode = TRAN_PERI_TO_MEM;
922 rx_conf.dma_control.irq_mode = DMA_ALL_IRQ_ENABLE;
923 rx_conf.link_addr = 0;
924
925 tx_conf.src_addr = (unsigned int)zx29spi->tx;
926 tx_conf.dest_addr = (SPI_DR_OFFSET+zx29spi->phybase);
927 tx_conf.dma_control.tran_mode = TRAN_MEM_TO_PERI;
928 tx_conf.dma_control.irq_mode = DMA_ALL_IRQ_ENABLE;
929 tx_conf.link_addr = 0;
930
931
932 /* Check that the channels are available */
933 if (!rxchan || !txchan)
934 return -ENODEV;
935
936 /*
937 * If supplied, the DMA burstsize should equal the FIFO trigger level.
938 * Notice that the DMA engine uses one-to-one mapping. Since we can
939 * not trigger on 2 elements this needs explicit mapping rather than
940 * calculation.
941 */
942
943 switch (zx29spi->rx_lev_trig) {
944 case SPI_RX_1_OR_MORE_ELEM:
945 rx_conf.dma_control.src_burst_len = DMA_BURST_LEN_1;
946 rx_conf.dma_control.dest_burst_len = DMA_BURST_LEN_1;
947 break;
948 case SPI_RX_4_OR_MORE_ELEM:
949 rx_conf.dma_control.src_burst_len = DMA_BURST_LEN_4;
950 rx_conf.dma_control.dest_burst_len = DMA_BURST_LEN_4;
951 break;
952 case SPI_RX_8_OR_MORE_ELEM:
953 rx_conf.dma_control.src_burst_len = DMA_BURST_LEN_8;
954 rx_conf.dma_control.dest_burst_len = DMA_BURST_LEN_8;
955 break;
956 case SPI_RX_16_OR_MORE_ELEM:
957 rx_conf.dma_control.src_burst_len = DMA_BURST_LEN_16;
958 rx_conf.dma_control.dest_burst_len = DMA_BURST_LEN_16;
959 break;
960 case SPI_RX_32_OR_MORE_ELEM:
961 rx_conf.dma_control.src_burst_len = DMA_BURST_LEN_ALL;
962 rx_conf.dma_control.dest_burst_len = DMA_BURST_LEN_ALL;
963 break;
964 default:
965 rx_conf.dma_control.src_burst_len = zx29spi->vendor->fifodepth >> 1;
966 rx_conf.dma_control.dest_burst_len = zx29spi->vendor->fifodepth >> 1;
967 break;
968 }
969
970 switch (zx29spi->tx_lev_trig) {
971 case SPI_TX_1_OR_MORE_EMPTY_LOC:
972 tx_conf.dma_control.src_burst_len = DMA_BURST_LEN_1;
973 tx_conf.dma_control.dest_burst_len = DMA_BURST_LEN_1;
974 break;
975 case SPI_TX_4_OR_MORE_EMPTY_LOC:
976 tx_conf.dma_control.src_burst_len = DMA_BURST_LEN_4;
977 tx_conf.dma_control.dest_burst_len = DMA_BURST_LEN_4;
978 break;
979 case SPI_TX_8_OR_MORE_EMPTY_LOC:
980 tx_conf.dma_control.src_burst_len = DMA_BURST_LEN_8;
981 tx_conf.dma_control.dest_burst_len = DMA_BURST_LEN_8;
982 break;
983 case SPI_TX_16_OR_MORE_EMPTY_LOC:
984 tx_conf.dma_control.src_burst_len = DMA_BURST_LEN_16;
985 tx_conf.dma_control.dest_burst_len = DMA_BURST_LEN_16;
986 break;
987 case SPI_TX_32_OR_MORE_EMPTY_LOC:
988 tx_conf.dma_control.src_burst_len = DMA_BURST_LEN_ALL;
989 tx_conf.dma_control.dest_burst_len = DMA_BURST_LEN_ALL;
990 break;
991 default:
992 tx_conf.dma_control.src_burst_len = zx29spi->vendor->fifodepth >> 1;
993 tx_conf.dma_control.dest_burst_len = zx29spi->vendor->fifodepth >> 1;
994 break;
995 }
996
997 switch (zx29spi->read) {
998 case READING_NULL:
999 /* Use the same as for writing */
1000 rx_conf.dma_control.src_burst_size = DMA_BURST_SIZE_8BIT;
1001 rx_conf.dma_control.dest_burst_size = DMA_BURST_SIZE_8BIT;
1002 rx_conf.count = zx29spi->cur_transfer->len;
1003 break;
1004 case READING_U8:
1005 rx_conf.dma_control.src_burst_size = DMA_BURST_SIZE_8BIT;
1006 rx_conf.dma_control.dest_burst_size = DMA_BURST_SIZE_8BIT;
1007 rx_conf.count = zx29spi->cur_transfer->len;
1008 break;
1009 case READING_U16:
1010 rx_conf.dma_control.src_burst_size = DMA_BURST_SIZE_16BIT;
1011 rx_conf.dma_control.dest_burst_size = DMA_BURST_SIZE_16BIT;
1012 rx_conf.count = zx29spi->cur_transfer->len;
1013 break;
1014 case READING_U32:
1015 rx_conf.dma_control.src_burst_size = DMA_BURST_SIZE_32BIT;
1016 rx_conf.dma_control.dest_burst_size = DMA_BURST_SIZE_32BIT;
1017 rx_conf.count = zx29spi->cur_transfer->len;
1018 break;
1019 }
1020
1021 switch (zx29spi->write) {
1022 case WRITING_NULL:
1023 /* Use the same as for reading */
1024 tx_conf.dma_control.src_burst_size = DMA_BURST_SIZE_8BIT;
1025 tx_conf.dma_control.dest_burst_size = DMA_BURST_SIZE_8BIT;
1026 tx_conf.count = zx29spi->cur_transfer->len;
1027 break;
1028 case WRITING_U8:
1029 tx_conf.dma_control.src_burst_size = DMA_BURST_SIZE_8BIT;
1030 tx_conf.dma_control.dest_burst_size = DMA_BURST_SIZE_8BIT;
1031 tx_conf.count = zx29spi->cur_transfer->len;
1032 break;
1033 case WRITING_U16:
1034 tx_conf.dma_control.src_burst_size = DMA_BURST_SIZE_16BIT;
1035 tx_conf.dma_control.dest_burst_size = DMA_BURST_SIZE_16BIT;
1036 tx_conf.count = zx29spi->cur_transfer->len;
1037 break;
1038 case WRITING_U32:
1039 tx_conf.dma_control.src_burst_size = DMA_BURST_SIZE_32BIT;
1040 tx_conf.dma_control.dest_burst_size = DMA_BURST_SIZE_32BIT;
1041 tx_conf.count = zx29spi->cur_transfer->len;
1042 break;
1043 }
1044
1045 dmaengine_slave_config(rxchan,(struct dma_slave_config*)&rx_conf);
1046 dmaengine_slave_config(txchan,(struct dma_slave_config*)&tx_conf);
1047
1048 /* Submit and fire RX and TX with TX last so we're ready to read! */
1049 if (zx29spi->rx) {
1050 //printk("%s,tx=%p,rx=%p,len=%d\n",__func__,zx29spi->tx,zx29spi->rx,zx29spi->cur_transfer->len);
1051 //printk("tx_conf:sb_len=%d,db_len=%d, sb_size=%d,db_size=%d\n",tx_conf.dma_control.src_burst_len, tx_conf.dma_control.dest_burst_len, tx_conf.dma_control.src_burst_size, tx_conf.dma_control.dest_burst_size);
1052 //printk("rx_conf:sb_len=%d,db_len=%d, sb_size=%d,db_size=%d\n",rx_conf.dma_control.src_burst_len, rx_conf.dma_control.dest_burst_len, rx_conf.dma_control.src_burst_size, rx_conf.dma_control.dest_burst_size);
1053
1054 rxdesc= rxchan->device->device_prep_interleaved_dma(rxchan,NULL,0);
1055 txdesc= txchan->device->device_prep_interleaved_dma(txchan,NULL,0);
1056 /* Put the callback on the RX transfer only, that should finish last */
1057 rxdesc->callback = dma_callback;
1058 rxdesc->callback_param = zx29spi;
1059 // txdesc->callback = dma_callback_tx;
1060 // txdesc->callback_param = zx29spi;
1061
1062 dmaengine_submit(rxdesc);
1063 dma_async_issue_pending(rxchan);
1064 if (transfer->tx_dma) {
1065 /* SPI RX buffer may overflow in DMA busy situation. */
1066 dmaengine_submit(txdesc);
1067 dma_async_issue_pending(txchan);
1068 zx29spi->dma_running = TX_TRANSFER | RX_TRANSFER;
1069 enable_irq(zx29spi->irq); /* detect overflow through interrupt */
1070 } else {
1071 if(zx29spi->mode == ZX29_SSP_MASTER_TYPE)
1072 zx29_fill_txfifo(zx29spi);
1073 zx29spi->dma_running = RX_TRANSFER;
1074 }
1075 }
1076 else if (zx29spi->tx){
1077 txdesc = txchan->device->device_prep_interleaved_dma(txchan,NULL,0);
1078 txdesc->callback = dma_callback;
1079 txdesc->callback_param = zx29spi;
1080 dmaengine_submit(txdesc);
1081 dma_async_issue_pending(txchan);
1082 zx29spi->dma_running = TX_TRANSFER;
1083 }
1084
1085 return 0;
1086}
1087
1088#if 0
1089static bool zx29_dma_filter_fn(struct dma_chan *chan, void *param)
1090{
1091 dma_peripheral_id peri_id = (dma_peripheral_id) param;
1092#if 0
1093 if ((chan->chan_id == (unsigned int)peri_id) && \
1094 (strcmp(dev_name(chan->device->dev), "a1200000.dma") == 0))
1095 return true;
1096
1097 chan->private = param;
1098
1099 return false;
1100#endif
1101 if (chan->chan_id == (unsigned int)peri_id)
1102 return true;
1103
1104 chan->private = param;
1105
1106 return false;
1107
1108}
1109#endif
1110extern bool zx29_dma_filter_fn(struct dma_chan *chan, void *param);
1111static int zx29_dma_probe(struct zx29_spi *zx29spi)
1112{
1113 dma_cap_mask_t mask;
1114
1115 /* Try to acquire a generic DMA engine slave channel */
1116 dma_cap_zero(mask);
1117 dma_cap_set(DMA_SLAVE, mask);
1118 /*
1119 * We need both RX and TX channels to do DMA, else do none
1120 * of them.
1121 */
1122 zx29spi->dma_rx_channel = dma_request_channel(mask,
1123 zx29_dma_filter_fn,
1124 zx29spi->master_info->dma_rx_param);
1125 if (!zx29spi->dma_rx_channel) {
1126 dev_dbg(&zx29spi->pdev->dev, "no RX DMA channel!\n");
1127 dev_err(&zx29spi->pdev->dev, "no RX DMA channel!,dma_rx_param=:%d\n",zx29spi->master_info->dma_rx_param);
1128 goto err_no_rxchan;
1129 }
1130
1131 zx29spi->dma_tx_channel = dma_request_channel(mask,
1132 zx29_dma_filter_fn,
1133 zx29spi->master_info->dma_tx_param);
1134 if (!zx29spi->dma_tx_channel) {
1135 dev_dbg(&zx29spi->pdev->dev, "no TX DMA channel!\n");
1136 dev_err(&zx29spi->pdev->dev, "no TX DMA channel!\n");
1137 goto err_no_txchan;
1138 }
1139
1140 zx29spi->dummypage = kmalloc(PAGE_SIZE, GFP_KERNEL);
1141 if (!zx29spi->dummypage) {
1142 dev_dbg(&zx29spi->pdev->dev, "no DMA dummypage!\n");
1143 dev_err(&zx29spi->pdev->dev, "no DMA dummypage!\n");
1144 goto err_no_dummypage;
1145 }
1146
1147 dev_info(&zx29spi->pdev->dev, "setup for DMA on RX %s, TX %s\n",
1148 dma_chan_name(zx29spi->dma_rx_channel),
1149 dma_chan_name(zx29spi->dma_tx_channel));
1150
1151 return 0;
1152
1153err_no_dummypage:
1154 dma_release_channel(zx29spi->dma_tx_channel);
1155err_no_txchan:
1156 dma_release_channel(zx29spi->dma_rx_channel);
1157 zx29spi->dma_rx_channel = NULL;
1158err_no_rxchan:
1159 dev_err(&zx29spi->pdev->dev,
1160 "Failed to work in dma mode, work without dma!\n");
1161 dev_dbg(&zx29spi->pdev->dev,
1162 "Failed to work in dma mode, work without dma!\n");
1163 return -ENODEV;
1164}
1165
1166static void terminate_dma(struct zx29_spi *zx29spi)
1167{
1168 struct dma_chan *rxchan = zx29spi->dma_rx_channel;
1169 struct dma_chan *txchan = zx29spi->dma_tx_channel;
1170
1171 dmaengine_terminate_all(rxchan);
1172 dmaengine_terminate_all(txchan);
1173// unmap_free_dma_scatter(zx29spi);
1174 zx29spi->dma_running = 0;
1175}
1176
1177static void zx29_dma_remove(struct zx29_spi *zx29spi)
1178{
1179 if (zx29spi->dma_running)
1180 terminate_dma(zx29spi);
1181 if (zx29spi->dma_tx_channel)
1182 dma_release_channel(zx29spi->dma_tx_channel);
1183 if (zx29spi->dma_rx_channel)
1184 dma_release_channel(zx29spi->dma_rx_channel);
1185 kfree(zx29spi->dummypage);
1186}
1187
1188#endif
1189
1190static irqreturn_t zx29_spi_irq(int irqno, void *dev_id)
1191{
1192 struct zx29_spi *zx29spi = dev_id;
1193
1194 disable_irq_nosync(zx29spi->irq);
1195 up(&zx29spi->sema_dma);
1196
1197 //pr_info("spi_irq %X-%X\n", zx29spi->dma_running, readl((SPI_INTR_SR_OFFSET+zx29spi->virtbase)));
1198 return IRQ_HANDLED;
1199}
1200
1201static void print_info_data(void * data, int len) {
1202 int i = 0;
1203 unsigned char *p = data;
1204
1205 if(p) {
1206
1207 for(i = 0;i <= (len-8);i+=8) {
1208 printk("%02x %02x %02x %02x %02x %02x %02x %02x \r\n",p[i],p[i+1],p[i+2],p[i+3],p[i+4],p[i+5],p[i+6],p[i+7]);
1209 }
1210 printk("\n");
1211 }
1212
1213}
1214
1215static int zx29_flush_rxfifo(struct zx29_spi *zx29spi,void *buf)
1216{
1217 int ret = 0;
1218 struct spi_transfer transfer;
1219 unsigned char data[64] = {0};
1220 uint32_t fifo_sr = 0;
1221 uint32_t rd_max = 0;
1222 uint32_t rx_fifo_cnt_msk= SPI_FIFO_SR_MASK_RX_FIFO_CNTR;
1223
1224 transfer.tx_buf = 0;
1225 transfer.rx_buf = data;
1226 transfer.len = 0;
1227
1228 fifo_sr = readl((SPI_FIFO_SR_OFFSET+zx29spi->virtbase));
1229 if(!strcmp(zx29spi->pdev->name,"140a000.ssp")) {
1230 rd_max = (fifo_sr & rx_fifo_cnt_msk) >> SPI_FIFO_SR_SHIFT_RX_CNT;
1231 }else {
1232 rd_max = (fifo_sr>>SPI_FIFO_SR_SHIFT_RX_CNT)&0x7f;
1233 }
1234 while(rd_max--) {
1235 *(u8 *) transfer.rx_buf =
1236 readw((SPI_DR_OFFSET+zx29spi->virtbase)) & 0xFFU;
1237 transfer.rx_buf++;
1238 transfer.len++;
1239 }
1240 memcpy(buf,data,transfer.len);
1241 //dev_info(&zx29spi->pdev->dev,"spi_fifo_sr = %d transfer.len=%d \n",fifo_sr,transfer.len);
1242 //print_info_data(data,transfer.len);
1243 return transfer.len;
1244
1245}
1246
1247int get_spi_rx_fifo(struct spi_device *spi,unsigned char *buf)
1248{
1249
1250 struct zx29_spi *zx29spi = spi_master_get_devdata(spi->master);
1251 int ret = 0;
1252
1253 if(!spi || !buf || !zx29spi)
1254 return ret;
1255 if(!zx29spi->zx29_flush_rxfifo)
1256 return ret;
1257 return zx29spi->zx29_flush_rxfifo(zx29spi,buf);
1258}
1259
1260
1261void set_spi_timing(struct spi_device *spi,unsigned int param)
1262{
1263 struct zx29_spi *zx29spi = spi_master_get_devdata(spi->master);
1264
1265 printk("val set before: 0x%x \n",readl((SPI_TIMING_OFFSET+zx29spi->virtbase)));
1266 writel(param, (SPI_TIMING_OFFSET+zx29spi->virtbase));
1267 printk("val set after: 0x%x \n",readl((SPI_TIMING_OFFSET+zx29spi->virtbase)));
1268}
1269
1270
1271void slave_mode_set(struct spi_device *spi,unsigned int mode)
1272{
1273 unsigned int regval = 0;
1274 ktime_t k_time_start = 0;
1275 ktime_t diff_ns = 0;
1276 struct zx29_spi *zx29spi = spi_master_get_devdata(spi->master);
1277
1278 regval = readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase)) & (~SPI_COM_CTRL_MASK_SSPE);
1279 writel(regval, (SPI_COM_CTRL_OFFSET+zx29spi->virtbase));
1280
1281 regval = readl((SPI_FMT_CTRL_OFFSET+zx29spi->virtbase)) & (~(SPI_FMT_CTRL_MASK_POL|SPI_FMT_CTRL_MASK_PHA));
1282 printk("val set before: 0x%x \n",regval);
1283 switch(mode){
1284 case 0:
1285 break;
1286 case 1:
1287 regval |= SPI_FMT_CTRL_MASK_PHA;
1288 break;
1289 case 2:
1290 regval |= SPI_FMT_CTRL_MASK_POL;
1291 break;
1292 case 3:
1293 regval |= (SPI_FMT_CTRL_MASK_POL|SPI_FMT_CTRL_MASK_PHA);
1294 break;
1295 default:
1296 break;
1297 }
1298 writel(regval, (SPI_FMT_CTRL_OFFSET+zx29spi->virtbase));
1299
1300 writel(readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase)) | SPI_COM_CTRL_MASK_SSPE, (SPI_COM_CTRL_OFFSET+zx29spi->virtbase));
1301 //while(((readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase))>> 4)&0x1) == 0);
1302 k_time_start = ktime_get();
1303 do {
1304 diff_ns = ktime_sub(ktime_get(),k_time_start);
1305 regval = readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase))>> 4 & 0x1;
1306 cpu_relax();
1307 }
1308 while (!regval && diff_ns < 100000000); //100ms
1309 if(diff_ns >= 100000000)
1310 dev_info(&zx29spi->pdev->dev, "wait sspe timeout, slave_mode_set failed! diff_ns= 0x%x \n",diff_ns);
1311 else
1312 printk("val set after: 0x%x \n",readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase)));
1313 return;
1314}
1315
1316/* yu.dong@20240521 [T106BUG-616] SPI set to slave mode for read will get stuck start */
1317#define SSP0_PARA_BASE_ADDR 0x1400030
1318#define SSP1_PARA_BASE_ADDR 0x1400048
1319#define SSP_MASK_SW_WRST (0x1L << 9)
1320#define SSP_MASK_SW_PRST (0x1L << 8)
1321static int zx29_slave_ctrl_reset(struct zx29_spi *zx29spi)
1322{
1323 void __iomem *addr = NULL;
1324 ktime_t k_time_start = 0;
1325 ktime_t diff_ns = 0;
1326 volatile unsigned int val = 0;
1327
1328 if(!strcmp(zx29spi->pdev->name,"1410000.ssp")) {
1329 addr = ioremap(SSP1_PARA_BASE_ADDR, 0x1000);
1330 }else{
1331 addr = ioremap(SSP0_PARA_BASE_ADDR, 0x1000);
1332 }
1333
1334 if(addr){
1335 val = *(volatile unsigned int *)addr;
1336 //dev_info(&zx29spi->pdev->dev, "val = 0x%x 0x%x\n",val,(~(SSP_MASK_SW_WRST|SSP_MASK_SW_PRST)));
1337 *(volatile unsigned int *)addr = val & (~(SSP_MASK_SW_WRST|SSP_MASK_SW_PRST));
1338
1339 k_time_start = ktime_get();
1340 do {
1341 diff_ns = ktime_sub(ktime_get(),k_time_start);
1342 val = readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase))>> 1 & 0x1;
1343 cpu_relax();
1344 }while(val && diff_ns < 100000000); //100ms
1345
1346 if(diff_ns >= 100000000)
1347 dev_info(&zx29spi->pdev->dev, "zx29_slave_assert_ctrl failed!!! \n");
1348 else
1349 dev_info(&zx29spi->pdev->dev, "zx29_slave_assert_ctrl success! \n");
1350
1351
1352 val = *(volatile unsigned int *)addr;
1353 *(volatile unsigned int *)addr = val|(SSP_MASK_SW_WRST|SSP_MASK_SW_PRST);
1354 udelay(500);
1355
1356 iounmap(addr);
1357 }
1358
1359 return 0;
1360}
1361
1362static int zx29_slave_ctrl_reinit(struct zx29_spi *zx29spi)
1363{
1364 volatile unsigned int regval;
1365 ktime_t k_time_start = 0;
1366 ktime_t diff_ns = 0;
1367
1368 zx29_slave_ctrl_reset(zx29spi);
1369
1370 /* Disable SPI */
1371 regval = readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase)) & (~SPI_COM_CTRL_MASK_SSPE);
1372 writel(regval, (SPI_COM_CTRL_OFFSET+zx29spi->virtbase));
1373
1374 load_spi_default_config(zx29spi);
1375 writel(0, (SPI_TIMING_OFFSET + zx29spi->virtbase));
1376
1377 if(!strcmp(zx29spi->pdev->name,"1410000.ssp")) {
1378 regval = readl((SPI_FMT_CTRL_OFFSET+zx29spi->virtbase))&(~(0x1<<12));
1379 writel(regval, (SPI_FMT_CTRL_OFFSET+zx29spi->virtbase));
1380 dev_info(&zx29spi->pdev->dev," %s set non-camera mode regval:0x%x \n",zx29spi->pdev->name,regval);
1381 }
1382
1383 writel(readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase)) | SPI_COM_CTRL_MASK_SSPE, (SPI_COM_CTRL_OFFSET+zx29spi->virtbase));
1384 k_time_start = ktime_get();
1385 do{
1386 diff_ns = ktime_sub(ktime_get(),k_time_start);
1387 regval = ((readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase))>> 4)&0x1);
1388 cpu_relax();
1389 }while((regval == 0) && diff_ns < 100000000);
1390
1391 if(diff_ns >= 100000000)
1392 dev_info(&zx29spi->pdev->dev, "wait sspen timeout!!! \n");
1393 else
1394 dev_info(&zx29spi->pdev->dev,"ssp enabled \n",regval);
1395
1396 return 0;
1397}
1398/* yu.dong@20240521 [T106BUG-616] SPI set to slave mode for read will get stuck end */
1399
1400static int zx29_slave_do_interrupt_dma_transfer(struct zx29_spi *zx29spi)
1401{
1402 struct spi_transfer *transfer = zx29spi->cur_transfer;
1403 int ret = 0;
1404 ktime_t k_time_start = 0;
1405 ktime_t diff_ns = 0;
1406 unsigned int fifo_sr = 0;
1407 if((void *)transfer->tx_dma != NULL){
1408 zx29spi->tx = (void *)transfer->tx_dma;
1409 zx29spi->tx_end = zx29spi->tx + zx29spi->cur_transfer->len;
1410 }
1411 if((void *)transfer->rx_dma != NULL){
1412 zx29spi->rx = (void *)transfer->rx_dma;
1413 zx29spi->rx_end = zx29spi->rx + zx29spi->cur_transfer->len;
1414
1415 /*if tx is null, use rx buffer as a dummy tx buffer.*/
1416 if((void *)transfer->tx_dma == NULL){
1417 zx29spi->tx = (void *)transfer->rx_dma;
1418 zx29spi->tx_end = zx29spi->tx + zx29spi->cur_transfer->len;
1419 }
1420 }
1421
1422 zx29spi->write = zx29spi->tx ? zx29spi->cur_chip->write : WRITING_NULL;
1423 zx29spi->read = zx29spi->rx ? zx29spi->cur_chip->read : READING_NULL;
1424
1425 /* If we're using DMA, set up DMA here */
1426 if (zx29spi->cur_chip->enable_dma) {
1427 /* Configure DMA transfer */
1428 zx29spi->trans_done = false; //yu.dong@20240521 [T106BUG-616] SPI set to slave mode for read will get stuck
1429 ret = configure_dma(zx29spi);
1430 if (ret) {
1431 dev_err(&zx29spi->pdev->dev, "configuration of DMA failed, fall back to interrupt mode\n");
1432 goto err_config_dma;
1433 }
1434 }
1435
1436 if (zx29spi->cur_chip->enable_dma)
1437 {
1438 extern void spi_dev_send_dma_cfg_down(struct spi_device *spi);
1439 struct spi_device *spi = zx29spi->cur_msg->spi;
1440 spi_dev_send_dma_cfg_down(spi);
1441 /* yu.dong@20240521 [T106BUG-616] SPI set to slave mode for read will get stuck start */
1442 //down(&zx29spi->sema_dma);
1443 ret = wait_event_freezable(zx29spi->wait, zx29spi->trans_done);
1444 if(ret){
1445 terminate_dma(zx29spi);
1446 disable_irq_nosync(zx29spi->irq);
1447 zx29spi->dma_running = 0;
1448 zx29_slave_ctrl_reinit(zx29spi);
1449 goto err_config_dma;
1450
1451 }
1452 /* yu.dong@20240521 [T106BUG-616] SPI set to slave mode for read will get stuck end */
1453 //printk("COM=0x%x,FMT=0x%x,FIFO_CTL=0x%x,FIFO_SR=0x%x\n",readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase)),readl((SPI_FMT_CTRL_OFFSET+zx29spi->virtbase)),readl((SPI_FIFO_CTRL_OFFSET+zx29spi->virtbase)),readl((SPI_FIFO_SR_OFFSET+zx29spi->virtbase)));
1454
1455
1456 k_time_start = ktime_get();
1457 do {
1458 diff_ns = ktime_sub(ktime_get(),k_time_start);
1459 fifo_sr = readl((SPI_FIFO_SR_OFFSET+zx29spi->virtbase)) & SPI_FIFO_SR_MASK_BUSY;
1460 cpu_relax();
1461 }
1462 while (fifo_sr && diff_ns < 100000000); //100ms
1463
1464 if(diff_ns >= 100000000) {
1465 fifo_sr = readl((SPI_FIFO_SR_OFFSET+zx29spi->virtbase)) & SPI_FIFO_SR_MASK_BUSY;
1466 if(fifo_sr)
1467 dev_info(&zx29spi->pdev->dev, "bus busy... time_start=%lld diff_ns=%lld \n",k_time_start,diff_ns);
1468 }
1469 if (zx29spi->dma_running == (TX_TRANSFER | RX_TRANSFER)) {
1470 u32 intr_status;
1471 intr_status = readl((SPI_INTR_SR_OFFSET+zx29spi->virtbase));
1472 if (intr_status & SPI_INTR_SR_SCLR_MASK_RX_OVERRUN_INTR) {
1473 terminate_dma(zx29spi);
1474 dev_err(&zx29spi->cur_msg->spi->dev, "spi rx fifo overflow status = %X!!\n", intr_status);
1475 ret = -EIO;
1476 } else
1477 disable_irq_nosync(zx29spi->irq);
1478 }
1479 zx29spi->dma_running = 0;
1480 }
1481
1482err_config_dma:
1483 if(ret)
1484 {
1485 dev_err(&zx29spi->pdev->dev, "down_interruptible, ret=%d\n",ret);
1486 }
1487 return ret;
1488}
1489
1490
1491static int zx29_do_interrupt_dma_transfer(struct zx29_spi *zx29spi)
1492{
1493 u32 irqflags = ENABLE_ALL_INTERRUPTS;
1494 struct spi_transfer *transfer = zx29spi->cur_transfer;
1495 int ret = 0;
1496 static int sc_debug_info_record_cnt[4] ={0};
1497 ktime_t k_time_start = 0;
1498 ktime_t diff_ns = 0;
1499 unsigned int fifo_sr = 0;
1500
1501 if((void *)transfer->tx_dma != NULL){
1502 zx29spi->tx = (void *)transfer->tx_dma;
1503 zx29spi->tx_end = zx29spi->tx + zx29spi->cur_transfer->len;
1504 }
1505 if((void *)transfer->rx_dma != NULL){
1506 zx29spi->rx = (void *)transfer->rx_dma;
1507 zx29spi->rx_end = zx29spi->rx + zx29spi->cur_transfer->len;
1508
1509 /*if tx is null, use rx buffer as a dummy tx buffer.*/
1510 if((void *)transfer->tx_dma == NULL){
1511 zx29spi->tx = (void *)transfer->rx_dma;
1512 zx29spi->tx_end = zx29spi->tx + zx29spi->cur_transfer->len;
1513 }
1514 }
1515
1516 zx29spi->write = zx29spi->tx ? zx29spi->cur_chip->write : WRITING_NULL;
1517 zx29spi->read = zx29spi->rx ? zx29spi->cur_chip->read : READING_NULL;
1518 //printk("zx29spi->cur_chip->enable_dma= 0x%x transfer->tx_dma=0x%x transfer->rx_dma=0x%x\n",zx29spi->cur_chip->enable_dma,transfer->tx_dma,transfer->rx_dma);
1519 /* If we're using DMA, set up DMA here */
1520 if (zx29spi->cur_chip->enable_dma) {
1521 /* Configure DMA transfer */
1522 ret = configure_dma(zx29spi);
1523 if (ret) {
1524 dev_err(&zx29spi->pdev->dev, "configuration of DMA failed, fall back to interrupt mode\n");
1525 goto err_config_dma;
1526 }
1527 /* Disable interrupts in DMA mode, IRQ from DMA controller */
1528 irqflags = DISABLE_ALL_INTERRUPTS;
1529 }
1530
1531 /* config interrupts */
1532 /* writel(irqflags, (SPI_INTR_EN_OFFSET+zx29spi->virtbase)); //spi interrupt mode is not supported. */
1533
1534 /* Enable SSP, turn on interrupts */
1535// writel(readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase)) | SPI_COM_CTRL_MASK_SSPE, (SPI_COM_CTRL_OFFSET+zx29spi->virtbase));
1536
1537 if (zx29spi->cur_chip->enable_dma)
1538 {
1539 ret = down_timeout(&zx29spi->sema_dma, msecs_to_jiffies(1500));
1540 //printk("COM=0x%x,FMT=0x%x,FIFO_CTL=0x%x,FIFO_SR=0x%x\n",readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase)),readl((SPI_FMT_CTRL_OFFSET+zx29spi->virtbase)),readl((SPI_FIFO_CTRL_OFFSET+zx29spi->virtbase)),readl((SPI_FIFO_SR_OFFSET+zx29spi->virtbase)));
1541 if (ret < 0) {
1542 panic("spi transfer timeout,times(%d)\n",sc_debug_info_record_cnt[zx29spi->pdev->id]);
1543 if(sc_debug_info_record_cnt[zx29spi->pdev->id] < 5) {
1544 sc_debug_info_record(MODULE_ID_CAP_SPI, "%s transfer timeout:0x%x 0x%x 0x%x \n",zx29spi->pdev->name,readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase)),
1545 readl((SPI_FIFO_SR_OFFSET+zx29spi->virtbase)),readl((SPI_INTR_SR_OFFSET+zx29spi->virtbase)));
1546 }
1547 sc_debug_info_record_cnt[zx29spi->pdev->id]++;
1548 }
1549
1550 k_time_start = ktime_get();
1551 do {
1552 diff_ns = ktime_sub(ktime_get(),k_time_start);
1553 fifo_sr = readl((SPI_FIFO_SR_OFFSET+zx29spi->virtbase)) & SPI_FIFO_SR_MASK_BUSY;
1554 cpu_relax();
1555 }
1556 while (fifo_sr && diff_ns < 100000000); //100ms
1557
1558 if(diff_ns >= 100000000) {
1559 fifo_sr = readl((SPI_FIFO_SR_OFFSET+zx29spi->virtbase)) & SPI_FIFO_SR_MASK_BUSY;
1560 if(fifo_sr)
1561 dev_info(&zx29spi->pdev->dev, "bus busy.... time_start=%lld diff_ns=%lld \n",k_time_start,diff_ns);
1562 }
1563
1564 if (zx29spi->dma_running == (TX_TRANSFER | RX_TRANSFER)) {
1565 u32 intr_status;
1566 intr_status = readl((SPI_INTR_SR_OFFSET+zx29spi->virtbase));
1567 if (intr_status & SPI_INTR_SR_SCLR_MASK_RX_OVERRUN_INTR) {
1568 terminate_dma(zx29spi);
1569 dev_err(&zx29spi->cur_msg->spi->dev, "spi rx fifo overflow status = %X!!\n", intr_status);
1570 ret = -EIO;
1571 } else
1572 disable_irq_nosync(zx29spi->irq);
1573 }
1574 zx29spi->dma_running = 0;
1575 }
1576
1577err_config_dma:
1578 if(ret)
1579 {
1580 dev_err(&zx29spi->pdev->dev, "down_interruptible, ret=%d\n",ret);
1581 }
1582// writel(readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase)) & ~ SPI_COM_CTRL_MASK_SSPE, (SPI_COM_CTRL_OFFSET+zx29spi->virtbase));
1583 return ret;
1584}
1585
1586
1587static int zx29_do_polling_transfer(struct zx29_spi *zx29spi)
1588{
1589 struct spi_transfer *transfer = zx29spi->cur_transfer;
1590 int ret = 0;
1591 unsigned int fifo_sr = 0;
1592 ktime_t k_time_start = 0;
1593 ktime_t diff_ns = 0;
1594
1595 dev_dbg(&zx29spi->pdev->dev, "polling transfer ongoing ...\n");
1596
1597 if (!zx29spi->tx && !zx29spi->rx) {
1598 return ret;
1599 }
1600
1601 k_time_start = ktime_get();
1602 /*read and write*/
1603 while ((zx29spi->tx < zx29spi->tx_end) || (zx29spi->rx < zx29spi->rx_end)) {
1604 readwriter(zx29spi);
1605 diff_ns = ktime_sub(ktime_get(),k_time_start);
1606 if(diff_ns >= 1000000000) /*1s*/{
1607 dev_info(&zx29spi->pdev->dev, "do_polling time out,diff_ns=%lld len=0x%x tx=0x%x tx_end=0x%x rx=0x%x rx_end=0x%x \n",
1608 diff_ns,zx29spi->cur_transfer->len,zx29spi->tx,zx29spi->tx_end,zx29spi->rx,zx29spi->rx_end);
1609 ret = -EIO;
1610 break;
1611 }
1612 }
1613
1614 k_time_start = ktime_get();
1615 do {
1616 diff_ns = ktime_sub(ktime_get(),k_time_start);
1617 fifo_sr = readl((SPI_FIFO_SR_OFFSET+zx29spi->virtbase)) & SPI_FIFO_SR_MASK_BUSY;
1618 cpu_relax();
1619
1620 }while (fifo_sr && diff_ns < 100000000); //100ms
1621
1622 if(diff_ns >= 100000000) {
1623 fifo_sr = readl((SPI_FIFO_SR_OFFSET+zx29spi->virtbase)) & SPI_FIFO_SR_MASK_BUSY;
1624 if(fifo_sr) {
1625 dev_info(&zx29spi->pdev->dev, "bus busy.. time_start=%lld diff_ns=%lld \n",k_time_start,diff_ns);
1626 ret = -EIO;
1627 }
1628 }
1629 return ret;
1630}
1631
1632static int zx29_spi_map_mssg(struct zx29_spi *zx29spi,
1633 struct spi_message *msg)
1634{
1635 struct device *dev;
1636 struct spi_transfer *transfer;
1637 int ret = 0;
1638 static int sc_debug_info_record_tx_cnt[4] ={0};
1639 static int sc_debug_info_record_rx_cnt[4] ={0};
1640
1641 if(!zx29spi || !msg)
1642 return -EFAULT;
1643
1644 if (msg->is_dma_mapped || !msg->spi->dma_used || !zx29spi->master_info->enable_dma) {
1645 return 0;
1646 }
1647 dev = &zx29spi->pdev->dev;
1648 /* Map until end or first fail */
1649 list_for_each_entry(transfer, &msg->transfers, transfer_list) {
1650
1651 if (/*transfer->len <= zx29spi->vendor->fifodepth ||*/ transfer->tx_dma || transfer->rx_dma )
1652 continue;
1653
1654 if (transfer->tx_buf != NULL) {
1655 transfer->tx_dma = dma_map_single(dev,(void *)transfer->tx_buf, transfer->len, DMA_TO_DEVICE);
1656 if (dma_mapping_error(dev, transfer->tx_dma)) {
1657 dev_err(dev, "dma_map_single spi Tx failed,times(%d)\n",sc_debug_info_record_tx_cnt[zx29spi->pdev->id]);
1658 if(sc_debug_info_record_tx_cnt[zx29spi->pdev->id] < 5)
1659 sc_debug_info_record(MODULE_ID_CAP_SPI, "%s tx_dma_map failed \n",zx29spi->pdev->name);
1660 transfer->tx_dma = 0;
1661 ret |= -ENOMEM;
1662 sc_debug_info_record_tx_cnt[zx29spi->pdev->id]++;
1663 }
1664 }
1665
1666 if (transfer->rx_buf != NULL) {
1667 transfer->rx_dma = dma_map_single(dev, transfer->rx_buf, transfer->len, DMA_FROM_DEVICE);
1668 if (dma_mapping_error(dev, transfer->rx_dma)) {
1669 dev_err(dev, "dma_map_single spi Rx failed,times(%d)\n",sc_debug_info_record_rx_cnt[zx29spi->pdev->id]);
1670 if(sc_debug_info_record_rx_cnt[zx29spi->pdev->id] < 5)
1671 sc_debug_info_record(MODULE_ID_CAP_SPI, "%s rx_dma_map failed \n",zx29spi->pdev->name);
1672 transfer->rx_dma = 0;
1673 ret |= -ENOMEM;
1674 sc_debug_info_record_rx_cnt[zx29spi->pdev->id]++;
1675 }
1676
1677 if (!transfer->rx_dma && transfer->tx_dma && transfer->tx_buf) {
1678 dma_unmap_single(dev, transfer->tx_dma, transfer->len, DMA_TO_DEVICE);
1679 transfer->tx_dma = 0;
1680 }
1681 }
1682 }
1683
1684
1685 return ret;
1686}
1687
1688static void zx29_spi_unmap_mssg(struct zx29_spi *zx29spi,
1689 struct spi_message *msg)
1690{
1691 struct device *dev = &zx29spi->pdev->dev;
1692 struct spi_transfer *transfer;
1693
1694 if (msg->is_dma_mapped || !msg->spi->dma_used || !zx29spi->master_info->enable_dma)
1695 return;
1696
1697 list_for_each_entry(transfer, &msg->transfers, transfer_list) {
1698
1699 if ( (!transfer->tx_buf && transfer->tx_dma) || (! transfer->rx_buf && transfer->rx_dma) )
1700 continue;
1701
1702 if (transfer->rx_buf != NULL && transfer->rx_dma)
1703 dma_unmap_single(dev, transfer->rx_dma, transfer->len, DMA_FROM_DEVICE);
1704
1705 if (transfer->tx_buf != NULL && transfer->tx_dma)
1706 dma_unmap_single(dev, transfer->tx_dma, transfer->len, DMA_TO_DEVICE);
1707 }
1708}
1709
1710static int zx29_slave_transfer_one_message(struct spi_master *master,
1711 struct spi_message *msg)
1712{
1713 struct zx29_spi *zx29spi = spi_master_get_devdata(master);
1714 struct spi_device *spi = msg->spi;
1715 struct spi_transfer *transfer;
1716 unsigned cs_change = 1;
1717 const int nsecs = 100;
1718 int ret = 0;
1719
1720 zx29spi->cur_msg = msg;
1721
1722 /* Setup the SPI using the per chip configuration */
1723 zx29spi->cur_chip = spi_get_ctldata(msg->spi);
1724 ret = zx29_spi_map_mssg(zx29spi, msg);
1725 /* continue with polling mode */
1726 if(ret){
1727 dev_info(&zx29spi->pdev->dev, "ret = %d\n",ret);
1728 goto out;
1729 }
1730 //restore_state(zx29spi);
1731 list_for_each_entry(transfer, &msg->transfers, transfer_list) {
1732
1733 zx29spi->cur_transfer = transfer;
1734 if((void *)transfer->tx_buf != NULL){
1735 zx29spi->tx = (void *)transfer->tx_buf;
1736 zx29spi->tx_end = zx29spi->tx + zx29spi->cur_transfer->len;
1737 }
1738 else
1739 zx29spi->tx = zx29spi->tx_end = NULL;
1740
1741 if((void *)transfer->rx_buf != NULL){
1742 zx29spi->rx = (void *)transfer->rx_buf;
1743 zx29spi->rx_end = zx29spi->rx + zx29spi->cur_transfer->len;
1744#if 0
1745 /*if tx is null, use rx buffer as a dummy tx buffer.*/
1746 if((void *)transfer->tx_buf == NULL){
1747 zx29spi->tx = (void *)transfer->rx_buf;
1748 zx29spi->tx_end = zx29spi->tx + zx29spi->cur_transfer->len;
1749 }
1750#endif
1751 }
1752 else
1753 zx29spi->rx = zx29spi->rx_end = NULL;
1754
1755 zx29spi->write = zx29spi->tx ? zx29spi->cur_chip->write : WRITING_NULL;
1756 zx29spi->read = zx29spi->rx ? zx29spi->cur_chip->read : READING_NULL;
1757
1758 if (/*transfer->rx_buf || */transfer->rx_dma)
1759 flush(zx29spi);
1760
1761 writel(CLEAR_ALL_INTERRUPTS, (SPI_INTR_SR_OFFSET+zx29spi->virtbase));
1762
1763 if (zx29spi->cur_chip->xfer_type == POLLING_TRANSFER || (!transfer->tx_dma && !transfer->rx_dma)) {
1764 if (zx29spi->tx < zx29spi->tx_end)
1765 zx29spi->cur_transfer->len = writer(zx29spi);
1766 if(zx29spi->rx < zx29spi->rx_end)
1767 zx29spi->cur_transfer->len = reader(zx29spi);
1768#if defined(CONFIG_DEBUG_FS)
1769 zx29spi->spi_poll_cnt ++;
1770#endif
1771 } else {
1772 struct chip_data *chip = zx29spi->cur_chip;
1773 if (transfer->rx_buf || transfer->rx_dma) {
1774 writel((chip->fifo_ctrl | (SPI_FIFO_CTRL_MASK_RX_DMA_EN | SPI_FIFO_CTRL_MASK_TX_DMA_EN)),
1775 (SPI_FIFO_CTRL_OFFSET+zx29spi->virtbase));
1776 } else {
1777 writel((chip->fifo_ctrl | SPI_FIFO_CTRL_MASK_TX_DMA_EN), (SPI_FIFO_CTRL_OFFSET+zx29spi->virtbase));
1778 }
1779 ret = zx29_slave_do_interrupt_dma_transfer(zx29spi);
1780#if defined(CONFIG_DEBUG_FS)
1781 zx29spi->spi_dma_cnt ++;
1782#endif
1783
1784 /* clear TX/RX DMA Enable */
1785 writel(chip->fifo_ctrl, (SPI_FIFO_CTRL_OFFSET+zx29spi->virtbase));
1786 }
1787
1788 if (ret) {
1789 pr_info("ssp:transfer error,transfer=%p\n", transfer);
1790 break;
1791 }
1792
1793 /* Update total byte transferred */
1794 msg->actual_length += zx29spi->cur_transfer->len;
1795
1796 if (transfer->delay_usecs)
1797 udelay(transfer->delay_usecs);
1798 }
1799out:
1800 zx29_spi_unmap_mssg(zx29spi, msg);
1801 msg->status = ret;
1802 spi_finalize_current_message(master);
1803
1804#if SPI_PSM_CONTROL
1805 zx29_spi_set_idle(&zx29spi->psm_lock);
1806#endif
1807
1808 return ret;
1809}
1810
1811static int zx29_transfer_one_message(struct spi_master *master,
1812 struct spi_message *msg)
1813{
1814 struct zx29_spi *zx29spi = spi_master_get_devdata(master);
1815 struct spi_device *spi = msg->spi;
1816 struct spi_transfer *transfer;
1817 unsigned cs_change = 1;
1818 const int nsecs = 100;
1819 int ret = 0;
1820 ktime_t k_time_start = 0;
1821 ktime_t diff_ns = 0;
1822 unsigned int reg_val = 0;
1823
1824 pm_stay_awake(&zx29spi->pdev->dev);
1825 //printk(KERN_INFO "ssp:in function %s \n", __FUNCTION__);
1826#if SPI_PSM_CONTROL
1827 zx29_spi_set_active(&zx29spi->psm_lock);
1828#endif
1829 //mutex_lock(&zx29spi->spi_lock);
1830 //printk(KERN_INFO "ssp:lock \n");
1831 /* Initial message state */
1832 zx29spi->cur_msg = msg;
1833 /* Setup the SPI using the per chip configuration */
1834 zx29spi->cur_chip = spi_get_ctldata(msg->spi);
1835
1836 if ((clk_get_rate(zx29spi->spi_clk) / 2) != spi->max_speed_hz) {
1837 clk_set_rate(zx29spi->spi_clk, spi->max_speed_hz * 2);
1838 }
1839
1840 restore_state(zx29spi);
1841
1842 ret = zx29_spi_map_mssg(zx29spi, msg);
1843 /* continue with polling mode */
1844 if(ret){
1845 dev_info(&zx29spi->pdev->dev, "ret = %d\n",ret);
1846 goto out;
1847 }
1848
1849
1850 //while (readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase)) & SPI_COM_CTRL_MASK_SSPE_BACK);
1851 k_time_start = ktime_get();
1852 do {
1853 diff_ns = ktime_sub(ktime_get(),k_time_start);
1854 reg_val = readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase)) & SPI_COM_CTRL_MASK_SSPE_BACK;
1855 cpu_relax();
1856 }
1857 while (reg_val && diff_ns < 100000000); //100ms
1858
1859 if(diff_ns >= 100000000) {
1860 reg_val = readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase)) & SPI_COM_CTRL_MASK_SSPE_BACK;
1861 if(reg_val) {
1862 dev_info(&zx29spi->pdev->dev, "wait sspe back time_out diff_ns=%lld \n",diff_ns);
1863 goto out;
1864 }
1865 }
1866
1867 writel(readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase)) | SPI_COM_CTRL_MASK_SSPE, (SPI_COM_CTRL_OFFSET+zx29spi->virtbase));
1868
1869 list_for_each_entry(transfer, &msg->transfers, transfer_list) {
1870
1871 zx29spi->cur_transfer = transfer;
1872 //if (transfer->bits_per_word || transfer->speed_hz)
1873 // dev_warn(&msg->spi->dev, "ignore bits & speed setting in transfer.");
1874
1875 if((void *)transfer->tx_buf != NULL){
1876 zx29spi->tx = (void *)transfer->tx_buf;
1877 zx29spi->tx_end = zx29spi->tx + zx29spi->cur_transfer->len;
1878 }
1879 else
1880 zx29spi->tx = zx29spi->tx_end = NULL;
1881
1882 if((void *)transfer->rx_buf != NULL){
1883 zx29spi->rx = (void *)transfer->rx_buf;
1884 zx29spi->rx_end = zx29spi->rx + zx29spi->cur_transfer->len;
1885
1886 /*if tx is null, use rx buffer as a dummy tx buffer.*/
1887 if((void *)transfer->tx_buf == NULL){
1888 zx29spi->tx = (void *)transfer->rx_buf;
1889 zx29spi->tx_end = zx29spi->tx + zx29spi->cur_transfer->len;
1890 }
1891 }
1892 else
1893 zx29spi->rx = zx29spi->rx_end = NULL;
1894
1895 zx29spi->write = zx29spi->tx ? zx29spi->cur_chip->write : WRITING_NULL;
1896 zx29spi->read = zx29spi->rx ? zx29spi->cur_chip->read : READING_NULL;
1897
1898 if (transfer->rx_buf || transfer->rx_dma)
1899 flush(zx29spi);
1900
1901 writel(CLEAR_ALL_INTERRUPTS, (SPI_INTR_SR_OFFSET+zx29spi->virtbase));
1902 if (cs_change) {
1903 zx29spi->cur_chip->cs_control(zx29spi->pdev->id,ZX29_CS_ACTIVE);
1904 }
1905
1906 cs_change = transfer->cs_change;
1907
1908 if (zx29spi->cur_chip->xfer_type == POLLING_TRANSFER || (!transfer->tx_dma && !transfer->rx_dma)) {
1909
1910 ret = zx29_do_polling_transfer(zx29spi);
1911 #if defined(CONFIG_DEBUG_FS)
1912 zx29spi->spi_poll_cnt ++;
1913 #endif
1914 } else {
1915 struct chip_data *chip = zx29spi->cur_chip;
1916
1917 if (transfer->rx_buf || transfer->rx_dma) {
1918 writel((chip->fifo_ctrl | (SPI_FIFO_CTRL_MASK_RX_DMA_EN | SPI_FIFO_CTRL_MASK_TX_DMA_EN)),
1919 (SPI_FIFO_CTRL_OFFSET+zx29spi->virtbase));
1920 } else {
1921 writel((chip->fifo_ctrl | SPI_FIFO_CTRL_MASK_TX_DMA_EN), (SPI_FIFO_CTRL_OFFSET+zx29spi->virtbase));
1922 }
1923
1924 ret = zx29_do_interrupt_dma_transfer(zx29spi);
1925 #if defined(CONFIG_DEBUG_FS)
1926 zx29spi->spi_dma_cnt ++;
1927 #endif
1928
1929 /* clear TX/RX DMA Enable */
1930 writel(chip->fifo_ctrl, (SPI_FIFO_CTRL_OFFSET+zx29spi->virtbase));
1931 }
1932
1933 if (ret) {
1934 pr_info("ssp:transfer error,transfer=%p\n", transfer);
1935 break;
1936 }
1937
1938 /* Update total byte transferred */
1939 msg->actual_length += zx29spi->cur_transfer->len;
1940
1941 if (transfer->delay_usecs)
1942 udelay(transfer->delay_usecs);
1943
1944 if (cs_change) {
1945 zx29spi->cur_chip->cs_control(zx29spi->pdev->id,ZX29_CS_INACTIVE);
1946 ndelay(nsecs);
1947 }
1948 }
1949 if (ret || !cs_change) {
1950 zx29spi->cur_chip->cs_control(zx29spi->pdev->id,ZX29_CS_INACTIVE);
1951 }
1952 //while (~ readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase)) & SPI_COM_CTRL_MASK_SSPE_BACK);
1953 k_time_start = ktime_get();
1954 do {
1955 diff_ns = ktime_sub(ktime_get(),k_time_start);
1956 reg_val = readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase)) & SPI_COM_CTRL_MASK_SSPE_BACK;
1957 cpu_relax();
1958 }
1959 while (!reg_val && diff_ns < 100000000); //100ms
1960
1961 if(diff_ns >= 100000000) {
1962 reg_val = readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase)) & SPI_COM_CTRL_MASK_SSPE_BACK;
1963 if(!reg_val) {
1964 dev_info(&zx29spi->pdev->dev, "wait sspe back time_out diff_ns=%lld \n",diff_ns);
1965 goto out;
1966 }
1967 }
1968 writel(readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase)) & ~ SPI_COM_CTRL_MASK_SSPE, (SPI_COM_CTRL_OFFSET+zx29spi->virtbase));
1969
1970out:
1971 zx29_spi_unmap_mssg(zx29spi, msg);
1972 //mutex_unlock(&zx29spi->spi_lock);
1973 //printk(KERN_INFO "ssp:unlock \n");
1974
1975 msg->status = ret;
1976 spi_finalize_current_message(master);
1977
1978#if SPI_PSM_CONTROL
1979 zx29_spi_set_idle(&zx29spi->psm_lock);
1980#endif
1981 pm_relax(&zx29spi->pdev->dev);
1982 return ret;
1983}
1984
xf.li1867bfa2024-08-20 02:32:16 -07001985/* yu.dong@20240715 [T106BUG-641] SPI packet loss problem, merged into ZXW patch start */
xf.li2f424182024-08-20 00:47:34 -07001986/* yu.dong@20240617 [T106BUG-641] SPI packet loss problem, add kernel buffer scheme start */
xf.li1867bfa2024-08-20 02:32:16 -07001987#define SPI_SLAVE_RX_BUFF_SIZE 4096
1988#define SPI_SLAVE_RX_MAX_PACK_NUM 15
1989#define SPI_SLAVE_RX_PACK_LEN 146
1990#define SPI_SLAVE_RX_LIST_BUFF_LEN (SPI_SLAVE_RX_MAX_PACK_NUM*SPI_SLAVE_RX_PACK_LEN)
1991static dma_channel_def slave_rx_conf[SPI_SLAVE_RX_MAX_PACK_NUM] = {0};
xf.li2f424182024-08-20 00:47:34 -07001992//yu.dong@20240617 [T106BUG-641] SPI packet loss issue, increase kernel buffer and read all cached data away, no data loss.
1993#define SPI_MAGIC 0x55555555
1994static bool rxbuf_is_free_space(struct spi_device *spi)
1995{
1996 if (spi->recv_pos < spi->rd_pos) {
xf.li1867bfa2024-08-20 02:32:16 -07001997 if ((spi->rd_pos - spi->recv_pos) > SPI_SLAVE_RX_PACK_LEN)
xf.li2f424182024-08-20 00:47:34 -07001998 return 1;
1999 else
2000 return 0;
2001 }
2002 else {
xf.li1867bfa2024-08-20 02:32:16 -07002003 if ((SPI_SLAVE_RX_BUFF_SIZE - spi->recv_pos + spi->rd_pos ) > SPI_SLAVE_RX_PACK_LEN)
xf.li2f424182024-08-20 00:47:34 -07002004 return 1;
2005 else
2006 return 0;
2007 }
2008}
2009
2010/* yu.dong@20240617 [T106BUG-641] SPI packet loss issue, increase kernel buffer and read all cached data away, no data loss start */
2011static void dma_cyclic_callback(void *data)
2012{
2013 struct spi_device *spi = (struct spi_device *)data;
2014 struct zx29_spi *zx29spi = NULL;
2015 int index = 0;
2016 unsigned int end = 0;
2017
2018 zx29spi = spi_master_get_devdata(spi->master);
2019 zx29spi->spi_poll_cnt++;
xf.li1867bfa2024-08-20 02:32:16 -07002020 end = *(volatile unsigned int *)(spi->cyc_buf +spi->cyc_index * SPI_SLAVE_RX_PACK_LEN + SPI_SLAVE_RX_PACK_LEN - 4);
2021 while((end != SPI_MAGIC) && index < SPI_SLAVE_RX_MAX_PACK_NUM) {
xf.li2f424182024-08-20 00:47:34 -07002022 if(!rxbuf_is_free_space(spi)) {
2023 printk("rx_buff not enough space!!!!!");
2024 zx29spi->spi_dma_cnt++;
2025 break;
2026 }else {
xf.li1867bfa2024-08-20 02:32:16 -07002027 if((spi->recv_pos + SPI_SLAVE_RX_PACK_LEN) <= SPI_SLAVE_RX_BUFF_SIZE) {
2028 memcpy(spi->rx_buf + spi->recv_pos,spi->cyc_buf + spi->cyc_index * SPI_SLAVE_RX_PACK_LEN,SPI_SLAVE_RX_PACK_LEN);
xf.li2f424182024-08-20 00:47:34 -07002029 }else {
xf.li1867bfa2024-08-20 02:32:16 -07002030 memcpy(spi->rx_buf + spi->recv_pos,spi->cyc_buf + spi->cyc_index * SPI_SLAVE_RX_PACK_LEN,SPI_SLAVE_RX_BUFF_SIZE - spi->recv_pos);
2031 memcpy(spi->rx_buf,spi->cyc_buf + spi->cyc_index * SPI_SLAVE_RX_PACK_LEN + (SPI_SLAVE_RX_BUFF_SIZE - spi->recv_pos),SPI_SLAVE_RX_PACK_LEN-(SPI_SLAVE_RX_BUFF_SIZE-spi->recv_pos));
xf.li2f424182024-08-20 00:47:34 -07002032 }
xf.li1867bfa2024-08-20 02:32:16 -07002033 *(volatile unsigned int *)(spi->cyc_buf +spi->cyc_index * SPI_SLAVE_RX_PACK_LEN + SPI_SLAVE_RX_PACK_LEN - 4) = SPI_MAGIC;
2034 spi->recv_pos = (spi->recv_pos + SPI_SLAVE_RX_PACK_LEN)%SPI_SLAVE_RX_BUFF_SIZE;
2035 spi->cyc_index = (spi->cyc_index + 1)%SPI_SLAVE_RX_MAX_PACK_NUM;
xf.li2f424182024-08-20 00:47:34 -07002036
2037 zx29spi->spi_dma_cnt++;
2038 index++;
xf.li1867bfa2024-08-20 02:32:16 -07002039 end = *(volatile unsigned int *)(spi->cyc_buf +spi->cyc_index * SPI_SLAVE_RX_PACK_LEN + SPI_SLAVE_RX_PACK_LEN - 4);
xf.li2f424182024-08-20 00:47:34 -07002040 }
2041
2042 if(spi->is_rd_waiting == true && spi->recv_done == 0) {
2043 wake_up(&spi->rd_wait);
2044 spi->recv_done = 1;
2045 }
2046 }
xf.li1867bfa2024-08-20 02:32:16 -07002047 if((end != SPI_MAGIC) && index == SPI_SLAVE_RX_MAX_PACK_NUM)
xf.li2f424182024-08-20 00:47:34 -07002048 printk("cyc_buf be covered!!!!!");
2049 return;
2050}
2051/* yu.dong@20240617 [T106BUG-641] SPI packet loss issue, increase kernel buffer and read all cached data away, no data loss end */
2052
2053static int zx29_slave_config_dma(struct zx29_spi *zx29spi,struct spi_device *spi)
2054{
2055 struct chip_data *chip = NULL;
2056 struct dma_chan *rxchan = NULL;
2057 struct dma_async_tx_descriptor *rxdesc;
xf.li1867bfa2024-08-20 02:32:16 -07002058 unsigned short transfer_len = SPI_SLAVE_RX_PACK_LEN;
xf.li2f424182024-08-20 00:47:34 -07002059 int i;
2060
2061 chip = zx29spi->cur_chip = spi->controller_state;
2062
2063 if (spi->rx_dma)
2064 flush(zx29spi);
2065 writel(CLEAR_ALL_INTERRUPTS, (SPI_INTR_SR_OFFSET+zx29spi->virtbase));
2066 writel((chip->fifo_ctrl | SPI_FIFO_CTRL_MASK_RX_DMA_EN), (SPI_FIFO_CTRL_OFFSET+zx29spi->virtbase));
2067
2068 zx29spi->write = zx29spi->tx ? zx29spi->cur_chip->write : WRITING_NULL;
2069 zx29spi->read = zx29spi->rx ? zx29spi->cur_chip->read : READING_NULL;
2070 rxchan = zx29spi->dma_rx_channel;
2071 /* Check that the channels are available */
2072 if (!rxchan)
2073 return -ENODEV;
2074
2075 /*
2076 * If supplied, the DMA burstsize should equal the FIFO trigger level.
2077 * Notice that the DMA engine uses one-to-one mapping. Since we can
2078 * not trigger on 2 elements this needs explicit mapping rather than
2079 * calculation.
2080 */
xf.li1867bfa2024-08-20 02:32:16 -07002081 for(i = 0;i < SPI_SLAVE_RX_MAX_PACK_NUM;i++) {
xf.li2f424182024-08-20 00:47:34 -07002082 switch (zx29spi->rx_lev_trig) {
2083 case SPI_RX_1_OR_MORE_ELEM:
2084 slave_rx_conf[i].dma_control.src_burst_len = DMA_BURST_LEN_1;
2085 slave_rx_conf[i].dma_control.dest_burst_len = DMA_BURST_LEN_1;
2086 break;
2087 case SPI_RX_4_OR_MORE_ELEM:
2088 slave_rx_conf[i].dma_control.src_burst_len = DMA_BURST_LEN_4;
2089 slave_rx_conf[i].dma_control.dest_burst_len = DMA_BURST_LEN_4;
2090 break;
2091 case SPI_RX_8_OR_MORE_ELEM:
2092 slave_rx_conf[i].dma_control.src_burst_len = DMA_BURST_LEN_8;
2093 slave_rx_conf[i].dma_control.dest_burst_len = DMA_BURST_LEN_8;
2094 break;
2095 case SPI_RX_16_OR_MORE_ELEM:
2096 slave_rx_conf[i].dma_control.src_burst_len = DMA_BURST_LEN_16;
2097 slave_rx_conf[i].dma_control.dest_burst_len = DMA_BURST_LEN_16;
2098 break;
2099 case SPI_RX_32_OR_MORE_ELEM:
2100 slave_rx_conf[i].dma_control.src_burst_len = DMA_BURST_LEN_ALL;
2101 slave_rx_conf[i].dma_control.dest_burst_len = DMA_BURST_LEN_ALL;
2102 break;
2103 default:
2104 slave_rx_conf[i].dma_control.src_burst_len = zx29spi->vendor->fifodepth >> 1;
2105 slave_rx_conf[i].dma_control.dest_burst_len = zx29spi->vendor->fifodepth >> 1;
2106 break;
2107 }
2108
2109 switch (zx29spi->read) {
2110 case READING_NULL:
2111 /* Use the same as for writing */
2112 slave_rx_conf[i].dma_control.src_burst_size = DMA_BURST_SIZE_8BIT;
2113 slave_rx_conf[i].dma_control.dest_burst_size = DMA_BURST_SIZE_8BIT;
2114 slave_rx_conf[i].count = transfer_len;
2115 break;
2116 case READING_U8:
2117 slave_rx_conf[i].dma_control.src_burst_size = DMA_BURST_SIZE_8BIT;
2118 slave_rx_conf[i].dma_control.dest_burst_size = DMA_BURST_SIZE_8BIT;
2119 slave_rx_conf[i].count = transfer_len;
2120 break;
2121 case READING_U16:
2122 slave_rx_conf[i].dma_control.src_burst_size = DMA_BURST_SIZE_16BIT;
2123 slave_rx_conf[i].dma_control.dest_burst_size = DMA_BURST_SIZE_16BIT;
2124 slave_rx_conf[i].count = transfer_len;
2125 break;
2126 case READING_U32:
2127 slave_rx_conf[i].dma_control.src_burst_size = DMA_BURST_SIZE_32BIT;
2128 slave_rx_conf[i].dma_control.dest_burst_size = DMA_BURST_SIZE_32BIT;
2129 slave_rx_conf[i].count = transfer_len;
2130 break;
2131 }
2132
2133 slave_rx_conf[i].src_addr = (SPI_DR_OFFSET+zx29spi->phybase);
2134 slave_rx_conf[i].dma_control.tran_mode = TRAN_PERI_TO_MEM;
2135 slave_rx_conf[i].dma_control.irq_mode = DMA_ALL_IRQ_ENABLE;
2136 slave_rx_conf[i].dest_addr = (unsigned int)spi->rx_dma + transfer_len*i;
2137 slave_rx_conf[i].link_addr = 1;
2138 //yu.dong@20240617 [T106BUG-641] SPI packet loss issue, increase kernel buffer and read all cached data away, no data loss.
2139 *(volatile unsigned int *)(spi->cyc_buf + transfer_len*i + transfer_len -4) = SPI_MAGIC;
2140 }
2141
2142 dmaengine_slave_config(rxchan,(struct dma_slave_config*)&slave_rx_conf[0]);
2143
2144 /* Submit and fire RX and TX with TX last so we're ready to read! */
2145 if (spi->rx_dma) {
xf.li1867bfa2024-08-20 02:32:16 -07002146 rxdesc = rxchan->device->device_prep_dma_cyclic(rxchan,NULL,SPI_SLAVE_RX_MAX_PACK_NUM * SPI_SLAVE_RX_PACK_LEN, SPI_SLAVE_RX_PACK_LEN,0,0);
xf.li2f424182024-08-20 00:47:34 -07002147 if (!rxdesc) {
2148 printk(KERN_INFO "!!ERROR DESC !!![%s][%d]\n",__func__,__LINE__);
2149 dmaengine_terminate_all(rxchan);
2150 return -EBUSY;
2151 }
2152 /* Put the callback on the RX transfer only, that should finish last */
2153 rxdesc->callback = dma_cyclic_callback;
2154 rxdesc->callback_param = spi;
2155 dmaengine_submit(rxdesc);
2156 dma_async_issue_pending(rxchan);
2157 zx29spi->dma_running = RX_TRANSFER;
2158 }
2159 return 0;
2160}
2161
2162static int zx29_slave_rd_start(struct spi_device *spi)
2163{
2164 struct zx29_spi *zx29spi = NULL;
2165 struct device *dev;
2166 int status = 0;
2167 static int wd_wait_queue_init = 0;
2168
2169 printk("zx29_slave_rd_start...\r\n");
2170
2171 zx29spi = spi_master_get_devdata(spi->master);
xf.li2f424182024-08-20 00:47:34 -07002172 if (!zx29spi)
2173 return -EINVAL;
xf.li1867bfa2024-08-20 02:32:16 -07002174 dev = &zx29spi->pdev->dev;
xf.li2f424182024-08-20 00:47:34 -07002175 spi->cyc_index = 0;
2176 spi->rd_pos = spi->recv_pos = 0;
2177
xf.li1867bfa2024-08-20 02:32:16 -07002178 spi->cyc_buf = dma_alloc_coherent(dev, SPI_SLAVE_RX_BUFF_SIZE, &spi->rx_dma, GFP_KERNEL);
xf.li2f424182024-08-20 00:47:34 -07002179 if (dma_mapping_error(dev, spi->rx_dma)) {
2180 dev_err(dev, "dma_map_single spi rx failed\n");
2181 return -ENOMEM;
2182 }
2183
2184 if(wd_wait_queue_init == 0) {
2185 init_waitqueue_head(&spi->rd_wait);
2186 spi->recv_done = false;
2187 spi->is_rd_waiting = false;
2188 wd_wait_queue_init = 1;
2189 }
2190 status = zx29_slave_config_dma(zx29spi,spi);
2191
2192 return status;
2193}
2194
2195static int zx29_slave_rd_stop(struct spi_device *spi)
2196{
2197 struct zx29_spi *zx29spi = NULL;
2198 struct device *dev;
2199 int status = 0;
2200 struct chip_data *chip = NULL;
2201 struct dma_chan *rxchan = NULL;
2202
2203 zx29spi = spi_master_get_devdata(spi->master);
xf.li2f424182024-08-20 00:47:34 -07002204 if (!zx29spi)
2205 return -EINVAL;
xf.li1867bfa2024-08-20 02:32:16 -07002206 dev = &zx29spi->pdev->dev;
xf.li2f424182024-08-20 00:47:34 -07002207 chip = zx29spi->cur_chip= spi->controller_state;
2208 writel(chip->fifo_ctrl, (SPI_FIFO_CTRL_OFFSET+zx29spi->virtbase));
2209 rxchan = zx29spi->dma_rx_channel;
2210 /* Submit and fire RX and TX with TX last so we're ready to read! */
2211 if(spi->rx_dma) {
2212 dmaengine_terminate_all(rxchan);
2213 zx29spi->dma_running = 0;
2214 }
2215
2216 if(spi->cyc_buf != NULL && spi->rx_dma) {
xf.li1867bfa2024-08-20 02:32:16 -07002217 dma_free_coherent(dev, SPI_SLAVE_RX_BUFF_SIZE, spi->cyc_buf, spi->rx_dma);
xf.li2f424182024-08-20 00:47:34 -07002218 spi->cyc_buf = NULL;
2219 }
2220
2221 spi->cyc_index = 0;
2222 spi->rd_pos = spi->recv_pos = 0;
2223 spi->recv_done = false;
2224 spi->is_rd_waiting = false;
2225 printk("zx29_slave_rd_stop...\r\n");
2226
2227 return status;
2228}
2229/* yu.dong@20240617 [T106BUG-641] SPI packet loss problem, add kernel buffer scheme end */
xf.li1867bfa2024-08-20 02:32:16 -07002230/* yu.dong@20240715 [T106BUG-641] SPI packet loss problem, merged into ZXW patch end*/
xf.li2f424182024-08-20 00:47:34 -07002231
2232static int zx29_prepare_transfer_hardware(struct spi_master *master)
2233{
2234
2235 return 0;
2236}
2237
2238static int zx29_unprepare_transfer_hardware(struct spi_master *master)
2239{
2240 //struct zx29_spi *zx29spi = spi_master_get_devdata(master);
2241
2242 //dev_warn(&zx29spi->pdev->dev,"in function %s\n", __FUNCTION__);
2243
2244 /* nothing more to do - disable spi/ssp and power off */
2245 //writel(readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase)) & ~ SPI_COM_CTRL_MASK_SSPE, (SPI_COM_CTRL_OFFSET+zx29spi->virtbase));
2246
2247 return 0;
2248}
2249
2250static int verify_controller_parameters(struct zx29_spi *zx29spi,
2251 struct spi_config_chip const *chip_info)
2252{
2253 if ((chip_info->iface < SPI_INTERFACE_MOTOROLA_SPI)
2254 || (chip_info->iface > SPI_INTERFACE_ISI_SPI)) {
2255 dev_err(&zx29spi->pdev->dev,
2256 "interface is configured incorrectly\n");
2257 return -EINVAL;
2258 }
2259
2260 if ((chip_info->hierarchy != SPI_MASTER)
2261 && (chip_info->hierarchy != SPI_SLAVE)) {
2262 dev_err(&zx29spi->pdev->dev,
2263 "hierarchy is configured incorrectly\n");
2264 return -EINVAL;
2265 }
2266 if ((chip_info->com_mode != INTERRUPT_TRANSFER)
2267 && (chip_info->com_mode != DMA_TRANSFER)
2268 && (chip_info->com_mode != POLLING_TRANSFER)) {
2269 dev_err(&zx29spi->pdev->dev,
2270 "Communication mode is configured incorrectly\n");
2271 return -EINVAL;
2272 }
2273 switch (chip_info->rx_lev_trig) {
2274 case SPI_RX_1_OR_MORE_ELEM:
2275 case SPI_RX_4_OR_MORE_ELEM:
2276 case SPI_RX_8_OR_MORE_ELEM:
2277 /* These are always OK, all variants can handle this */
2278 break;
2279 case SPI_RX_16_OR_MORE_ELEM:
2280 if (zx29spi->vendor->fifodepth < 16) {
2281 dev_err(&zx29spi->pdev->dev,
2282 "RX FIFO Trigger Level is configured incorrectly\n");
2283 return -EINVAL;
2284 }
2285 break;
2286 case SPI_RX_32_OR_MORE_ELEM:
2287 if (zx29spi->vendor->fifodepth < 32) {
2288 dev_err(&zx29spi->pdev->dev,
2289 "RX FIFO Trigger Level is configured incorrectly\n");
2290 return -EINVAL;
2291 }
2292 break;
2293 default:
2294 dev_err(&zx29spi->pdev->dev,
2295 "RX FIFO Trigger Level is configured incorrectly\n");
2296 return -EINVAL;
2297 break;
2298 }
2299 switch (chip_info->tx_lev_trig) {
2300 case SPI_TX_1_OR_MORE_EMPTY_LOC:
2301 case SPI_TX_4_OR_MORE_EMPTY_LOC:
2302 case SPI_TX_8_OR_MORE_EMPTY_LOC:
2303 /* These are always OK, all variants can handle this */
2304 break;
2305 case SPI_TX_16_OR_MORE_EMPTY_LOC:
2306 if (zx29spi->vendor->fifodepth < 16) {
2307 dev_err(&zx29spi->pdev->dev,
2308 "TX FIFO Trigger Level is configured incorrectly\n");
2309 return -EINVAL;
2310 }
2311 break;
2312 case SPI_TX_32_OR_MORE_EMPTY_LOC:
2313 if (zx29spi->vendor->fifodepth < 32) {
2314 dev_err(&zx29spi->pdev->dev,
2315 "TX FIFO Trigger Level is configured incorrectly\n");
2316 return -EINVAL;
2317 }
2318 break;
2319 default:
2320 dev_err(&zx29spi->pdev->dev,
2321 "TX FIFO Trigger Level is configured incorrectly\n");
2322 return -EINVAL;
2323 break;
2324 }
2325
2326 return 0;
2327}
2328
2329static struct vendor_data vendor_arm = {
2330 .fifodepth = 16,
2331 .max_bpw = 32,
2332 .loopback = true,
2333};
2334
2335/*
2336 * A piece of default chip info unless the platform
2337 * supplies it.
2338 */
2339static const struct spi_config_chip spi_default_chip_info = {
2340 .com_mode = DMA_TRANSFER,//INTERRUPT_TRANSFER,//POLLING_TRANSFER,
2341 .iface = SPI_INTERFACE_MOTOROLA_SPI,
2342 .hierarchy = SPI_MASTER,
2343 .slave_tx_disable = DO_NOT_DRIVE_TX,
l.yangafee7ee2024-10-10 15:01:10 +08002344 /* yu.dong@20240521 [T106BUG-616] SPI set to slave mode for read will get stuck start */
xf.li1867bfa2024-08-20 02:32:16 -07002345 .rx_lev_trig = SPI_RX_8_OR_MORE_ELEM,
l.yangafee7ee2024-10-10 15:01:10 +08002346 /* yu.dong@20240521 [T106BUG-616] SPI set to slave mode for read will get stuck end */
xf.li2f424182024-08-20 00:47:34 -07002347 .tx_lev_trig = SPI_TX_4_OR_MORE_EMPTY_LOC,
2348// .ctrl_len = SSP_BITS_8,
2349// .wait_state = SSP_MWIRE_WAIT_ZERO,
2350// .duplex = SSP_MICROWIRE_CHANNEL_FULL_DUPLEX,
2351// .cs_control = default_cs_control,
2352};
2353
2354/*
2355
2356*/
2357static void spi_set_gpio_function(int dev_id)
2358{
2359 if (pinctrl_select_state(ssp_pins[dev_id].pctrl, ssp_pins[dev_id].pfunc) < 0) {
2360 printk("spi%d setting spi pin ctrl failed\n",dev_id);
2361 }
2362 return;
2363}
2364static void spi_set_gpio_gpio(int dev_id)
2365{
2366 if (pinctrl_select_state(ssp_pins[dev_id].pctrl, ssp_pins[dev_id].pgpio) < 0) {
2367 printk("spi%d setting spi pin ctrl failed\n",dev_id);
2368 }
2369 return;
2370}
2371
2372
2373static void spi_set_gpio_val(int gpio_num, int val)
2374{
2375 //zx29_gpio_output_data(gpio_num, val);
2376 gpio_set_value(gpio_num,val);
2377
2378}
2379
2380static int spi_get_gpio_val(int gpio_num)
2381{
2382 //zx29_gpio_set_direction(gpio,GPIO_IN);
2383
2384 return gpio_get_value(gpio_num);
2385}
2386
2387static void spi_time_delay(int delay/*us*/)
2388{
2389 udelay(delay);
2390}
2391
2392void spi_fun_mode_stop(int dev_id)
2393{
2394 spi_set_gpio_gpio(dev_id);
2395}
2396
2397void spi_gpio_mode_start(int dev_id)
2398{
2399 //mutex_lock(&g_zx29_spi->spi_lock); //spi control function mutex.
2400 /* set clk tx rx cs to gpio */
2401 //spi_set_gpio_gpio(dev_id);
2402 gpio_direction_output(ssp_pins[dev_id].gpio_cs,SPI_GPIO_HIGH);
2403 gpio_direction_output(ssp_pins[dev_id].gpio_clk,SPI_GPIO_LOW);
2404 gpio_direction_output(ssp_pins[dev_id].gpio_tx,0);//value ?
2405 gpio_direction_input(ssp_pins[dev_id].gpio_rx);
2406
2407 return ;
2408}
2409EXPORT_SYMBOL(spi_gpio_mode_start);
2410void spi_gpio_mode_stop(int dev_id)
2411{
2412 /* set clk tx rx cs to function */
2413 spi_set_gpio_function(dev_id);
2414 //mutex_unlock(&g_zx29_spi->spi_lock); //spi control function mutex.
2415}
2416EXPORT_SYMBOL(spi_gpio_mode_stop);
2417
2418void spi_gpio_write_single8(int dev_id,unsigned char data)
2419{
2420 int i;
2421
2422 //printk("spi_gpio_write_single8 %x\n", data);
2423
2424 spi_set_gpio_val(ssp_pins[dev_id].gpio_clk, SPI_GPIO_HIGH);
2425 spi_set_gpio_val(ssp_pins[dev_id].gpio_cs, SPI_GPIO_LOW);/* CS invail*/
2426
2427 for( i=7; i>=0; i-- )
2428 {
2429 spi_set_gpio_val(ssp_pins[dev_id].gpio_clk, SPI_GPIO_LOW);
2430 if ((data >> i) & 0x1)
2431 {
2432 spi_set_gpio_val(ssp_pins[dev_id].gpio_tx, SPI_GPIO_HIGH);
2433 }
2434 else
2435 {
2436 spi_set_gpio_val(ssp_pins[dev_id].gpio_tx, SPI_GPIO_LOW);
2437 }
2438 spi_time_delay(1);
2439 spi_set_gpio_val(ssp_pins[dev_id].gpio_clk, SPI_GPIO_HIGH);
2440 spi_time_delay(1);
2441 }
2442 spi_set_gpio_val(ssp_pins[dev_id].gpio_cs, SPI_GPIO_HIGH);
2443 spi_set_gpio_val(ssp_pins[dev_id].gpio_tx, SPI_GPIO_LOW);
2444
2445}
2446EXPORT_SYMBOL(spi_gpio_write_single8);
2447/*******************************************************************************
2448 * Function:
2449 * Description:
2450 * Parameters:
2451 * Input:
2452 *
2453 * Output:
2454 *
2455 * Returns:
2456 *
2457 *
2458 * Others:
2459 ********************************************************************************/
2460unsigned char spi_gpio_read_single8(int dev_id)
2461{
2462 int i;
2463 unsigned char readData = 0;
2464
2465 spi_set_gpio_val(ssp_pins[dev_id].gpio_clk, SPI_GPIO_HIGH);
2466 spi_set_gpio_val(ssp_pins[dev_id].gpio_cs, SPI_GPIO_LOW);/* CS */
2467
2468 for( i=7; i>=0; i-- )
2469 {
2470 spi_set_gpio_val(ssp_pins[dev_id].gpio_clk, SPI_GPIO_LOW);
2471 spi_time_delay(1);
2472 spi_set_gpio_val(ssp_pins[dev_id].gpio_clk, SPI_GPIO_HIGH);
2473 if( spi_get_gpio_val(ssp_pins[dev_id].gpio_rx) )/* lcd tx rx */
2474 {
2475 readData |= (1 << i);
2476 }
2477 spi_time_delay(1);
2478 }
2479 spi_set_gpio_val(ssp_pins[dev_id].gpio_cs, SPI_GPIO_HIGH);
2480
2481 //printk("spi_gpio_read_single8 %x\n", readData);
2482 return readData;
2483}
2484EXPORT_SYMBOL(spi_gpio_read_single8);
2485
2486/**
2487 * @brief spi gpio mode, cs control
2488 *
2489 * This function used for lcd 3-wires spi mode.
2490 * before cs pull down, spi pads will change to gpio mode.
2491 * after cs pull high, spi pads gpio mode recovery to spi mode.
2492 *
2493 * @param level 0: cs line pull down, no-zero: cs line pull up.
2494 *
2495 * @retval none
2496 */
2497void spi_gpio_3wire_cs(int dev_id,unsigned char level)
2498{
2499 if(level){
2500 spi_set_gpio_val(ssp_pins[dev_id].gpio_cs,SPI_GPIO_HIGH);
2501 gpio_direction_input(ssp_pins[dev_id].gpio_tx);
2502
2503 /* zx29_gpio_function_sel(GPIO_AP_SPI0_CS, GPIO_AP_SPI0_CS_FUN); */
2504 //zx29_gpio_function_sel(GPIO_AP_SPI0_CLK, GPIO_AP_SPI0_CLK_FUN);
2505 //zx29_gpio_function_sel(GPIO_AP_SPI0_TXD, GPIO_AP_SPI0_TXD_FUN);
2506
2507 //mutex_unlock(&g_zx29_spi->spi_lock); //spi control function mutex.
2508 }
2509 else{
2510 //mutex_lock(&g_zx29_spi->spi_lock);
2511
2512 /* zx29_gpio_function_sel(GPIO_AP_SPI0_CS, GPIO_AP_CS_GPIO_FUN); */
2513 //zx29_gpio_function_sel(GPIO_AP_SPI0_CLK, GPIO_AP_CLK_GPIO_FUN);
2514 //zx29_gpio_function_sel(GPIO_AP_SPI0_TXD, GPIO_AP_TXD_GPIO_FUN);
2515
2516 gpio_direction_output(ssp_pins[dev_id].gpio_cs,SPI_GPIO_LOW);
2517 gpio_direction_output(ssp_pins[dev_id].gpio_clk,SPI_GPIO_LOW);
2518 gpio_direction_output(ssp_pins[dev_id].gpio_tx,SPI_GPIO_LOW);
2519
2520 spi_set_gpio_val(ssp_pins[dev_id].gpio_clk,SPI_GPIO_LOW);
2521 spi_set_gpio_val(ssp_pins[dev_id].gpio_cs,SPI_GPIO_LOW);
2522 }
2523}
2524EXPORT_SYMBOL(spi_gpio_3wire_cs);
2525
2526/**
2527 * @brief spi gpio mode, one byte write.
2528 *
2529 * This function used for lcd 3-wires spi mode.
2530 * txd line used tx function and rx function at different time.
2531 *
2532 * @param reg one byte write data.
2533 *
2534 * @retval none
2535 */
2536void spi_gpio_3wire_write8(int dev_id,unsigned char reg)
2537{
2538 int i;
2539 //unsigned char readData = 0;
2540
2541 //write
2542 spi_time_delay(50);
2543 for (i = 0; i < 8; i++)
2544 {
2545 gpio_set_value(ssp_pins[dev_id].gpio_clk, SPI_GPIO_LOW);
2546 spi_time_delay(50);
2547
2548 if ((reg & 0x80)==0x80)
2549 {
2550 gpio_set_value(ssp_pins[dev_id].gpio_tx, SPI_GPIO_HIGH);
2551 }
2552 else
2553 {
2554 gpio_set_value(ssp_pins[dev_id].gpio_tx, SPI_GPIO_LOW);
2555 }
2556 spi_time_delay(50);
2557
2558 gpio_set_value(ssp_pins[dev_id].gpio_clk, SPI_GPIO_HIGH);
2559 spi_time_delay(50);
2560
2561 reg <<= 1;
2562 }
2563 //spi_time_delay(50);
2564}
2565EXPORT_SYMBOL(spi_gpio_3wire_write8);
2566
2567/**
2568 * @brief spi gpio mode, one byte read.
2569 *
2570 * This function used for lcd 3-wires spi mode.
2571 * txd line used tx function and rx function at different time.
2572 *
2573 * @param none.
2574 *
2575 * @retval one byte readed data.
2576 */
2577unsigned char spi_gpio_3wire_read8(int dev_id)
2578{
2579 int i;
2580 unsigned char readData = 0;
2581 //read
2582 gpio_direction_input(ssp_pins[dev_id].gpio_tx);
2583 spi_time_delay(50);
2584
2585 readData = 0;
2586 for (i = 0; i < 8; i++)
2587 {
2588 readData <<= 1;
2589 gpio_set_value(ssp_pins[dev_id].gpio_clk, SPI_GPIO_LOW);
2590 spi_time_delay(50);
2591
2592 if (SPI_GPIO_HIGH == gpio_get_value(ssp_pins[dev_id].gpio_tx))
2593 {
2594 readData |= 0x01;
2595 }
2596
2597 gpio_set_value(ssp_pins[dev_id].gpio_clk, SPI_GPIO_HIGH);
2598 spi_time_delay(50);
2599 }
2600 //spi_time_delay(50);
2601
2602 //printk("spi_gpio_read_single8 %x\n", readData);
2603 return readData;
2604}
2605EXPORT_SYMBOL(spi_gpio_3wire_read8);
2606
2607
2608static void zx29_setup_to_regs(struct chip_data *chip,struct zx29_spi *zx29spi)
2609{
2610 unsigned int regval = 0;
2611 ktime_t k_time_start = 0;
2612 ktime_t k_time_end = 0;
2613 ktime_t diff_ns = 0;
xf.li1867bfa2024-08-20 02:32:16 -07002614 /* yu.dong@20240715 [T106BUG-641] SPI packet loss problem, merged into ZXW patch start */
2615 /* yu.dong@20240617 [T106BUG-641] SPI packet loss problem, add kernel buffer scheme start */
2616 if(false == zx29spi->master->slave)
2617 pm_stay_awake(&zx29spi->pdev->dev);
xf.li2f424182024-08-20 00:47:34 -07002618 regval = readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase)) & (~SPI_COM_CTRL_MASK_SSPE);
2619 writel(regval, (SPI_COM_CTRL_OFFSET+zx29spi->virtbase));
2620
2621 writel(chip->fmt_ctrl, (SPI_FMT_CTRL_OFFSET+zx29spi->virtbase));
2622 //writel(chip->fifo_ctrl, (SPI_FIFO_CTRL_OFFSET+zx29spi->virtbase));
2623 /* yu.dong@20240617 [T106BUG-641] SPI packet loss problem, add kernel buffer scheme end */
2624 writel(chip->com_ctrl, (SPI_COM_CTRL_OFFSET + zx29spi->virtbase));
2625 //writel(chip->timing, (SPI_TIMING_OFFSET + zx29spi->virtbase));
2626
2627 writel(readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase)) | SPI_COM_CTRL_MASK_SSPE, (SPI_COM_CTRL_OFFSET+zx29spi->virtbase));
2628
2629 //while(((readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase))>> 4)&0x1) == 0);
2630 k_time_start = ktime_get();
2631 do {
2632 regval = readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase)) & SPI_COM_CTRL_MASK_SSPE_BACK;
2633 diff_ns = ktime_sub(ktime_get(),k_time_start);
2634 cpu_relax();
2635 }
2636 while (!regval && diff_ns < 10000000);
2637
2638 if(diff_ns >= 10000000) {
2639 dev_info(&zx29spi->pdev->dev, " zx29_setup_to_regs failed! diff_ns=%lld \n",diff_ns);
2640 }
xf.li1867bfa2024-08-20 02:32:16 -07002641
2642 if(false == zx29spi->master->slave)
2643 pm_relax(&zx29spi->pdev->dev);
2644 /* yu.dong@20240715 [T106BUG-641] SPI packet loss problem, merged into ZXW patch end */
xf.li2f424182024-08-20 00:47:34 -07002645}
2646/**
2647 * zx29_setup - setup function registered to SPI master framework
2648 * @spi: spi device which is requesting setup
2649 *
2650 * This function is registered to the SPI framework for this SPI master
2651 * controller. If it is the first time when setup is called by this device,
2652 * this function will initialize the runtime state for this chip and save
2653 * the same in the device structure. Else it will update the runtime info
2654 * with the updated chip info. Nothing is really being written to the
2655 * controller hardware here, that is not done until the actual transfer
2656 * commence.
2657 */
2658static int zx29_setup(struct spi_device *spi)
2659{
2660 struct spi_config_chip const *chip_info;
2661 struct chip_data *chip;
2662 unsigned speed_hz;
2663 int status = 0;
2664 struct zx29_spi *zx29spi = NULL;
2665 unsigned int bits =0;
2666 u8 iface = 0;
2667 u32 tmp;
2668
2669 if (!spi)
2670 return -EINVAL;
2671 bits = spi->bits_per_word;
2672 zx29spi = spi_master_get_devdata(spi->master);
2673 if (!zx29spi)
2674 return -EINVAL;
2675 iface = zx29spi->iface_mode;
2676
2677 /* Get controller_state if one is supplied */
2678 chip = spi_get_ctldata(spi);
2679
2680 if (chip == NULL) {
2681 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
2682 if (!chip) {
2683 dev_err(&spi->dev, "cannot allocate controller state\n");
2684 return -ENOMEM;
2685 }
2686 dev_dbg(&spi->dev, "allocated memory for controller's runtime state\n");
2687 }
2688
2689 /* Get controller data if one is supplied */
2690 chip_info = spi->controller_data;
2691
2692 if (chip_info == NULL) {
2693 chip_info = &spi_default_chip_info;
2694 /* spi_board_info.controller_data not is supplied */
2695 dev_dbg(&spi->dev, "using default controller_data settings\n");
2696 } else
2697 dev_dbg(&spi->dev, "using user supplied controller_data settings\n");
2698
2699 /*
2700 * We can override with custom divisors, else we use the board
2701 * frequency setting
2702 */
2703
2704 /* set spi clock source at 104MHz/1 */
2705 //writel(chip ->clk_div-1, M0_SSP_CLKDIV_REG_VA);
2706 speed_hz = spi->max_speed_hz;
2707// clk_set_rate(zx29spi->spi_clk, speed_hz * 2); /* f(ssp_clk) = 2*f(ssp_sclk_out) */
2708 spi->max_speed_hz = clk_round_rate(zx29spi->spi_clk, speed_hz * 2) / 2;
2709
2710 if (spi->max_speed_hz != speed_hz)
2711 dev_dbg(&spi->dev, "round speed %dHz differs from requested %dHz.", spi->max_speed_hz, speed_hz);
2712
2713 status = verify_controller_parameters(zx29spi, chip_info);
2714 if (status) {
2715 dev_err(&spi->dev, "controller data is incorrect");
2716 goto err_config_params;
2717 }
2718
2719 zx29spi->rx_lev_trig = chip_info->rx_lev_trig;
2720 zx29spi->tx_lev_trig = chip_info->tx_lev_trig;
2721
2722 /* Now set controller state based on controller data */
2723 //chip->xfer_type = chip_info->com_mode;
2724 chip->xfer_type = spi->dma_used ? DMA_TRANSFER : POLLING_TRANSFER;
2725 dev_dbg(&spi->dev, "chip->xfer_type = 0x%x \n",chip->xfer_type);
2726
2727 if (!chip_info->cs_control) {
2728 chip->cs_control = default_cs_control;
2729
2730 if (spi->master->num_chipselect != 1)
2731 dev_err(&spi->dev, "chip select function is NULL!\n");
2732 } else
2733 chip->cs_control = chip_info->cs_control;
2734
2735 /* Check bits per word with vendor specific range */
2736 if ((bits <= 3) || (bits > zx29spi->vendor->max_bpw)) {
2737 status = -ENOTSUPP;
2738 dev_err(&spi->dev, "illegal data size for this controller!\n");
2739 dev_err(&spi->dev, "This controller can only handle 4 <= n <= %d bit words\n",
2740 zx29spi->vendor->max_bpw);
2741 goto err_config_params;
2742 } else if (bits <= 8) {
2743 dev_dbg(&spi->dev, "4 <= n <=8 bits per word\n");
2744 chip->n_bytes = 1;
2745 chip->read = READING_U8;
2746 chip->write = WRITING_U8;
2747 } else if (bits <= 16) {
2748 dev_dbg(&spi->dev, "9 <= n <= 16 bits per word\n");
2749 chip->n_bytes = 2;
2750 chip->read = READING_U16;
2751 chip->write = WRITING_U16;
2752 } else {
2753 dev_dbg(&spi->dev, "17 <= n <= 32 bits per word\n");
2754 chip->n_bytes = 4;
2755 chip->read = READING_U32;
2756 chip->write = WRITING_U32;
2757 }
2758
2759 /* Now Initialize all register settings required for this chip */
2760 chip->com_ctrl = 0;
2761 chip->fmt_ctrl = 0;
2762 chip->fifo_ctrl = 0;
2763 chip->timing = 0;
2764
2765 if ((chip->xfer_type == DMA_TRANSFER)
2766 && ((zx29spi->master_info)->enable_dma)) {
2767 chip->enable_dma = true;
2768 dev_dbg(&spi->dev, "DMA mode set in controller state\n");
2769 } else {
2770 chip->enable_dma = false;
2771 dev_dbg(&spi->dev, "DMA mode NOT set in controller state\n");
2772 }
2773 SPI_WRITE_BITS(chip->fifo_ctrl, SPI_DMA_DISABLED, SPI_FIFO_CTRL_MASK_RX_DMA_EN, 2);
2774 SPI_WRITE_BITS(chip->fifo_ctrl, SPI_DMA_DISABLED, SPI_FIFO_CTRL_MASK_TX_DMA_EN, 3);
2775
2776 if (zx29spi->rx_lev_trig == SPI_RX_8_OR_MORE_ELEM)
2777 SPI_WRITE_BITS(chip->fifo_ctrl, SPI_FIFO_THRES_8, SPI_FIFO_CTRL_MASK_RX_FIFO_THRES, 4);
2778 else
2779 SPI_WRITE_BITS(chip->fifo_ctrl, SPI_FIFO_THRES_4, SPI_FIFO_CTRL_MASK_RX_FIFO_THRES, 4);
2780 if (zx29spi->tx_lev_trig == SPI_TX_8_OR_MORE_EMPTY_LOC)
2781 SPI_WRITE_BITS(chip->fifo_ctrl, SPI_FIFO_THRES_8, SPI_FIFO_CTRL_MASK_TX_FIFO_THRES, 8);
2782 else
2783 SPI_WRITE_BITS(chip->fifo_ctrl, SPI_FIFO_THRES_4, SPI_FIFO_CTRL_MASK_TX_FIFO_THRES, 8);
2784
2785 SPI_WRITE_BITS(chip->fmt_ctrl, bits - 1, SPI_FMT_CTRL_MASK_DSS, 4);
2786 SPI_WRITE_BITS(chip->fmt_ctrl, chip_info->iface, SPI_FMT_CTRL_MASK_FRF, 0);
2787
2788 if((iface== SPI_TI_FORMAT)||(iface== SPI_ISI_FORMAT)){
2789 printk("qhf %s set iface = %d\n",__func__,iface);
2790 SPI_WRITE_BITS(chip->fmt_ctrl, iface, SPI_FMT_CTRL_MASK_FRF, 0);
2791 }
2792 /* Stuff that is common for all versions */
2793 if (spi->mode & SPI_CPOL)
2794 tmp = SPI_CLK_POL_IDLE_HIGH;
2795 else
2796 tmp = SPI_CLK_POL_IDLE_LOW;
2797 SPI_WRITE_BITS(chip->fmt_ctrl, tmp, SPI_FMT_CTRL_MASK_POL, 2);
2798
2799 if (spi->mode & SPI_CPHA)
2800 tmp = SPI_CLK_SECOND_EDGE;
2801 else
2802 tmp = SPI_CLK_FIRST_EDGE;
2803
2804 SPI_WRITE_BITS(chip->fmt_ctrl, tmp, SPI_FMT_CTRL_MASK_PHA, 3);
2805
2806 /* Loopback is available on all versions except PL023 */
2807 if (zx29spi->vendor->loopback) {
2808 if (spi->mode & SPI_LOOP)
2809 tmp = LOOPBACK_ENABLED;
2810 else
2811 tmp = LOOPBACK_DISABLED;
2812 SPI_WRITE_BITS(chip->com_ctrl, tmp, SPI_COM_CTRL_MASK_LBM, 0);
2813 }
2814// SPI_WRITE_BITS(chip->com_ctrl, SPI_ENABLED, SPI_COM_CTRL_MASK_SSPE, 1);
2815 SPI_WRITE_BITS(chip->com_ctrl, chip_info->hierarchy, SPI_COM_CTRL_MASK_MS, 2);
2816// SPI_WRITE_BITS(chip->com_ctrl, chip_info->slave_tx_disable, SPI_COM_CTRL_MASK_SOD, 3);
2817
2818 if(spi->trans_gaped) {
2819 chip->enable_trans_gap = true;
2820 }
2821 SPI_WRITE_BITS(chip->timing, spi->trans_gap_num, SPI_TIMING_MASK_T_CS_DESEL, 0);
2822 /* Save controller_state */
2823 spi_set_ctldata(spi, chip);
2824 if(zx29spi->mode == ZX29_SSP_SLAVE_TYPE) {
2825
2826 SPI_WRITE_BITS(chip->com_ctrl, SPI_SLAVE_MODE, SPI_COM_CTRL_MASK_MS, 2);
2827 zx29_setup_to_regs(chip,zx29spi);
2828 }
2829
xf.li1867bfa2024-08-20 02:32:16 -07002830 if(zx29spi->mode == ZX29_SSP_MASTER_TYPE) {
2831 if(spi->setup_immediately == 1)
2832 zx29_setup_to_regs(chip,zx29spi);
2833 }
xf.li2f424182024-08-20 00:47:34 -07002834
2835 return status;
2836 err_config_params:
2837 spi_set_ctldata(spi, NULL);
2838 kfree(chip);
2839 return status;
2840}
2841
2842/**
2843 * zx29_cleanup - cleanup function registered to SPI master framework
2844 * @spi: spi device which is requesting cleanup
2845 *
2846 * This function is registered to the SPI framework for this SPI master
2847 * controller. It will free the runtime state of chip.
2848 */
2849static void zx29_cleanup(struct spi_device *spi)
2850{
2851 struct chip_data *chip = spi_get_ctldata(spi);
2852
2853 spi_set_ctldata(spi, NULL);
2854 kfree(chip);
2855}
2856
2857static int zx29_spi_clock_init(struct zx29_spi *zx29spi)
2858{
2859 int status = 0;
2860 struct platform_device *pdev = zx29spi->pdev;
2861 /* work clock */
2862 zx29spi->spi_clk = devm_clk_get(&pdev->dev, "work_clk");
2863 if (IS_ERR(zx29spi->spi_clk)) {
2864 status = PTR_ERR(zx29spi->spi_clk);
2865 dev_err(&pdev->dev, "could not retrieve SPI work clock\n");
2866 return status;
2867 }
2868
2869 /* enable spiclk at function zx29_setup */
2870
2871 if (device_property_read_u32(&pdev->dev, "clock-frequency", &zx29spi->clkfreq))
2872 zx29spi->clkfreq = SPI_SPICLK_FREQ_26M;
2873 status = clk_set_rate(zx29spi->spi_clk, zx29spi->clkfreq);
2874 if(status) {
2875 dev_err(&pdev->dev,"clc_set_rate err status=%d \n",status);
2876 return status;
2877 }
2878 /* enable ssp clock source */
2879 clk_prepare_enable(zx29spi->spi_clk);
2880
2881 /* apb clock */
2882 zx29spi->pclk = devm_clk_get(&pdev->dev, "apb_clk");
2883 if (IS_ERR(zx29spi->pclk)) {
2884 status = PTR_ERR(zx29spi->pclk);
2885 dev_err(&pdev->dev, "could not retrieve SPI work clock\n");
2886 return status;
2887 }
2888 clk_prepare_enable(zx29spi->pclk);
2889
2890 return status;
2891}
2892
2893static void spicc_clkgate_ctrl(struct zx29_spi *zx29spi,unsigned char is_enable)
2894{
2895 if (is_enable) {
2896 clk_enable(zx29spi->spi_clk);
2897 clk_enable(zx29spi->pclk);
2898 } else {
2899 clk_disable(zx29spi->spi_clk);
2900 clk_disable(zx29spi->pclk);
2901 }
2902}
2903
2904static int zx29_spi_slave_clock_init(struct zx29_spi *zx29spi)
2905{
2906 int status=0;
2907 struct platform_device *pdev = zx29spi->pdev;
2908 /* work clock */
2909 zx29spi->spi_clk = devm_clk_get(&pdev->dev, "work_clk");
2910 if (IS_ERR(zx29spi->spi_clk)) {
2911 status = PTR_ERR(zx29spi->spi_clk);
2912 dev_err(&pdev->dev, "could not retrieve SPI work clock\n");
2913 return status;
2914 }
2915
2916 /* enable spiclk at function zx29_setup */
2917 //if (device_property_read_u32(&pdev->dev, "clock-frequency", &zx29spi->clkfreq))
2918 zx29spi->clkfreq = SPI_SPICLK_FREQ_156M; /*salve */
2919
2920 status = clk_set_rate(zx29spi->spi_clk, zx29spi->clkfreq);
2921 if(status) {
2922 dev_err(&pdev->dev,"clc_set_rate err status=%d \n",status);
2923 return status;
2924 }
2925 /* enable ssp clock source */
2926 clk_prepare_enable(zx29spi->spi_clk);
2927
2928 /* apb clock */
2929 zx29spi->pclk = devm_clk_get(&pdev->dev, "apb_clk");
2930 if (IS_ERR(zx29spi->pclk)) {
2931 status = PTR_ERR(zx29spi->pclk);
2932 dev_err(&pdev->dev, "could not retrieve SPI work clock\n");
2933 return status;
2934 }
2935 clk_prepare_enable(zx29spi->pclk);
2936 spicc_clkgate_ctrl(zx29spi,true);
2937
2938 return status;
2939}
2940
2941
2942static int zx29_spi_init_pinctrl(struct platform_device *pdev)
2943{
2944 struct pinctrl *pctrl;
2945 enum of_gpio_flags flags;
2946 int ret;
2947 struct zx29_spi *zx29spi = NULL;
2948
2949 if(!pdev) {
2950 printk("pdev not exist \n");
2951 return -1;
2952 }
2953 zx29spi = (struct zx29_spi *)platform_get_drvdata(pdev);
2954 if (IS_ERR(zx29spi)) {
2955 dev_warn(&pdev->dev, "Failed to get zx29->ssp%d pins",pdev->id);
2956 pctrl = NULL;
2957 return 0;
2958 }
2959
2960 pctrl = devm_pinctrl_get(&pdev->dev);
2961 if (IS_ERR(pctrl)) {
2962 dev_warn(&pdev->dev, "Failed to get zx29->ssp%d pins",pdev->id);
2963 pctrl = NULL;
2964 return 0;
2965 }
2966 ssp_pins[pdev->id].pctrl=pctrl;
2967
2968 ssp_pins[pdev->id].pcs_gpio_active = pinctrl_lookup_state(pctrl, "cs_gpio_active");
2969 if (IS_ERR(ssp_pins[pdev->id].pcs_gpio_active)) {
2970 dev_err(&pdev->dev, "missing cs_gpio_active \n");
2971 }
2972
2973 ssp_pins[pdev->id].pcs_gpio_sleep = pinctrl_lookup_state(pctrl, "cs_gpio_sleep");
2974 if (IS_ERR(ssp_pins[pdev->id].pcs_gpio_sleep)) {
2975 dev_err(&pdev->dev, "missing cs_gpio_sleep \n");
2976 }
2977 ssp_pins[pdev->id].pcs_func = pinctrl_lookup_state(ssp_pins[pdev->id].pctrl, "cs_func");
2978 if (IS_ERR(ssp_pins[pdev->id].pcs_func)) {
2979 dev_err(&pdev->dev, "missing cs_func \n");
2980 }
2981
2982 if(zx29spi->master->slave == false) {
2983 if (pinctrl_select_state(ssp_pins[pdev->id].pctrl, ssp_pins[pdev->id].pcs_gpio_active) < 0) {
2984 printk("spi%d setting cs_gpio pin ctrl failed\n",pdev->id);
2985 }
2986 }else {
2987 if (pinctrl_select_state(ssp_pins[pdev->id].pctrl, ssp_pins[pdev->id].pcs_func) < 0) {
2988 printk("spi%d setting cs_func pin ctrl failed\n",pdev->id);
2989 }
2990 }
2991
2992 ssp_pins[pdev->id].gpio_cs = of_get_gpio_flags(pdev->dev.of_node, 0, &flags);
2993 if (!gpio_is_valid(ssp_pins[pdev->id].gpio_cs)) {
2994 pr_info("ssp%d gpio_cs no found\n",pdev->id);
2995 }
2996
2997 ssp_pins[pdev->id].gpio_clk = of_get_gpio_flags(pdev->dev.of_node, 1, &flags);
2998 if (!gpio_is_valid(ssp_pins[pdev->id].gpio_clk)) {
2999 pr_info("ssp%d gpio_clk no found\n",pdev->id);
3000 }
3001 ssp_pins[pdev->id].gpio_tx = of_get_gpio_flags(pdev->dev.of_node, 2, &flags);
3002 if (!gpio_is_valid(ssp_pins[pdev->id].gpio_tx)) {
3003 pr_info("ssp%d gpio_tx no found\n",pdev->id);
3004 }
3005
3006 ssp_pins[pdev->id].gpio_rx = of_get_gpio_flags(pdev->dev.of_node, 3, &flags);
3007 if (!gpio_is_valid(ssp_pins[pdev->id].gpio_rx)) {
3008 pr_info("ssp%d gpio_rx no found\n",pdev->id);
3009 }
3010
3011 if(zx29spi->master->slave == false)
3012 gpio_direction_output(ssp_pins[pdev->id].gpio_cs,SPI_GPIO_HIGH);
3013
3014 return 0;
3015}
3016static void zx29_spi_get_platformInfo(struct platform_device *pdev,struct zx29_spi_controller *platform_info)
3017{
3018 struct device *dev=&pdev->dev;
3019 u32 dma_tx,dma_rx,enable_dma;
3020
3021
3022 if (device_property_read_u16(dev, "bus_id", &platform_info->bus_id)) {
3023 platform_info->bus_id = pdev->id;
3024 }
3025 if (device_property_read_u8(dev, "num_chipselect", &platform_info->num_chipselect)) {
3026 platform_info->num_chipselect = 1;
3027 }
3028#if 0
3029 if (device_property_read_u32(dev, "enable_dma",&enable_dma)) {
3030 dev_err(&pdev->dev,"enable_dma get failed");
3031 platform_info->enable_dma = 0;
3032 }
3033 else {
3034 platform_info->enable_dma = enable_dma;
3035 }
3036#endif
3037 if (device_property_read_u32(dev, "autosuspend_delay", &platform_info->autosuspend_delay))
3038 platform_info->autosuspend_delay = 0;
3039
3040 if(device_property_read_u32(dev, "dma_rx", &dma_rx)){
3041 dev_err(&pdev->dev,"dma_rx get failed");
3042 }
3043 platform_info->dma_rx_param = (void*)dma_rx;
3044 device_property_read_u32(dev, "dma_tx", &dma_tx);
3045 platform_info->dma_tx_param = (void*)dma_tx;
3046
3047 dev_dbg(&pdev->dev,"get dma_rx=0x%x dma_tx=0x%x enable_dma=0x%x",dma_rx,dma_tx,platform_info->enable_dma);
3048
3049}
3050
3051#if defined(CONFIG_DEBUG_FS)
3052#define dump_register(reg) \
3053{ \
3054 .name = __stringify(reg), \
3055 .offset = SPI_ ##reg##_OFFSET, \
3056}
3057
3058
3059static const struct debugfs_reg32 spi_regs[] = {
3060 dump_register(VER_REG),
3061 dump_register(COM_CTRL),
3062 dump_register(FMT_CTRL),
3063 dump_register(DR),
3064 dump_register(FIFO_CTRL),
3065 dump_register(FIFO_SR),
3066 dump_register(INTR_EN),
3067 dump_register(INTR_SR),
3068 dump_register(TIMING),
3069};
3070
3071//#define Strcat(x, fmt, ...) sprintf(x, "%s" #fmt, x, __VA_ARGS__)
3072
3073static void debugfs_spi_init(struct zx29_spi *zx29spi)
3074{
3075 struct dentry *root;
3076 struct dentry *node;
3077 char tmp[32];
3078
3079 if(!zx29spi)
3080 return;
3081
3082 //create root
3083 sprintf(tmp,"spi%d_zx29", zx29spi->pdev->id);
3084 root = debugfs_create_dir(tmp, NULL);
3085 if (!root) {
3086 dev_err(&zx29spi->pdev->dev, "debugfs_create_dir %s err\n", tmp);
3087 goto err;
3088 }
3089
3090 //create regs
3091 zx29spi->spi_regset.regs = (struct debugfs_reg32 *)spi_regs;
3092 zx29spi->spi_regset.nregs = sizeof(spi_regs)/sizeof(struct debugfs_reg32);
3093 zx29spi->spi_regset.base = zx29spi->virtbase;
3094
3095 debugfs_create_regset32("spi_regs", S_IRUGO, root, &zx29spi->spi_regset);
3096 //create info
3097 debugfs_create_u32("poll_cnt", S_IRUGO, root, &zx29spi->spi_poll_cnt);
3098 debugfs_create_u32("dma_cnt", S_IRUGO, root, &zx29spi->spi_dma_cnt);
3099
3100 zx29spi->spi_root = (void *)root;
3101 return;
3102err:
3103 dev_err(&zx29spi->pdev->dev, "debugfs_spi_init err\n");
3104}
3105
3106#endif
3107
3108
3109static int zx29_spi_init_irq(struct platform_device *pdev, struct zx29_spi *zx29spi)
3110{
3111 int irq = 0,ret = 0;
3112
3113 if(!zx29spi || !pdev) {
3114 ret = -ENOENT;
3115 return ret;
3116 }
3117 irq = platform_get_irq(pdev, 0);
3118 if (irq == NULL) {
3119 dev_err(&pdev->dev, "Cannot get IORESOURCE_IRQ\n");
3120 ret = -ENOENT;
3121 return ret;
3122 }
3123 zx29spi->irq = irq;
3124 dev_dbg(&pdev->dev, "used interrupt num is %d\n", zx29spi->irq);
3125 ret = devm_request_irq(&pdev->dev, zx29spi->irq, zx29_spi_irq,
3126 IRQF_TRIGGER_HIGH | IRQF_NO_THREAD | IRQF_ONESHOT, dev_name(&pdev->dev), zx29spi);
3127 if (ret < 0) {
3128 dev_err(&pdev->dev, "probe - cannot get IRQ (%d)\n", ret);
3129 return ret;
3130 }
3131 disable_irq_nosync(zx29spi->irq);
3132 return ret;
3133
3134}
3135
3136static int zx29_spi_probe_of_master(struct platform_device *pdev)
3137{
3138 struct device *dev = &pdev->dev;
3139 struct zx29_spi_controller *platform_info=NULL ;
3140 struct spi_master *master;
3141 struct zx29_spi *zx29spi = NULL; /*Data for this driver */
3142 struct resource *regs = NULL;
3143 struct resource *gpio = NULL;
3144 struct resource *irq = NULL;
3145 struct device_node *np = pdev->dev.of_node;
3146 int status = 0, i,ret;
3147 u32 regval = 0;
3148
3149 platform_info = devm_kzalloc(&pdev->dev, sizeof(struct zx29_spi_controller), GFP_KERNEL);
3150 if(platform_info == NULL)
3151 return 0;
3152 platform_info->bus_id = 0,
3153 platform_info->num_chipselect = 1,
3154 platform_info->enable_dma = 1,
3155 platform_info->autosuspend_delay=0,
3156
3157 /* Allocate master with space for data */
3158 master = spi_alloc_master(dev, sizeof(struct zx29_spi));
3159 if (master == NULL) {
3160 dev_err(&pdev->dev, "probe - cannot alloc SPI master\n");
3161 status = -ENOMEM;
3162 goto err_no_master;
3163 }
3164
3165 zx29spi = spi_master_get_devdata(master);
3166 memset(zx29spi,0,sizeof(struct zx29_spi));
3167 pdev->id = of_alias_get_id(np, "spi");
3168 if(pdev->id < 0){
3169 printk("zx29_ssp of_alias_get_id fail ret:%d\n", pdev->id);
3170 status = -ENOMEM;
3171 goto err_no_master;
3172 }
3173 snprintf(zx29spi->name, sizeof(zx29spi->name), "zx29-spi%d", pdev->id);
3174 zx29_spi_get_platformInfo(pdev,platform_info);
3175 //mutex_init(&zx29spi->spi_lock);
3176 g_zx29_spi[pdev->id] = zx29spi;
3177 zx29spi->master = master;
3178 zx29spi->master_info = platform_info;
3179 zx29spi->pdev = pdev;
3180 zx29spi->vendor = &vendor_arm;
3181 zx29spi->mode = ZX29_SSP_MASTER_TYPE;
3182 zx29spi->zx29_flush_rxfifo = zx29_flush_rxfifo;
3183 sema_init(&zx29spi->sema_dma, 0);
3184 /* yu.dong@20240521 [T106BUG-616] SPI set to slave mode for read will get stuck start */
3185 init_waitqueue_head(&zx29spi->wait);
3186 zx29spi->trans_done = false;
3187 /* yu.dong@20240521 [T106BUG-616] SPI set to slave mode for read will get stuck end */
3188 dev_set_drvdata(&pdev->dev, zx29spi);
3189 device_init_wakeup(&pdev->dev, true);
3190 /*
3191 * Bus Number Which has been Assigned to this SSP controller
3192 * on this board
3193 */
3194 master->bus_num = platform_info->bus_id;
3195 master->num_chipselect = platform_info->num_chipselect;
3196 master->cleanup = zx29_cleanup;
3197 master->setup = zx29_setup;
3198 master->prepare_transfer_hardware = zx29_prepare_transfer_hardware;
3199 master->transfer_one_message = zx29_transfer_one_message;
3200 master->unprepare_transfer_hardware = zx29_unprepare_transfer_hardware;
3201 //master->rt = platform_info->rt;
3202
3203 /*
3204 * Supports mode 0-3, loopback, and active low CS..
3205 */
3206 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_NO_CS|SPI_LOOP;
3207
3208 dev_dbg(&pdev->dev, "BUSNO: %d\n", master->bus_num);
3209
3210 zx29_spi_init_pinctrl(pdev);
3211
3212 /* registers */
3213 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3214 if (regs == NULL){
3215 dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
3216 status = -ENOENT;
3217 goto err_no_registers;
3218 }
3219 zx29spi->phybase = regs->start;
3220 zx29spi->virtbase = devm_platform_ioremap_resource(pdev, 0);
3221 if (zx29spi->virtbase == NULL) {
3222 status = -ENOMEM;
3223 goto err_no_ioremap;
3224 }
3225 dev_dbg( &pdev->dev," mapped registers from 0x%08x to 0x%p\n",
3226 regs->start, zx29spi->virtbase);
3227
3228#if defined(CONFIG_DEBUG_FS)
3229 debugfs_spi_init(zx29spi);
3230#endif
3231
3232 /*clock init*/
3233 status = zx29_spi_clock_init(zx29spi);
3234 if(status)
3235 goto err_no_clk;
3236
3237 /* Initialize transfer pump */
3238 //tasklet_init(&zx29spi->pump_transfers, pump_transfers,(unsigned long)zx29spi);
3239
3240 /* Disable SPI */
3241 regval = readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase)) & (~SPI_COM_CTRL_MASK_SSPE);
3242 writel(regval, (SPI_COM_CTRL_OFFSET+zx29spi->virtbase));
3243
3244 load_spi_default_config(zx29spi);
3245 writel(0, (SPI_TIMING_OFFSET + zx29spi->virtbase));
3246 if(!strcmp(pdev->name,"1410000.ssp")) {
3247 regval = readl((SPI_FMT_CTRL_OFFSET+zx29spi->virtbase))&(~(0x1<<12));
3248 writel(regval, (SPI_FMT_CTRL_OFFSET+zx29spi->virtbase));
3249 dev_info(&pdev->dev,"%s set non-camera mode regval:0x%x \n",pdev->name,regval);
3250 }
3251 status = zx29_spi_init_irq(pdev,zx29spi);
3252 if(status != 0) {
3253 dev_err(&pdev->dev, "zx29_spi_init_irq err!!! \n");
3254 goto err_no_irq;
3255 }
3256 /* Get DMA channels */
3257 if (platform_info->enable_dma) {
3258 status = zx29_dma_probe(zx29spi);
3259 if (status != 0) {
3260 platform_info->enable_dma = 0;
3261 sc_debug_info_record(MODULE_ID_CAP_SPI, "%s dma probe failed \n",pdev->name);
3262 }
3263 }
3264
3265#if SPI_PSM_CONTROL
3266 wake_lock_init(&zx29spi->psm_lock, WAKE_LOCK_SUSPEND, zx29spi->name);
3267#endif
3268 master->dev.of_node = pdev->dev.of_node;
3269 ret = devm_spi_register_master(&pdev->dev, master);
3270 if (ret < 0) {
3271 dev_err(&pdev->dev, "can not register to master err %d\n", ret);
3272 goto err_spi_register;
3273 }
3274 dev_info(&pdev->dev," probe succeeded\n");
3275
3276 /* let runtime pm put suspend */
3277 if (platform_info->autosuspend_delay > 0) {
3278 dev_info(&pdev->dev, "will use autosuspend for runtime pm, delay %dms\n", platform_info->autosuspend_delay);
3279 pm_runtime_set_autosuspend_delay(dev, platform_info->autosuspend_delay);
3280 pm_runtime_use_autosuspend(dev);
3281 pm_runtime_put_autosuspend(dev);
3282 } else {
3283 pm_runtime_put(dev);
3284 }
3285
3286 return 0;
3287
3288 err_spi_register:
3289#if SPI_PSM_CONTROL
3290 wake_lock_destroy(&zx29spi->psm_lock);
3291#endif
3292 if (platform_info->enable_dma)
3293 zx29_dma_remove(zx29spi);
3294
3295 err_no_irq:
3296 clk_disable(zx29spi->spi_clk);
3297// err_no_clk_en:
3298 //clk_unprepare(pl022->clk);
3299 //err_clk_prep:
3300 clk_put(zx29spi->spi_clk);
3301 err_no_clk:
3302// iounmap(zx29spi->virtbase);
3303 err_gpios:
3304 /* add */
3305 err_no_ioremap:
3306 err_no_registers:
3307 spi_master_put(master);
3308 err_no_master:
3309 err_no_pdata:
3310 return status;
3311}
3312
3313static int zx29_spi_probe_of_slave(struct platform_device *pdev)
3314{
3315 struct device *dev = &pdev->dev;
3316 struct zx29_spi_controller *platform_info=NULL ;
3317 struct spi_master *master;
3318 struct zx29_spi *zx29spi = NULL; /*Data for this driver */
3319 struct resource *regs = NULL;
3320 struct resource *gpio = NULL;
3321 struct resource *irq = NULL;
3322 struct device_node *np = pdev->dev.of_node;
3323 int status = 0, i,ret;
3324 u32 regval = 0;
3325
3326 platform_info = devm_kzalloc(&pdev->dev, sizeof(struct zx29_spi_controller), GFP_KERNEL);
3327 if(platform_info == NULL)
3328 return 0;
3329 platform_info->bus_id = 0,
3330 platform_info->num_chipselect = 1,
3331 platform_info->enable_dma = 1,
3332 platform_info->autosuspend_delay=0,
3333
3334 /* Allocate master with space for data */
3335 master = spi_alloc_master(dev, sizeof(struct zx29_spi));
3336 if (master == NULL) {
3337 dev_err(&pdev->dev, "probe - cannot alloc SPI master\n");
3338 status = -ENOMEM;
3339 goto err_no_master;
3340 }
3341 master->slave = true;
3342 zx29spi = spi_master_get_devdata(master);
3343 memset(zx29spi,0,sizeof(struct zx29_spi));
3344 pdev->id = of_alias_get_id(np, "spi");
3345 if(pdev->id < 0){
3346 printk("zx29_ssp of_alias_get_id fail ret:%d\n", pdev->id);
3347 goto err_no_master;
3348 }
3349 snprintf(zx29spi->name, sizeof(zx29spi->name), "zx29-spi%d", pdev->id);
3350
3351 zx29_spi_get_platformInfo(pdev,platform_info);
3352 //mutex_init(&zx29spi->spi_lock);
3353 g_zx29_spi[pdev->id] = zx29spi;
3354 zx29spi->master = master;
3355 zx29spi->master_info = platform_info;
3356 zx29spi->pdev = pdev;
3357 zx29spi->vendor = &vendor_arm;
3358 zx29spi->mode = ZX29_SSP_SLAVE_TYPE;
3359 zx29spi->zx29_flush_rxfifo = zx29_flush_rxfifo;
3360 sema_init(&zx29spi->sema_dma, 0);
3361 /* yu.dong@20240521 [T106BUG-616] SPI set to slave mode for read will get stuck start */
3362 init_waitqueue_head(&zx29spi->wait);
3363 zx29spi->trans_done = false;
3364 /* yu.dong@20240521 [T106BUG-616] SPI set to slave mode for read will get stuck end */
3365 dev_set_drvdata(&pdev->dev, zx29spi);
3366 /*
3367 * Bus Number Which has been Assigned to this SSP controller
3368 * on this board
3369 */
3370 master->bus_num = platform_info->bus_id;
3371 master->num_chipselect = platform_info->num_chipselect;
3372 master->cleanup = zx29_cleanup;
3373 master->setup = zx29_setup;
3374 master->prepare_transfer_hardware = zx29_prepare_transfer_hardware;
3375 master->transfer_one_message = zx29_slave_transfer_one_message;
3376 master->unprepare_transfer_hardware = zx29_unprepare_transfer_hardware;
3377 /* yu.dong@20240617 [T106BUG-641] SPI packet loss problem, add kernel buffer scheme start */
3378 master->spi_slave_rd_start = zx29_slave_rd_start;
3379 master->spi_slave_rd_stop = zx29_slave_rd_stop;
3380 /* yu.dong@20240617 [T106BUG-641] SPI packet loss problem, add kernel buffer scheme end */
3381 //master->rt = platform_info->rt;
3382
3383 /*
3384 * Supports mode 0-3, loopback, and active low CS..
3385 */
3386 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_NO_CS|SPI_LOOP;
3387
3388 dev_dbg(&pdev->dev, "BUSNO: %d\n", master->bus_num);
3389
3390 zx29_spi_init_pinctrl(pdev);
3391
3392 /* registers */
3393 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3394 if (regs == NULL){
3395 dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
3396 status = -ENOENT;
3397 goto err_no_registers;
3398 }
3399 zx29spi->phybase = regs->start;
3400 zx29spi->virtbase = devm_platform_ioremap_resource(pdev, 0);
3401 if (zx29spi->virtbase == NULL) {
3402 status = -ENOMEM;
3403 goto err_no_ioremap;
3404 }
3405 dev_dbg( &pdev->dev," mapped registers from 0x%08x to 0x%p\n",
3406 regs->start, zx29spi->virtbase);
3407
3408#if defined(CONFIG_DEBUG_FS)
3409 debugfs_spi_init(zx29spi);
3410#endif
3411
3412 /*clock init*/
3413 status = zx29_spi_slave_clock_init(zx29spi);
3414 if(status)
3415 goto err_no_clk;
3416 /* Initialize transfer pump */
3417 //tasklet_init(&zx29spi->pump_transfers, pump_transfers,(unsigned long)zx29spi);
3418
3419 /* Disable SPI */
3420 regval = readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase)) & (~SPI_COM_CTRL_MASK_SSPE);
3421 writel(regval, (SPI_COM_CTRL_OFFSET+zx29spi->virtbase));
3422
3423 load_spi_default_config(zx29spi);
3424 writel(0, (SPI_TIMING_OFFSET + zx29spi->virtbase));
3425
3426 if(!strcmp(pdev->name,"1410000.ssp")) {
3427 regval = readl((SPI_FMT_CTRL_OFFSET+zx29spi->virtbase))&(~(0x1<<12));
3428 writel(regval, (SPI_FMT_CTRL_OFFSET+zx29spi->virtbase));
3429 dev_info(&pdev->dev," %s set non-camera mode regval:0x%x \n",pdev->name,regval);
3430 }
3431
3432 writel(readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase)) | SPI_COM_CTRL_MASK_SSPE, (SPI_COM_CTRL_OFFSET+zx29spi->virtbase));
3433 while(((readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase))>> 4)&0x1) == 0);
3434
3435 dev_info(&pdev->dev,"ssp enabled \n",regval);
3436 /* irq*/
3437 status = zx29_spi_init_irq(pdev,zx29spi);
3438 if(status != 0) {
3439 dev_err(&pdev->dev, "zx29_spi_init_irq err!!! \n");
3440 goto err_no_irq;
3441 }
3442
3443 /* Get DMA channels */
3444 if (platform_info->enable_dma) {
3445 status = zx29_dma_probe(zx29spi);
3446 if (status != 0) {
3447 platform_info->enable_dma = 0;
3448 sc_debug_info_record(MODULE_ID_CAP_SPI, "%s dma probe failed",pdev->name);
3449 }
3450 }
3451
3452#if SPI_PSM_CONTROL
3453 wake_lock_init(&zx29spi->psm_lock, WAKE_LOCK_SUSPEND, zx29spi->name);
3454#endif
3455 master->dev.of_node = pdev->dev.of_node;
3456 ret = devm_spi_register_master(&pdev->dev, master);
3457 if (ret < 0) {
3458 dev_err(&pdev->dev, "can not register to master err %d\n", ret);
3459 goto err_spi_register;
3460 }
3461
3462 dev_info(&pdev->dev," probe succeeded\n");
3463
3464 /* let runtime pm put suspend */
3465 if (platform_info->autosuspend_delay > 0) {
3466 dev_info(&pdev->dev, "will use autosuspend for runtime pm, delay %dms\n", platform_info->autosuspend_delay);
3467 pm_runtime_set_autosuspend_delay(dev, platform_info->autosuspend_delay);
3468 pm_runtime_use_autosuspend(dev);
3469 pm_runtime_put_autosuspend(dev);
3470 } else {
3471 pm_runtime_put(dev);
3472 }
3473
3474 return 0;
3475
3476 err_spi_register:
3477#if SPI_PSM_CONTROL
3478 wake_lock_destroy(&zx29spi->psm_lock);
3479#endif
3480 if (platform_info->enable_dma)
3481 zx29_dma_remove(zx29spi);
3482
3483 err_no_irq:
3484 clk_disable(zx29spi->spi_clk);
3485// err_no_clk_en:
3486 //clk_unprepare(pl022->clk);
3487 //err_clk_prep:
3488 clk_put(zx29spi->spi_clk);
3489 err_no_clk:
3490// iounmap(zx29spi->virtbase);
3491 err_gpios:
3492 /* add */
3493 err_no_ioremap:
3494 err_no_registers:
3495 spi_master_put(master);
3496 err_no_master:
3497 err_no_pdata:
3498 return status;
3499}
3500
3501
3502
3503static int zx29_spi_probe(struct platform_device *pdev)
3504{
3505 struct device *dev = &pdev->dev;
3506 const struct of_device_id *match;
3507 const struct zx29_ssp_device_of_data *data;
3508 enum zx29_ssp_device_mode mode;
3509 int status = -EINVAL;
3510 match = of_match_device(zx29_spi_of_match, dev);
3511 if (!match)
3512 return -EINVAL;
3513 data = (struct zx29_ssp_device_of_data *)match->data;
3514 mode = (enum zx29_ssp_device_mode)data->mode;
3515
3516 dev_info(&pdev->dev,"%s mode \n", (mode==0)?"MASTER":(mode==1)?"SLAVE":"UNKNOWN");
3517 if(mode == ZX29_SSP_MASTER_TYPE)
3518 status = zx29_spi_probe_of_master(pdev);
3519 if(mode == ZX29_SSP_SLAVE_TYPE)
3520 status = zx29_spi_probe_of_slave(pdev);
3521
3522 return status;
3523}
3524
3525static int __exit zx29_spi_remove(struct platform_device *pdev)
3526{
3527 struct zx29_spi *zx29spi = dev_get_drvdata(&pdev->dev);
3528 struct resource * gpio = NULL;
3529 //struct resource * irq = NULL;
3530 int i;
3531
3532 if (!zx29spi)
3533 return 0;
3534
3535 /*
3536 * undo pm_runtime_put() in probe. I assume that we're not
3537 * accessing the primecell here.
3538 */
3539 pm_runtime_get_noresume(&pdev->dev);
3540
3541 spi_unregister_master(zx29spi->master);
3542
3543 load_spi_default_config(zx29spi);
3544 if (zx29spi->master_info->enable_dma)
3545 zx29_dma_remove(zx29spi);
3546/*
3547 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
3548 if( irq != NULL )
3549 {
3550 free_irq(irq->start, zx29spi);
3551 }
3552*/
3553 devm_free_irq(&pdev->dev, zx29spi->irq, zx29spi);
3554
3555 clk_disable(zx29spi->spi_clk);
3556 clk_put(zx29spi->spi_clk);
3557
3558 clk_disable(zx29spi->pclk);
3559 clk_put(zx29spi->pclk);
3560
3561 #if defined(CONFIG_DEBUG_FS)
3562 if(zx29spi->spi_root){
3563 printk(KERN_INFO "spi:debugfs_remove_recursive \n");
3564 debugfs_remove_recursive(zx29spi->spi_root);
3565 }
3566 #endif
3567
3568
3569 // iounmap(zx29spi->virtbase);
3570 //amba_release_regions(adev);
3571 //tasklet_disable(&zx29spi->pump_transfers);
3572
3573 spi_master_put(zx29spi->master);
3574 //amba_set_drvdata(adev, NULL);
3575 dev_set_drvdata(&pdev->dev, NULL);
3576
3577#if SPI_PSM_CONTROL
3578 wake_lock_destroy(&zx29spi->psm_lock);
3579#endif
3580
3581 return 0;
3582}
3583
3584static const struct zx29_ssp_device_of_data zx29_ssp_master_of_data = {
3585 .mode = ZX29_SSP_MASTER_TYPE,
3586};
3587
3588static const struct zx29_ssp_device_of_data zx29_ssp_slave_of_data = {
3589 .mode = ZX29_SSP_SLAVE_TYPE,
3590};
3591
3592static const struct of_device_id zx29_spi_of_match[] = {
3593 {
3594 .compatible = "zte,zx29_ssp",
3595 .data = &zx29_ssp_master_of_data,
3596 },
3597 {
3598 .compatible = "zte,zx29_ssp_slave",
3599 .data = &zx29_ssp_slave_of_data,
3600 },
3601 {},
3602};
3603MODULE_DEVICE_TABLE(of, zx29_spi_of_match);
3604
3605#ifdef CONFIG_PM
3606static int zx29_spi_suspend(struct platform_device *pdev,pm_message_t state)
3607{
3608 struct zx29_spi *zx29spi = NULL;
3609
3610 if(pdev == NULL)
3611 return -1;
3612 if(pdev && &pdev->dev)
3613 pinctrl_pm_select_sleep_state(&pdev->dev);
3614
3615 zx29spi = (struct zx29_spi *)platform_get_drvdata(pdev);
3616 if(zx29spi && (zx29spi->master->slave == true)) {
3617 if (pinctrl_select_state(ssp_pins[pdev->id].pctrl, ssp_pins[pdev->id].pcs_gpio_sleep) < 0) {
3618 printk("spi%d setting cs_gpio pin ctrl failed\n",pdev->id);
3619 return -1;
3620 }
3621 }
3622 if(zx29spi&&zx29spi->master->slave == false) {
3623 if (pinctrl_select_state(ssp_pins[pdev->id].pctrl, ssp_pins[pdev->id].pcs_gpio_sleep) < 0) {
3624 printk("spi%d setting cs_gpio_sleep pin ctrl failed\n",pdev->id);
3625 return -1;
3626 }
3627 printk("spi%d setting cs_gpio_sleep pin ctrl\n",pdev->id);
3628 }
3629 return 0;
3630}
3631
3632static int zx29_spi_resume(struct platform_device *pdev)
3633{
3634 struct zx29_spi *zx29spi = NULL;
3635
3636 if(pdev == NULL)
3637 return -1;
3638 if(pdev && &pdev->dev)
3639 pinctrl_pm_select_default_state(&pdev->dev);
3640
3641 zx29spi = (struct zx29_spi *)platform_get_drvdata(pdev);
3642 if(zx29spi && (zx29spi->master->slave == true)) {
3643 if (pinctrl_select_state(ssp_pins[pdev->id].pctrl, ssp_pins[pdev->id].pcs_func) < 0) {
3644 printk("spi%d setting cs_func pin ctrl failed\n",pdev->id);
3645 return -1;
3646 }
3647 }
3648 if(zx29spi&&zx29spi->master->slave == false) {
3649 if (pinctrl_select_state(ssp_pins[pdev->id].pctrl, ssp_pins[pdev->id].pcs_gpio_active) < 0) {
3650 printk("spi%d setting cs_gpio_active pin ctrl failed\n",pdev->id);
3651 return -1;
3652 }
3653 printk("spi%d setting cs_gpio_active pin ctrl\n",pdev->id);
3654 gpio_direction_output(ssp_pins[pdev->id].gpio_cs,SPI_GPIO_HIGH);
3655 }
3656 return 0;
3657}
3658#endif
3659
3660static struct platform_driver zx29_spi_driver = {
3661 .driver = {
3662 .name = "zx29_ssp",
3663 .of_match_table = of_match_ptr(zx29_spi_of_match),
3664 .owner = THIS_MODULE,
3665 },
3666 .probe = zx29_spi_probe,
3667 #ifdef CONFIG_PM
3668 .suspend = zx29_spi_suspend,
3669 .resume = zx29_spi_resume,
3670 #endif
3671 .remove = __exit_p(zx29_spi_remove),
3672};
3673
3674static int __init zx29_spi_init(void)
3675{
3676 return platform_driver_register(&zx29_spi_driver);
3677}
3678
3679static void __exit zx29_spi_exit(void)
3680{
3681 platform_driver_unregister(&zx29_spi_driver);
3682}
3683
3684module_init(zx29_spi_init);
3685module_exit(zx29_spi_exit);
3686
3687MODULE_DESCRIPTION("zx29 spi controller driver");
3688MODULE_AUTHOR("zte");
3689MODULE_LICENSE("GPL");
3690