blob: fd49fd6586705e1b1ef3bb12ed12ced00d48eaa7 [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,
xf.li1867bfa2024-08-20 02:32:16 -07002344 .rx_lev_trig = SPI_RX_8_OR_MORE_ELEM,
xf.li2f424182024-08-20 00:47:34 -07002345 .tx_lev_trig = SPI_TX_4_OR_MORE_EMPTY_LOC,
2346// .ctrl_len = SSP_BITS_8,
2347// .wait_state = SSP_MWIRE_WAIT_ZERO,
2348// .duplex = SSP_MICROWIRE_CHANNEL_FULL_DUPLEX,
2349// .cs_control = default_cs_control,
2350};
2351
2352/*
2353
2354*/
2355static void spi_set_gpio_function(int dev_id)
2356{
2357 if (pinctrl_select_state(ssp_pins[dev_id].pctrl, ssp_pins[dev_id].pfunc) < 0) {
2358 printk("spi%d setting spi pin ctrl failed\n",dev_id);
2359 }
2360 return;
2361}
2362static void spi_set_gpio_gpio(int dev_id)
2363{
2364 if (pinctrl_select_state(ssp_pins[dev_id].pctrl, ssp_pins[dev_id].pgpio) < 0) {
2365 printk("spi%d setting spi pin ctrl failed\n",dev_id);
2366 }
2367 return;
2368}
2369
2370
2371static void spi_set_gpio_val(int gpio_num, int val)
2372{
2373 //zx29_gpio_output_data(gpio_num, val);
2374 gpio_set_value(gpio_num,val);
2375
2376}
2377
2378static int spi_get_gpio_val(int gpio_num)
2379{
2380 //zx29_gpio_set_direction(gpio,GPIO_IN);
2381
2382 return gpio_get_value(gpio_num);
2383}
2384
2385static void spi_time_delay(int delay/*us*/)
2386{
2387 udelay(delay);
2388}
2389
2390void spi_fun_mode_stop(int dev_id)
2391{
2392 spi_set_gpio_gpio(dev_id);
2393}
2394
2395void spi_gpio_mode_start(int dev_id)
2396{
2397 //mutex_lock(&g_zx29_spi->spi_lock); //spi control function mutex.
2398 /* set clk tx rx cs to gpio */
2399 //spi_set_gpio_gpio(dev_id);
2400 gpio_direction_output(ssp_pins[dev_id].gpio_cs,SPI_GPIO_HIGH);
2401 gpio_direction_output(ssp_pins[dev_id].gpio_clk,SPI_GPIO_LOW);
2402 gpio_direction_output(ssp_pins[dev_id].gpio_tx,0);//value ?
2403 gpio_direction_input(ssp_pins[dev_id].gpio_rx);
2404
2405 return ;
2406}
2407EXPORT_SYMBOL(spi_gpio_mode_start);
2408void spi_gpio_mode_stop(int dev_id)
2409{
2410 /* set clk tx rx cs to function */
2411 spi_set_gpio_function(dev_id);
2412 //mutex_unlock(&g_zx29_spi->spi_lock); //spi control function mutex.
2413}
2414EXPORT_SYMBOL(spi_gpio_mode_stop);
2415
2416void spi_gpio_write_single8(int dev_id,unsigned char data)
2417{
2418 int i;
2419
2420 //printk("spi_gpio_write_single8 %x\n", data);
2421
2422 spi_set_gpio_val(ssp_pins[dev_id].gpio_clk, SPI_GPIO_HIGH);
2423 spi_set_gpio_val(ssp_pins[dev_id].gpio_cs, SPI_GPIO_LOW);/* CS invail*/
2424
2425 for( i=7; i>=0; i-- )
2426 {
2427 spi_set_gpio_val(ssp_pins[dev_id].gpio_clk, SPI_GPIO_LOW);
2428 if ((data >> i) & 0x1)
2429 {
2430 spi_set_gpio_val(ssp_pins[dev_id].gpio_tx, SPI_GPIO_HIGH);
2431 }
2432 else
2433 {
2434 spi_set_gpio_val(ssp_pins[dev_id].gpio_tx, SPI_GPIO_LOW);
2435 }
2436 spi_time_delay(1);
2437 spi_set_gpio_val(ssp_pins[dev_id].gpio_clk, SPI_GPIO_HIGH);
2438 spi_time_delay(1);
2439 }
2440 spi_set_gpio_val(ssp_pins[dev_id].gpio_cs, SPI_GPIO_HIGH);
2441 spi_set_gpio_val(ssp_pins[dev_id].gpio_tx, SPI_GPIO_LOW);
2442
2443}
2444EXPORT_SYMBOL(spi_gpio_write_single8);
2445/*******************************************************************************
2446 * Function:
2447 * Description:
2448 * Parameters:
2449 * Input:
2450 *
2451 * Output:
2452 *
2453 * Returns:
2454 *
2455 *
2456 * Others:
2457 ********************************************************************************/
2458unsigned char spi_gpio_read_single8(int dev_id)
2459{
2460 int i;
2461 unsigned char readData = 0;
2462
2463 spi_set_gpio_val(ssp_pins[dev_id].gpio_clk, SPI_GPIO_HIGH);
2464 spi_set_gpio_val(ssp_pins[dev_id].gpio_cs, SPI_GPIO_LOW);/* CS */
2465
2466 for( i=7; i>=0; i-- )
2467 {
2468 spi_set_gpio_val(ssp_pins[dev_id].gpio_clk, SPI_GPIO_LOW);
2469 spi_time_delay(1);
2470 spi_set_gpio_val(ssp_pins[dev_id].gpio_clk, SPI_GPIO_HIGH);
2471 if( spi_get_gpio_val(ssp_pins[dev_id].gpio_rx) )/* lcd tx rx */
2472 {
2473 readData |= (1 << i);
2474 }
2475 spi_time_delay(1);
2476 }
2477 spi_set_gpio_val(ssp_pins[dev_id].gpio_cs, SPI_GPIO_HIGH);
2478
2479 //printk("spi_gpio_read_single8 %x\n", readData);
2480 return readData;
2481}
2482EXPORT_SYMBOL(spi_gpio_read_single8);
2483
2484/**
2485 * @brief spi gpio mode, cs control
2486 *
2487 * This function used for lcd 3-wires spi mode.
2488 * before cs pull down, spi pads will change to gpio mode.
2489 * after cs pull high, spi pads gpio mode recovery to spi mode.
2490 *
2491 * @param level 0: cs line pull down, no-zero: cs line pull up.
2492 *
2493 * @retval none
2494 */
2495void spi_gpio_3wire_cs(int dev_id,unsigned char level)
2496{
2497 if(level){
2498 spi_set_gpio_val(ssp_pins[dev_id].gpio_cs,SPI_GPIO_HIGH);
2499 gpio_direction_input(ssp_pins[dev_id].gpio_tx);
2500
2501 /* zx29_gpio_function_sel(GPIO_AP_SPI0_CS, GPIO_AP_SPI0_CS_FUN); */
2502 //zx29_gpio_function_sel(GPIO_AP_SPI0_CLK, GPIO_AP_SPI0_CLK_FUN);
2503 //zx29_gpio_function_sel(GPIO_AP_SPI0_TXD, GPIO_AP_SPI0_TXD_FUN);
2504
2505 //mutex_unlock(&g_zx29_spi->spi_lock); //spi control function mutex.
2506 }
2507 else{
2508 //mutex_lock(&g_zx29_spi->spi_lock);
2509
2510 /* zx29_gpio_function_sel(GPIO_AP_SPI0_CS, GPIO_AP_CS_GPIO_FUN); */
2511 //zx29_gpio_function_sel(GPIO_AP_SPI0_CLK, GPIO_AP_CLK_GPIO_FUN);
2512 //zx29_gpio_function_sel(GPIO_AP_SPI0_TXD, GPIO_AP_TXD_GPIO_FUN);
2513
2514 gpio_direction_output(ssp_pins[dev_id].gpio_cs,SPI_GPIO_LOW);
2515 gpio_direction_output(ssp_pins[dev_id].gpio_clk,SPI_GPIO_LOW);
2516 gpio_direction_output(ssp_pins[dev_id].gpio_tx,SPI_GPIO_LOW);
2517
2518 spi_set_gpio_val(ssp_pins[dev_id].gpio_clk,SPI_GPIO_LOW);
2519 spi_set_gpio_val(ssp_pins[dev_id].gpio_cs,SPI_GPIO_LOW);
2520 }
2521}
2522EXPORT_SYMBOL(spi_gpio_3wire_cs);
2523
2524/**
2525 * @brief spi gpio mode, one byte write.
2526 *
2527 * This function used for lcd 3-wires spi mode.
2528 * txd line used tx function and rx function at different time.
2529 *
2530 * @param reg one byte write data.
2531 *
2532 * @retval none
2533 */
2534void spi_gpio_3wire_write8(int dev_id,unsigned char reg)
2535{
2536 int i;
2537 //unsigned char readData = 0;
2538
2539 //write
2540 spi_time_delay(50);
2541 for (i = 0; i < 8; i++)
2542 {
2543 gpio_set_value(ssp_pins[dev_id].gpio_clk, SPI_GPIO_LOW);
2544 spi_time_delay(50);
2545
2546 if ((reg & 0x80)==0x80)
2547 {
2548 gpio_set_value(ssp_pins[dev_id].gpio_tx, SPI_GPIO_HIGH);
2549 }
2550 else
2551 {
2552 gpio_set_value(ssp_pins[dev_id].gpio_tx, SPI_GPIO_LOW);
2553 }
2554 spi_time_delay(50);
2555
2556 gpio_set_value(ssp_pins[dev_id].gpio_clk, SPI_GPIO_HIGH);
2557 spi_time_delay(50);
2558
2559 reg <<= 1;
2560 }
2561 //spi_time_delay(50);
2562}
2563EXPORT_SYMBOL(spi_gpio_3wire_write8);
2564
2565/**
2566 * @brief spi gpio mode, one byte read.
2567 *
2568 * This function used for lcd 3-wires spi mode.
2569 * txd line used tx function and rx function at different time.
2570 *
2571 * @param none.
2572 *
2573 * @retval one byte readed data.
2574 */
2575unsigned char spi_gpio_3wire_read8(int dev_id)
2576{
2577 int i;
2578 unsigned char readData = 0;
2579 //read
2580 gpio_direction_input(ssp_pins[dev_id].gpio_tx);
2581 spi_time_delay(50);
2582
2583 readData = 0;
2584 for (i = 0; i < 8; i++)
2585 {
2586 readData <<= 1;
2587 gpio_set_value(ssp_pins[dev_id].gpio_clk, SPI_GPIO_LOW);
2588 spi_time_delay(50);
2589
2590 if (SPI_GPIO_HIGH == gpio_get_value(ssp_pins[dev_id].gpio_tx))
2591 {
2592 readData |= 0x01;
2593 }
2594
2595 gpio_set_value(ssp_pins[dev_id].gpio_clk, SPI_GPIO_HIGH);
2596 spi_time_delay(50);
2597 }
2598 //spi_time_delay(50);
2599
2600 //printk("spi_gpio_read_single8 %x\n", readData);
2601 return readData;
2602}
2603EXPORT_SYMBOL(spi_gpio_3wire_read8);
2604
2605
2606static void zx29_setup_to_regs(struct chip_data *chip,struct zx29_spi *zx29spi)
2607{
2608 unsigned int regval = 0;
2609 ktime_t k_time_start = 0;
2610 ktime_t k_time_end = 0;
2611 ktime_t diff_ns = 0;
xf.li1867bfa2024-08-20 02:32:16 -07002612 /* yu.dong@20240715 [T106BUG-641] SPI packet loss problem, merged into ZXW patch start */
2613 /* yu.dong@20240617 [T106BUG-641] SPI packet loss problem, add kernel buffer scheme start */
2614 if(false == zx29spi->master->slave)
2615 pm_stay_awake(&zx29spi->pdev->dev);
xf.li2f424182024-08-20 00:47:34 -07002616 regval = readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase)) & (~SPI_COM_CTRL_MASK_SSPE);
2617 writel(regval, (SPI_COM_CTRL_OFFSET+zx29spi->virtbase));
2618
2619 writel(chip->fmt_ctrl, (SPI_FMT_CTRL_OFFSET+zx29spi->virtbase));
2620 //writel(chip->fifo_ctrl, (SPI_FIFO_CTRL_OFFSET+zx29spi->virtbase));
2621 /* yu.dong@20240617 [T106BUG-641] SPI packet loss problem, add kernel buffer scheme end */
2622 writel(chip->com_ctrl, (SPI_COM_CTRL_OFFSET + zx29spi->virtbase));
2623 //writel(chip->timing, (SPI_TIMING_OFFSET + zx29spi->virtbase));
2624
2625 writel(readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase)) | SPI_COM_CTRL_MASK_SSPE, (SPI_COM_CTRL_OFFSET+zx29spi->virtbase));
2626
2627 //while(((readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase))>> 4)&0x1) == 0);
2628 k_time_start = ktime_get();
2629 do {
2630 regval = readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase)) & SPI_COM_CTRL_MASK_SSPE_BACK;
2631 diff_ns = ktime_sub(ktime_get(),k_time_start);
2632 cpu_relax();
2633 }
2634 while (!regval && diff_ns < 10000000);
2635
2636 if(diff_ns >= 10000000) {
2637 dev_info(&zx29spi->pdev->dev, " zx29_setup_to_regs failed! diff_ns=%lld \n",diff_ns);
2638 }
xf.li1867bfa2024-08-20 02:32:16 -07002639
2640 if(false == zx29spi->master->slave)
2641 pm_relax(&zx29spi->pdev->dev);
2642 /* yu.dong@20240715 [T106BUG-641] SPI packet loss problem, merged into ZXW patch end */
xf.li2f424182024-08-20 00:47:34 -07002643}
2644/**
2645 * zx29_setup - setup function registered to SPI master framework
2646 * @spi: spi device which is requesting setup
2647 *
2648 * This function is registered to the SPI framework for this SPI master
2649 * controller. If it is the first time when setup is called by this device,
2650 * this function will initialize the runtime state for this chip and save
2651 * the same in the device structure. Else it will update the runtime info
2652 * with the updated chip info. Nothing is really being written to the
2653 * controller hardware here, that is not done until the actual transfer
2654 * commence.
2655 */
2656static int zx29_setup(struct spi_device *spi)
2657{
2658 struct spi_config_chip const *chip_info;
2659 struct chip_data *chip;
2660 unsigned speed_hz;
2661 int status = 0;
2662 struct zx29_spi *zx29spi = NULL;
2663 unsigned int bits =0;
2664 u8 iface = 0;
2665 u32 tmp;
2666
2667 if (!spi)
2668 return -EINVAL;
2669 bits = spi->bits_per_word;
2670 zx29spi = spi_master_get_devdata(spi->master);
2671 if (!zx29spi)
2672 return -EINVAL;
2673 iface = zx29spi->iface_mode;
2674
2675 /* Get controller_state if one is supplied */
2676 chip = spi_get_ctldata(spi);
2677
2678 if (chip == NULL) {
2679 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
2680 if (!chip) {
2681 dev_err(&spi->dev, "cannot allocate controller state\n");
2682 return -ENOMEM;
2683 }
2684 dev_dbg(&spi->dev, "allocated memory for controller's runtime state\n");
2685 }
2686
2687 /* Get controller data if one is supplied */
2688 chip_info = spi->controller_data;
2689
2690 if (chip_info == NULL) {
2691 chip_info = &spi_default_chip_info;
2692 /* spi_board_info.controller_data not is supplied */
2693 dev_dbg(&spi->dev, "using default controller_data settings\n");
2694 } else
2695 dev_dbg(&spi->dev, "using user supplied controller_data settings\n");
2696
2697 /*
2698 * We can override with custom divisors, else we use the board
2699 * frequency setting
2700 */
2701
2702 /* set spi clock source at 104MHz/1 */
2703 //writel(chip ->clk_div-1, M0_SSP_CLKDIV_REG_VA);
2704 speed_hz = spi->max_speed_hz;
2705// clk_set_rate(zx29spi->spi_clk, speed_hz * 2); /* f(ssp_clk) = 2*f(ssp_sclk_out) */
2706 spi->max_speed_hz = clk_round_rate(zx29spi->spi_clk, speed_hz * 2) / 2;
2707
2708 if (spi->max_speed_hz != speed_hz)
2709 dev_dbg(&spi->dev, "round speed %dHz differs from requested %dHz.", spi->max_speed_hz, speed_hz);
2710
2711 status = verify_controller_parameters(zx29spi, chip_info);
2712 if (status) {
2713 dev_err(&spi->dev, "controller data is incorrect");
2714 goto err_config_params;
2715 }
2716
2717 zx29spi->rx_lev_trig = chip_info->rx_lev_trig;
2718 zx29spi->tx_lev_trig = chip_info->tx_lev_trig;
2719
2720 /* Now set controller state based on controller data */
2721 //chip->xfer_type = chip_info->com_mode;
2722 chip->xfer_type = spi->dma_used ? DMA_TRANSFER : POLLING_TRANSFER;
2723 dev_dbg(&spi->dev, "chip->xfer_type = 0x%x \n",chip->xfer_type);
2724
2725 if (!chip_info->cs_control) {
2726 chip->cs_control = default_cs_control;
2727
2728 if (spi->master->num_chipselect != 1)
2729 dev_err(&spi->dev, "chip select function is NULL!\n");
2730 } else
2731 chip->cs_control = chip_info->cs_control;
2732
2733 /* Check bits per word with vendor specific range */
2734 if ((bits <= 3) || (bits > zx29spi->vendor->max_bpw)) {
2735 status = -ENOTSUPP;
2736 dev_err(&spi->dev, "illegal data size for this controller!\n");
2737 dev_err(&spi->dev, "This controller can only handle 4 <= n <= %d bit words\n",
2738 zx29spi->vendor->max_bpw);
2739 goto err_config_params;
2740 } else if (bits <= 8) {
2741 dev_dbg(&spi->dev, "4 <= n <=8 bits per word\n");
2742 chip->n_bytes = 1;
2743 chip->read = READING_U8;
2744 chip->write = WRITING_U8;
2745 } else if (bits <= 16) {
2746 dev_dbg(&spi->dev, "9 <= n <= 16 bits per word\n");
2747 chip->n_bytes = 2;
2748 chip->read = READING_U16;
2749 chip->write = WRITING_U16;
2750 } else {
2751 dev_dbg(&spi->dev, "17 <= n <= 32 bits per word\n");
2752 chip->n_bytes = 4;
2753 chip->read = READING_U32;
2754 chip->write = WRITING_U32;
2755 }
2756
2757 /* Now Initialize all register settings required for this chip */
2758 chip->com_ctrl = 0;
2759 chip->fmt_ctrl = 0;
2760 chip->fifo_ctrl = 0;
2761 chip->timing = 0;
2762
2763 if ((chip->xfer_type == DMA_TRANSFER)
2764 && ((zx29spi->master_info)->enable_dma)) {
2765 chip->enable_dma = true;
2766 dev_dbg(&spi->dev, "DMA mode set in controller state\n");
2767 } else {
2768 chip->enable_dma = false;
2769 dev_dbg(&spi->dev, "DMA mode NOT set in controller state\n");
2770 }
2771 SPI_WRITE_BITS(chip->fifo_ctrl, SPI_DMA_DISABLED, SPI_FIFO_CTRL_MASK_RX_DMA_EN, 2);
2772 SPI_WRITE_BITS(chip->fifo_ctrl, SPI_DMA_DISABLED, SPI_FIFO_CTRL_MASK_TX_DMA_EN, 3);
2773
2774 if (zx29spi->rx_lev_trig == SPI_RX_8_OR_MORE_ELEM)
2775 SPI_WRITE_BITS(chip->fifo_ctrl, SPI_FIFO_THRES_8, SPI_FIFO_CTRL_MASK_RX_FIFO_THRES, 4);
2776 else
2777 SPI_WRITE_BITS(chip->fifo_ctrl, SPI_FIFO_THRES_4, SPI_FIFO_CTRL_MASK_RX_FIFO_THRES, 4);
2778 if (zx29spi->tx_lev_trig == SPI_TX_8_OR_MORE_EMPTY_LOC)
2779 SPI_WRITE_BITS(chip->fifo_ctrl, SPI_FIFO_THRES_8, SPI_FIFO_CTRL_MASK_TX_FIFO_THRES, 8);
2780 else
2781 SPI_WRITE_BITS(chip->fifo_ctrl, SPI_FIFO_THRES_4, SPI_FIFO_CTRL_MASK_TX_FIFO_THRES, 8);
2782
2783 SPI_WRITE_BITS(chip->fmt_ctrl, bits - 1, SPI_FMT_CTRL_MASK_DSS, 4);
2784 SPI_WRITE_BITS(chip->fmt_ctrl, chip_info->iface, SPI_FMT_CTRL_MASK_FRF, 0);
2785
2786 if((iface== SPI_TI_FORMAT)||(iface== SPI_ISI_FORMAT)){
2787 printk("qhf %s set iface = %d\n",__func__,iface);
2788 SPI_WRITE_BITS(chip->fmt_ctrl, iface, SPI_FMT_CTRL_MASK_FRF, 0);
2789 }
2790 /* Stuff that is common for all versions */
2791 if (spi->mode & SPI_CPOL)
2792 tmp = SPI_CLK_POL_IDLE_HIGH;
2793 else
2794 tmp = SPI_CLK_POL_IDLE_LOW;
2795 SPI_WRITE_BITS(chip->fmt_ctrl, tmp, SPI_FMT_CTRL_MASK_POL, 2);
2796
2797 if (spi->mode & SPI_CPHA)
2798 tmp = SPI_CLK_SECOND_EDGE;
2799 else
2800 tmp = SPI_CLK_FIRST_EDGE;
2801
2802 SPI_WRITE_BITS(chip->fmt_ctrl, tmp, SPI_FMT_CTRL_MASK_PHA, 3);
2803
2804 /* Loopback is available on all versions except PL023 */
2805 if (zx29spi->vendor->loopback) {
2806 if (spi->mode & SPI_LOOP)
2807 tmp = LOOPBACK_ENABLED;
2808 else
2809 tmp = LOOPBACK_DISABLED;
2810 SPI_WRITE_BITS(chip->com_ctrl, tmp, SPI_COM_CTRL_MASK_LBM, 0);
2811 }
2812// SPI_WRITE_BITS(chip->com_ctrl, SPI_ENABLED, SPI_COM_CTRL_MASK_SSPE, 1);
2813 SPI_WRITE_BITS(chip->com_ctrl, chip_info->hierarchy, SPI_COM_CTRL_MASK_MS, 2);
2814// SPI_WRITE_BITS(chip->com_ctrl, chip_info->slave_tx_disable, SPI_COM_CTRL_MASK_SOD, 3);
2815
2816 if(spi->trans_gaped) {
2817 chip->enable_trans_gap = true;
2818 }
2819 SPI_WRITE_BITS(chip->timing, spi->trans_gap_num, SPI_TIMING_MASK_T_CS_DESEL, 0);
2820 /* Save controller_state */
2821 spi_set_ctldata(spi, chip);
2822 if(zx29spi->mode == ZX29_SSP_SLAVE_TYPE) {
2823
2824 SPI_WRITE_BITS(chip->com_ctrl, SPI_SLAVE_MODE, SPI_COM_CTRL_MASK_MS, 2);
2825 zx29_setup_to_regs(chip,zx29spi);
2826 }
2827
xf.li1867bfa2024-08-20 02:32:16 -07002828 if(zx29spi->mode == ZX29_SSP_MASTER_TYPE) {
2829 if(spi->setup_immediately == 1)
2830 zx29_setup_to_regs(chip,zx29spi);
2831 }
xf.li2f424182024-08-20 00:47:34 -07002832
2833 return status;
2834 err_config_params:
2835 spi_set_ctldata(spi, NULL);
2836 kfree(chip);
2837 return status;
2838}
2839
2840/**
2841 * zx29_cleanup - cleanup function registered to SPI master framework
2842 * @spi: spi device which is requesting cleanup
2843 *
2844 * This function is registered to the SPI framework for this SPI master
2845 * controller. It will free the runtime state of chip.
2846 */
2847static void zx29_cleanup(struct spi_device *spi)
2848{
2849 struct chip_data *chip = spi_get_ctldata(spi);
2850
2851 spi_set_ctldata(spi, NULL);
2852 kfree(chip);
2853}
2854
2855static int zx29_spi_clock_init(struct zx29_spi *zx29spi)
2856{
2857 int status = 0;
2858 struct platform_device *pdev = zx29spi->pdev;
2859 /* work clock */
2860 zx29spi->spi_clk = devm_clk_get(&pdev->dev, "work_clk");
2861 if (IS_ERR(zx29spi->spi_clk)) {
2862 status = PTR_ERR(zx29spi->spi_clk);
2863 dev_err(&pdev->dev, "could not retrieve SPI work clock\n");
2864 return status;
2865 }
2866
2867 /* enable spiclk at function zx29_setup */
2868
2869 if (device_property_read_u32(&pdev->dev, "clock-frequency", &zx29spi->clkfreq))
2870 zx29spi->clkfreq = SPI_SPICLK_FREQ_26M;
2871 status = clk_set_rate(zx29spi->spi_clk, zx29spi->clkfreq);
2872 if(status) {
2873 dev_err(&pdev->dev,"clc_set_rate err status=%d \n",status);
2874 return status;
2875 }
2876 /* enable ssp clock source */
2877 clk_prepare_enable(zx29spi->spi_clk);
2878
2879 /* apb clock */
2880 zx29spi->pclk = devm_clk_get(&pdev->dev, "apb_clk");
2881 if (IS_ERR(zx29spi->pclk)) {
2882 status = PTR_ERR(zx29spi->pclk);
2883 dev_err(&pdev->dev, "could not retrieve SPI work clock\n");
2884 return status;
2885 }
2886 clk_prepare_enable(zx29spi->pclk);
2887
2888 return status;
2889}
2890
2891static void spicc_clkgate_ctrl(struct zx29_spi *zx29spi,unsigned char is_enable)
2892{
2893 if (is_enable) {
2894 clk_enable(zx29spi->spi_clk);
2895 clk_enable(zx29spi->pclk);
2896 } else {
2897 clk_disable(zx29spi->spi_clk);
2898 clk_disable(zx29spi->pclk);
2899 }
2900}
2901
2902static int zx29_spi_slave_clock_init(struct zx29_spi *zx29spi)
2903{
2904 int status=0;
2905 struct platform_device *pdev = zx29spi->pdev;
2906 /* work clock */
2907 zx29spi->spi_clk = devm_clk_get(&pdev->dev, "work_clk");
2908 if (IS_ERR(zx29spi->spi_clk)) {
2909 status = PTR_ERR(zx29spi->spi_clk);
2910 dev_err(&pdev->dev, "could not retrieve SPI work clock\n");
2911 return status;
2912 }
2913
2914 /* enable spiclk at function zx29_setup */
2915 //if (device_property_read_u32(&pdev->dev, "clock-frequency", &zx29spi->clkfreq))
2916 zx29spi->clkfreq = SPI_SPICLK_FREQ_156M; /*salve */
2917
2918 status = clk_set_rate(zx29spi->spi_clk, zx29spi->clkfreq);
2919 if(status) {
2920 dev_err(&pdev->dev,"clc_set_rate err status=%d \n",status);
2921 return status;
2922 }
2923 /* enable ssp clock source */
2924 clk_prepare_enable(zx29spi->spi_clk);
2925
2926 /* apb clock */
2927 zx29spi->pclk = devm_clk_get(&pdev->dev, "apb_clk");
2928 if (IS_ERR(zx29spi->pclk)) {
2929 status = PTR_ERR(zx29spi->pclk);
2930 dev_err(&pdev->dev, "could not retrieve SPI work clock\n");
2931 return status;
2932 }
2933 clk_prepare_enable(zx29spi->pclk);
2934 spicc_clkgate_ctrl(zx29spi,true);
2935
2936 return status;
2937}
2938
2939
2940static int zx29_spi_init_pinctrl(struct platform_device *pdev)
2941{
2942 struct pinctrl *pctrl;
2943 enum of_gpio_flags flags;
2944 int ret;
2945 struct zx29_spi *zx29spi = NULL;
2946
2947 if(!pdev) {
2948 printk("pdev not exist \n");
2949 return -1;
2950 }
2951 zx29spi = (struct zx29_spi *)platform_get_drvdata(pdev);
2952 if (IS_ERR(zx29spi)) {
2953 dev_warn(&pdev->dev, "Failed to get zx29->ssp%d pins",pdev->id);
2954 pctrl = NULL;
2955 return 0;
2956 }
2957
2958 pctrl = devm_pinctrl_get(&pdev->dev);
2959 if (IS_ERR(pctrl)) {
2960 dev_warn(&pdev->dev, "Failed to get zx29->ssp%d pins",pdev->id);
2961 pctrl = NULL;
2962 return 0;
2963 }
2964 ssp_pins[pdev->id].pctrl=pctrl;
2965
2966 ssp_pins[pdev->id].pcs_gpio_active = pinctrl_lookup_state(pctrl, "cs_gpio_active");
2967 if (IS_ERR(ssp_pins[pdev->id].pcs_gpio_active)) {
2968 dev_err(&pdev->dev, "missing cs_gpio_active \n");
2969 }
2970
2971 ssp_pins[pdev->id].pcs_gpio_sleep = pinctrl_lookup_state(pctrl, "cs_gpio_sleep");
2972 if (IS_ERR(ssp_pins[pdev->id].pcs_gpio_sleep)) {
2973 dev_err(&pdev->dev, "missing cs_gpio_sleep \n");
2974 }
2975 ssp_pins[pdev->id].pcs_func = pinctrl_lookup_state(ssp_pins[pdev->id].pctrl, "cs_func");
2976 if (IS_ERR(ssp_pins[pdev->id].pcs_func)) {
2977 dev_err(&pdev->dev, "missing cs_func \n");
2978 }
2979
2980 if(zx29spi->master->slave == false) {
2981 if (pinctrl_select_state(ssp_pins[pdev->id].pctrl, ssp_pins[pdev->id].pcs_gpio_active) < 0) {
2982 printk("spi%d setting cs_gpio pin ctrl failed\n",pdev->id);
2983 }
2984 }else {
2985 if (pinctrl_select_state(ssp_pins[pdev->id].pctrl, ssp_pins[pdev->id].pcs_func) < 0) {
2986 printk("spi%d setting cs_func pin ctrl failed\n",pdev->id);
2987 }
2988 }
2989
2990 ssp_pins[pdev->id].gpio_cs = of_get_gpio_flags(pdev->dev.of_node, 0, &flags);
2991 if (!gpio_is_valid(ssp_pins[pdev->id].gpio_cs)) {
2992 pr_info("ssp%d gpio_cs no found\n",pdev->id);
2993 }
2994
2995 ssp_pins[pdev->id].gpio_clk = of_get_gpio_flags(pdev->dev.of_node, 1, &flags);
2996 if (!gpio_is_valid(ssp_pins[pdev->id].gpio_clk)) {
2997 pr_info("ssp%d gpio_clk no found\n",pdev->id);
2998 }
2999 ssp_pins[pdev->id].gpio_tx = of_get_gpio_flags(pdev->dev.of_node, 2, &flags);
3000 if (!gpio_is_valid(ssp_pins[pdev->id].gpio_tx)) {
3001 pr_info("ssp%d gpio_tx no found\n",pdev->id);
3002 }
3003
3004 ssp_pins[pdev->id].gpio_rx = of_get_gpio_flags(pdev->dev.of_node, 3, &flags);
3005 if (!gpio_is_valid(ssp_pins[pdev->id].gpio_rx)) {
3006 pr_info("ssp%d gpio_rx no found\n",pdev->id);
3007 }
3008
3009 if(zx29spi->master->slave == false)
3010 gpio_direction_output(ssp_pins[pdev->id].gpio_cs,SPI_GPIO_HIGH);
3011
3012 return 0;
3013}
3014static void zx29_spi_get_platformInfo(struct platform_device *pdev,struct zx29_spi_controller *platform_info)
3015{
3016 struct device *dev=&pdev->dev;
3017 u32 dma_tx,dma_rx,enable_dma;
3018
3019
3020 if (device_property_read_u16(dev, "bus_id", &platform_info->bus_id)) {
3021 platform_info->bus_id = pdev->id;
3022 }
3023 if (device_property_read_u8(dev, "num_chipselect", &platform_info->num_chipselect)) {
3024 platform_info->num_chipselect = 1;
3025 }
3026#if 0
3027 if (device_property_read_u32(dev, "enable_dma",&enable_dma)) {
3028 dev_err(&pdev->dev,"enable_dma get failed");
3029 platform_info->enable_dma = 0;
3030 }
3031 else {
3032 platform_info->enable_dma = enable_dma;
3033 }
3034#endif
3035 if (device_property_read_u32(dev, "autosuspend_delay", &platform_info->autosuspend_delay))
3036 platform_info->autosuspend_delay = 0;
3037
3038 if(device_property_read_u32(dev, "dma_rx", &dma_rx)){
3039 dev_err(&pdev->dev,"dma_rx get failed");
3040 }
3041 platform_info->dma_rx_param = (void*)dma_rx;
3042 device_property_read_u32(dev, "dma_tx", &dma_tx);
3043 platform_info->dma_tx_param = (void*)dma_tx;
3044
3045 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);
3046
3047}
3048
3049#if defined(CONFIG_DEBUG_FS)
3050#define dump_register(reg) \
3051{ \
3052 .name = __stringify(reg), \
3053 .offset = SPI_ ##reg##_OFFSET, \
3054}
3055
3056
3057static const struct debugfs_reg32 spi_regs[] = {
3058 dump_register(VER_REG),
3059 dump_register(COM_CTRL),
3060 dump_register(FMT_CTRL),
3061 dump_register(DR),
3062 dump_register(FIFO_CTRL),
3063 dump_register(FIFO_SR),
3064 dump_register(INTR_EN),
3065 dump_register(INTR_SR),
3066 dump_register(TIMING),
3067};
3068
3069//#define Strcat(x, fmt, ...) sprintf(x, "%s" #fmt, x, __VA_ARGS__)
3070
3071static void debugfs_spi_init(struct zx29_spi *zx29spi)
3072{
3073 struct dentry *root;
3074 struct dentry *node;
3075 char tmp[32];
3076
3077 if(!zx29spi)
3078 return;
3079
3080 //create root
3081 sprintf(tmp,"spi%d_zx29", zx29spi->pdev->id);
3082 root = debugfs_create_dir(tmp, NULL);
3083 if (!root) {
3084 dev_err(&zx29spi->pdev->dev, "debugfs_create_dir %s err\n", tmp);
3085 goto err;
3086 }
3087
3088 //create regs
3089 zx29spi->spi_regset.regs = (struct debugfs_reg32 *)spi_regs;
3090 zx29spi->spi_regset.nregs = sizeof(spi_regs)/sizeof(struct debugfs_reg32);
3091 zx29spi->spi_regset.base = zx29spi->virtbase;
3092
3093 debugfs_create_regset32("spi_regs", S_IRUGO, root, &zx29spi->spi_regset);
3094 //create info
3095 debugfs_create_u32("poll_cnt", S_IRUGO, root, &zx29spi->spi_poll_cnt);
3096 debugfs_create_u32("dma_cnt", S_IRUGO, root, &zx29spi->spi_dma_cnt);
3097
3098 zx29spi->spi_root = (void *)root;
3099 return;
3100err:
3101 dev_err(&zx29spi->pdev->dev, "debugfs_spi_init err\n");
3102}
3103
3104#endif
3105
3106
3107static int zx29_spi_init_irq(struct platform_device *pdev, struct zx29_spi *zx29spi)
3108{
3109 int irq = 0,ret = 0;
3110
3111 if(!zx29spi || !pdev) {
3112 ret = -ENOENT;
3113 return ret;
3114 }
3115 irq = platform_get_irq(pdev, 0);
3116 if (irq == NULL) {
3117 dev_err(&pdev->dev, "Cannot get IORESOURCE_IRQ\n");
3118 ret = -ENOENT;
3119 return ret;
3120 }
3121 zx29spi->irq = irq;
3122 dev_dbg(&pdev->dev, "used interrupt num is %d\n", zx29spi->irq);
3123 ret = devm_request_irq(&pdev->dev, zx29spi->irq, zx29_spi_irq,
3124 IRQF_TRIGGER_HIGH | IRQF_NO_THREAD | IRQF_ONESHOT, dev_name(&pdev->dev), zx29spi);
3125 if (ret < 0) {
3126 dev_err(&pdev->dev, "probe - cannot get IRQ (%d)\n", ret);
3127 return ret;
3128 }
3129 disable_irq_nosync(zx29spi->irq);
3130 return ret;
3131
3132}
3133
3134static int zx29_spi_probe_of_master(struct platform_device *pdev)
3135{
3136 struct device *dev = &pdev->dev;
3137 struct zx29_spi_controller *platform_info=NULL ;
3138 struct spi_master *master;
3139 struct zx29_spi *zx29spi = NULL; /*Data for this driver */
3140 struct resource *regs = NULL;
3141 struct resource *gpio = NULL;
3142 struct resource *irq = NULL;
3143 struct device_node *np = pdev->dev.of_node;
3144 int status = 0, i,ret;
3145 u32 regval = 0;
3146
3147 platform_info = devm_kzalloc(&pdev->dev, sizeof(struct zx29_spi_controller), GFP_KERNEL);
3148 if(platform_info == NULL)
3149 return 0;
3150 platform_info->bus_id = 0,
3151 platform_info->num_chipselect = 1,
3152 platform_info->enable_dma = 1,
3153 platform_info->autosuspend_delay=0,
3154
3155 /* Allocate master with space for data */
3156 master = spi_alloc_master(dev, sizeof(struct zx29_spi));
3157 if (master == NULL) {
3158 dev_err(&pdev->dev, "probe - cannot alloc SPI master\n");
3159 status = -ENOMEM;
3160 goto err_no_master;
3161 }
3162
3163 zx29spi = spi_master_get_devdata(master);
3164 memset(zx29spi,0,sizeof(struct zx29_spi));
3165 pdev->id = of_alias_get_id(np, "spi");
3166 if(pdev->id < 0){
3167 printk("zx29_ssp of_alias_get_id fail ret:%d\n", pdev->id);
3168 status = -ENOMEM;
3169 goto err_no_master;
3170 }
3171 snprintf(zx29spi->name, sizeof(zx29spi->name), "zx29-spi%d", pdev->id);
3172 zx29_spi_get_platformInfo(pdev,platform_info);
3173 //mutex_init(&zx29spi->spi_lock);
3174 g_zx29_spi[pdev->id] = zx29spi;
3175 zx29spi->master = master;
3176 zx29spi->master_info = platform_info;
3177 zx29spi->pdev = pdev;
3178 zx29spi->vendor = &vendor_arm;
3179 zx29spi->mode = ZX29_SSP_MASTER_TYPE;
3180 zx29spi->zx29_flush_rxfifo = zx29_flush_rxfifo;
3181 sema_init(&zx29spi->sema_dma, 0);
3182 /* yu.dong@20240521 [T106BUG-616] SPI set to slave mode for read will get stuck start */
3183 init_waitqueue_head(&zx29spi->wait);
3184 zx29spi->trans_done = false;
3185 /* yu.dong@20240521 [T106BUG-616] SPI set to slave mode for read will get stuck end */
3186 dev_set_drvdata(&pdev->dev, zx29spi);
3187 device_init_wakeup(&pdev->dev, true);
3188 /*
3189 * Bus Number Which has been Assigned to this SSP controller
3190 * on this board
3191 */
3192 master->bus_num = platform_info->bus_id;
3193 master->num_chipselect = platform_info->num_chipselect;
3194 master->cleanup = zx29_cleanup;
3195 master->setup = zx29_setup;
3196 master->prepare_transfer_hardware = zx29_prepare_transfer_hardware;
3197 master->transfer_one_message = zx29_transfer_one_message;
3198 master->unprepare_transfer_hardware = zx29_unprepare_transfer_hardware;
3199 //master->rt = platform_info->rt;
3200
3201 /*
3202 * Supports mode 0-3, loopback, and active low CS..
3203 */
3204 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_NO_CS|SPI_LOOP;
3205
3206 dev_dbg(&pdev->dev, "BUSNO: %d\n", master->bus_num);
3207
3208 zx29_spi_init_pinctrl(pdev);
3209
3210 /* registers */
3211 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3212 if (regs == NULL){
3213 dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
3214 status = -ENOENT;
3215 goto err_no_registers;
3216 }
3217 zx29spi->phybase = regs->start;
3218 zx29spi->virtbase = devm_platform_ioremap_resource(pdev, 0);
3219 if (zx29spi->virtbase == NULL) {
3220 status = -ENOMEM;
3221 goto err_no_ioremap;
3222 }
3223 dev_dbg( &pdev->dev," mapped registers from 0x%08x to 0x%p\n",
3224 regs->start, zx29spi->virtbase);
3225
3226#if defined(CONFIG_DEBUG_FS)
3227 debugfs_spi_init(zx29spi);
3228#endif
3229
3230 /*clock init*/
3231 status = zx29_spi_clock_init(zx29spi);
3232 if(status)
3233 goto err_no_clk;
3234
3235 /* Initialize transfer pump */
3236 //tasklet_init(&zx29spi->pump_transfers, pump_transfers,(unsigned long)zx29spi);
3237
3238 /* Disable SPI */
3239 regval = readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase)) & (~SPI_COM_CTRL_MASK_SSPE);
3240 writel(regval, (SPI_COM_CTRL_OFFSET+zx29spi->virtbase));
3241
3242 load_spi_default_config(zx29spi);
3243 writel(0, (SPI_TIMING_OFFSET + zx29spi->virtbase));
3244 if(!strcmp(pdev->name,"1410000.ssp")) {
3245 regval = readl((SPI_FMT_CTRL_OFFSET+zx29spi->virtbase))&(~(0x1<<12));
3246 writel(regval, (SPI_FMT_CTRL_OFFSET+zx29spi->virtbase));
3247 dev_info(&pdev->dev,"%s set non-camera mode regval:0x%x \n",pdev->name,regval);
3248 }
3249 status = zx29_spi_init_irq(pdev,zx29spi);
3250 if(status != 0) {
3251 dev_err(&pdev->dev, "zx29_spi_init_irq err!!! \n");
3252 goto err_no_irq;
3253 }
3254 /* Get DMA channels */
3255 if (platform_info->enable_dma) {
3256 status = zx29_dma_probe(zx29spi);
3257 if (status != 0) {
3258 platform_info->enable_dma = 0;
3259 sc_debug_info_record(MODULE_ID_CAP_SPI, "%s dma probe failed \n",pdev->name);
3260 }
3261 }
3262
3263#if SPI_PSM_CONTROL
3264 wake_lock_init(&zx29spi->psm_lock, WAKE_LOCK_SUSPEND, zx29spi->name);
3265#endif
3266 master->dev.of_node = pdev->dev.of_node;
3267 ret = devm_spi_register_master(&pdev->dev, master);
3268 if (ret < 0) {
3269 dev_err(&pdev->dev, "can not register to master err %d\n", ret);
3270 goto err_spi_register;
3271 }
3272 dev_info(&pdev->dev," probe succeeded\n");
3273
3274 /* let runtime pm put suspend */
3275 if (platform_info->autosuspend_delay > 0) {
3276 dev_info(&pdev->dev, "will use autosuspend for runtime pm, delay %dms\n", platform_info->autosuspend_delay);
3277 pm_runtime_set_autosuspend_delay(dev, platform_info->autosuspend_delay);
3278 pm_runtime_use_autosuspend(dev);
3279 pm_runtime_put_autosuspend(dev);
3280 } else {
3281 pm_runtime_put(dev);
3282 }
3283
3284 return 0;
3285
3286 err_spi_register:
3287#if SPI_PSM_CONTROL
3288 wake_lock_destroy(&zx29spi->psm_lock);
3289#endif
3290 if (platform_info->enable_dma)
3291 zx29_dma_remove(zx29spi);
3292
3293 err_no_irq:
3294 clk_disable(zx29spi->spi_clk);
3295// err_no_clk_en:
3296 //clk_unprepare(pl022->clk);
3297 //err_clk_prep:
3298 clk_put(zx29spi->spi_clk);
3299 err_no_clk:
3300// iounmap(zx29spi->virtbase);
3301 err_gpios:
3302 /* add */
3303 err_no_ioremap:
3304 err_no_registers:
3305 spi_master_put(master);
3306 err_no_master:
3307 err_no_pdata:
3308 return status;
3309}
3310
3311static int zx29_spi_probe_of_slave(struct platform_device *pdev)
3312{
3313 struct device *dev = &pdev->dev;
3314 struct zx29_spi_controller *platform_info=NULL ;
3315 struct spi_master *master;
3316 struct zx29_spi *zx29spi = NULL; /*Data for this driver */
3317 struct resource *regs = NULL;
3318 struct resource *gpio = NULL;
3319 struct resource *irq = NULL;
3320 struct device_node *np = pdev->dev.of_node;
3321 int status = 0, i,ret;
3322 u32 regval = 0;
3323
3324 platform_info = devm_kzalloc(&pdev->dev, sizeof(struct zx29_spi_controller), GFP_KERNEL);
3325 if(platform_info == NULL)
3326 return 0;
3327 platform_info->bus_id = 0,
3328 platform_info->num_chipselect = 1,
3329 platform_info->enable_dma = 1,
3330 platform_info->autosuspend_delay=0,
3331
3332 /* Allocate master with space for data */
3333 master = spi_alloc_master(dev, sizeof(struct zx29_spi));
3334 if (master == NULL) {
3335 dev_err(&pdev->dev, "probe - cannot alloc SPI master\n");
3336 status = -ENOMEM;
3337 goto err_no_master;
3338 }
3339 master->slave = true;
3340 zx29spi = spi_master_get_devdata(master);
3341 memset(zx29spi,0,sizeof(struct zx29_spi));
3342 pdev->id = of_alias_get_id(np, "spi");
3343 if(pdev->id < 0){
3344 printk("zx29_ssp of_alias_get_id fail ret:%d\n", pdev->id);
3345 goto err_no_master;
3346 }
3347 snprintf(zx29spi->name, sizeof(zx29spi->name), "zx29-spi%d", pdev->id);
3348
3349 zx29_spi_get_platformInfo(pdev,platform_info);
3350 //mutex_init(&zx29spi->spi_lock);
3351 g_zx29_spi[pdev->id] = zx29spi;
3352 zx29spi->master = master;
3353 zx29spi->master_info = platform_info;
3354 zx29spi->pdev = pdev;
3355 zx29spi->vendor = &vendor_arm;
3356 zx29spi->mode = ZX29_SSP_SLAVE_TYPE;
3357 zx29spi->zx29_flush_rxfifo = zx29_flush_rxfifo;
3358 sema_init(&zx29spi->sema_dma, 0);
3359 /* yu.dong@20240521 [T106BUG-616] SPI set to slave mode for read will get stuck start */
3360 init_waitqueue_head(&zx29spi->wait);
3361 zx29spi->trans_done = false;
3362 /* yu.dong@20240521 [T106BUG-616] SPI set to slave mode for read will get stuck end */
3363 dev_set_drvdata(&pdev->dev, zx29spi);
3364 /*
3365 * Bus Number Which has been Assigned to this SSP controller
3366 * on this board
3367 */
3368 master->bus_num = platform_info->bus_id;
3369 master->num_chipselect = platform_info->num_chipselect;
3370 master->cleanup = zx29_cleanup;
3371 master->setup = zx29_setup;
3372 master->prepare_transfer_hardware = zx29_prepare_transfer_hardware;
3373 master->transfer_one_message = zx29_slave_transfer_one_message;
3374 master->unprepare_transfer_hardware = zx29_unprepare_transfer_hardware;
3375 /* yu.dong@20240617 [T106BUG-641] SPI packet loss problem, add kernel buffer scheme start */
3376 master->spi_slave_rd_start = zx29_slave_rd_start;
3377 master->spi_slave_rd_stop = zx29_slave_rd_stop;
3378 /* yu.dong@20240617 [T106BUG-641] SPI packet loss problem, add kernel buffer scheme end */
3379 //master->rt = platform_info->rt;
3380
3381 /*
3382 * Supports mode 0-3, loopback, and active low CS..
3383 */
3384 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_NO_CS|SPI_LOOP;
3385
3386 dev_dbg(&pdev->dev, "BUSNO: %d\n", master->bus_num);
3387
3388 zx29_spi_init_pinctrl(pdev);
3389
3390 /* registers */
3391 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3392 if (regs == NULL){
3393 dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
3394 status = -ENOENT;
3395 goto err_no_registers;
3396 }
3397 zx29spi->phybase = regs->start;
3398 zx29spi->virtbase = devm_platform_ioremap_resource(pdev, 0);
3399 if (zx29spi->virtbase == NULL) {
3400 status = -ENOMEM;
3401 goto err_no_ioremap;
3402 }
3403 dev_dbg( &pdev->dev," mapped registers from 0x%08x to 0x%p\n",
3404 regs->start, zx29spi->virtbase);
3405
3406#if defined(CONFIG_DEBUG_FS)
3407 debugfs_spi_init(zx29spi);
3408#endif
3409
3410 /*clock init*/
3411 status = zx29_spi_slave_clock_init(zx29spi);
3412 if(status)
3413 goto err_no_clk;
3414 /* Initialize transfer pump */
3415 //tasklet_init(&zx29spi->pump_transfers, pump_transfers,(unsigned long)zx29spi);
3416
3417 /* Disable SPI */
3418 regval = readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase)) & (~SPI_COM_CTRL_MASK_SSPE);
3419 writel(regval, (SPI_COM_CTRL_OFFSET+zx29spi->virtbase));
3420
3421 load_spi_default_config(zx29spi);
3422 writel(0, (SPI_TIMING_OFFSET + zx29spi->virtbase));
3423
3424 if(!strcmp(pdev->name,"1410000.ssp")) {
3425 regval = readl((SPI_FMT_CTRL_OFFSET+zx29spi->virtbase))&(~(0x1<<12));
3426 writel(regval, (SPI_FMT_CTRL_OFFSET+zx29spi->virtbase));
3427 dev_info(&pdev->dev," %s set non-camera mode regval:0x%x \n",pdev->name,regval);
3428 }
3429
3430 writel(readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase)) | SPI_COM_CTRL_MASK_SSPE, (SPI_COM_CTRL_OFFSET+zx29spi->virtbase));
3431 while(((readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase))>> 4)&0x1) == 0);
3432
3433 dev_info(&pdev->dev,"ssp enabled \n",regval);
3434 /* irq*/
3435 status = zx29_spi_init_irq(pdev,zx29spi);
3436 if(status != 0) {
3437 dev_err(&pdev->dev, "zx29_spi_init_irq err!!! \n");
3438 goto err_no_irq;
3439 }
3440
3441 /* Get DMA channels */
3442 if (platform_info->enable_dma) {
3443 status = zx29_dma_probe(zx29spi);
3444 if (status != 0) {
3445 platform_info->enable_dma = 0;
3446 sc_debug_info_record(MODULE_ID_CAP_SPI, "%s dma probe failed",pdev->name);
3447 }
3448 }
3449
3450#if SPI_PSM_CONTROL
3451 wake_lock_init(&zx29spi->psm_lock, WAKE_LOCK_SUSPEND, zx29spi->name);
3452#endif
3453 master->dev.of_node = pdev->dev.of_node;
3454 ret = devm_spi_register_master(&pdev->dev, master);
3455 if (ret < 0) {
3456 dev_err(&pdev->dev, "can not register to master err %d\n", ret);
3457 goto err_spi_register;
3458 }
3459
3460 dev_info(&pdev->dev," probe succeeded\n");
3461
3462 /* let runtime pm put suspend */
3463 if (platform_info->autosuspend_delay > 0) {
3464 dev_info(&pdev->dev, "will use autosuspend for runtime pm, delay %dms\n", platform_info->autosuspend_delay);
3465 pm_runtime_set_autosuspend_delay(dev, platform_info->autosuspend_delay);
3466 pm_runtime_use_autosuspend(dev);
3467 pm_runtime_put_autosuspend(dev);
3468 } else {
3469 pm_runtime_put(dev);
3470 }
3471
3472 return 0;
3473
3474 err_spi_register:
3475#if SPI_PSM_CONTROL
3476 wake_lock_destroy(&zx29spi->psm_lock);
3477#endif
3478 if (platform_info->enable_dma)
3479 zx29_dma_remove(zx29spi);
3480
3481 err_no_irq:
3482 clk_disable(zx29spi->spi_clk);
3483// err_no_clk_en:
3484 //clk_unprepare(pl022->clk);
3485 //err_clk_prep:
3486 clk_put(zx29spi->spi_clk);
3487 err_no_clk:
3488// iounmap(zx29spi->virtbase);
3489 err_gpios:
3490 /* add */
3491 err_no_ioremap:
3492 err_no_registers:
3493 spi_master_put(master);
3494 err_no_master:
3495 err_no_pdata:
3496 return status;
3497}
3498
3499
3500
3501static int zx29_spi_probe(struct platform_device *pdev)
3502{
3503 struct device *dev = &pdev->dev;
3504 const struct of_device_id *match;
3505 const struct zx29_ssp_device_of_data *data;
3506 enum zx29_ssp_device_mode mode;
3507 int status = -EINVAL;
3508 match = of_match_device(zx29_spi_of_match, dev);
3509 if (!match)
3510 return -EINVAL;
3511 data = (struct zx29_ssp_device_of_data *)match->data;
3512 mode = (enum zx29_ssp_device_mode)data->mode;
3513
3514 dev_info(&pdev->dev,"%s mode \n", (mode==0)?"MASTER":(mode==1)?"SLAVE":"UNKNOWN");
3515 if(mode == ZX29_SSP_MASTER_TYPE)
3516 status = zx29_spi_probe_of_master(pdev);
3517 if(mode == ZX29_SSP_SLAVE_TYPE)
3518 status = zx29_spi_probe_of_slave(pdev);
3519
3520 return status;
3521}
3522
3523static int __exit zx29_spi_remove(struct platform_device *pdev)
3524{
3525 struct zx29_spi *zx29spi = dev_get_drvdata(&pdev->dev);
3526 struct resource * gpio = NULL;
3527 //struct resource * irq = NULL;
3528 int i;
3529
3530 if (!zx29spi)
3531 return 0;
3532
3533 /*
3534 * undo pm_runtime_put() in probe. I assume that we're not
3535 * accessing the primecell here.
3536 */
3537 pm_runtime_get_noresume(&pdev->dev);
3538
3539 spi_unregister_master(zx29spi->master);
3540
3541 load_spi_default_config(zx29spi);
3542 if (zx29spi->master_info->enable_dma)
3543 zx29_dma_remove(zx29spi);
3544/*
3545 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
3546 if( irq != NULL )
3547 {
3548 free_irq(irq->start, zx29spi);
3549 }
3550*/
3551 devm_free_irq(&pdev->dev, zx29spi->irq, zx29spi);
3552
3553 clk_disable(zx29spi->spi_clk);
3554 clk_put(zx29spi->spi_clk);
3555
3556 clk_disable(zx29spi->pclk);
3557 clk_put(zx29spi->pclk);
3558
3559 #if defined(CONFIG_DEBUG_FS)
3560 if(zx29spi->spi_root){
3561 printk(KERN_INFO "spi:debugfs_remove_recursive \n");
3562 debugfs_remove_recursive(zx29spi->spi_root);
3563 }
3564 #endif
3565
3566
3567 // iounmap(zx29spi->virtbase);
3568 //amba_release_regions(adev);
3569 //tasklet_disable(&zx29spi->pump_transfers);
3570
3571 spi_master_put(zx29spi->master);
3572 //amba_set_drvdata(adev, NULL);
3573 dev_set_drvdata(&pdev->dev, NULL);
3574
3575#if SPI_PSM_CONTROL
3576 wake_lock_destroy(&zx29spi->psm_lock);
3577#endif
3578
3579 return 0;
3580}
3581
3582static const struct zx29_ssp_device_of_data zx29_ssp_master_of_data = {
3583 .mode = ZX29_SSP_MASTER_TYPE,
3584};
3585
3586static const struct zx29_ssp_device_of_data zx29_ssp_slave_of_data = {
3587 .mode = ZX29_SSP_SLAVE_TYPE,
3588};
3589
3590static const struct of_device_id zx29_spi_of_match[] = {
3591 {
3592 .compatible = "zte,zx29_ssp",
3593 .data = &zx29_ssp_master_of_data,
3594 },
3595 {
3596 .compatible = "zte,zx29_ssp_slave",
3597 .data = &zx29_ssp_slave_of_data,
3598 },
3599 {},
3600};
3601MODULE_DEVICE_TABLE(of, zx29_spi_of_match);
3602
3603#ifdef CONFIG_PM
3604static int zx29_spi_suspend(struct platform_device *pdev,pm_message_t state)
3605{
3606 struct zx29_spi *zx29spi = NULL;
3607
3608 if(pdev == NULL)
3609 return -1;
3610 if(pdev && &pdev->dev)
3611 pinctrl_pm_select_sleep_state(&pdev->dev);
3612
3613 zx29spi = (struct zx29_spi *)platform_get_drvdata(pdev);
3614 if(zx29spi && (zx29spi->master->slave == true)) {
3615 if (pinctrl_select_state(ssp_pins[pdev->id].pctrl, ssp_pins[pdev->id].pcs_gpio_sleep) < 0) {
3616 printk("spi%d setting cs_gpio pin ctrl failed\n",pdev->id);
3617 return -1;
3618 }
3619 }
3620 if(zx29spi&&zx29spi->master->slave == false) {
3621 if (pinctrl_select_state(ssp_pins[pdev->id].pctrl, ssp_pins[pdev->id].pcs_gpio_sleep) < 0) {
3622 printk("spi%d setting cs_gpio_sleep pin ctrl failed\n",pdev->id);
3623 return -1;
3624 }
3625 printk("spi%d setting cs_gpio_sleep pin ctrl\n",pdev->id);
3626 }
3627 return 0;
3628}
3629
3630static int zx29_spi_resume(struct platform_device *pdev)
3631{
3632 struct zx29_spi *zx29spi = NULL;
3633
3634 if(pdev == NULL)
3635 return -1;
3636 if(pdev && &pdev->dev)
3637 pinctrl_pm_select_default_state(&pdev->dev);
3638
3639 zx29spi = (struct zx29_spi *)platform_get_drvdata(pdev);
3640 if(zx29spi && (zx29spi->master->slave == true)) {
3641 if (pinctrl_select_state(ssp_pins[pdev->id].pctrl, ssp_pins[pdev->id].pcs_func) < 0) {
3642 printk("spi%d setting cs_func pin ctrl failed\n",pdev->id);
3643 return -1;
3644 }
3645 }
3646 if(zx29spi&&zx29spi->master->slave == false) {
3647 if (pinctrl_select_state(ssp_pins[pdev->id].pctrl, ssp_pins[pdev->id].pcs_gpio_active) < 0) {
3648 printk("spi%d setting cs_gpio_active pin ctrl failed\n",pdev->id);
3649 return -1;
3650 }
3651 printk("spi%d setting cs_gpio_active pin ctrl\n",pdev->id);
3652 gpio_direction_output(ssp_pins[pdev->id].gpio_cs,SPI_GPIO_HIGH);
3653 }
3654 return 0;
3655}
3656#endif
3657
3658static struct platform_driver zx29_spi_driver = {
3659 .driver = {
3660 .name = "zx29_ssp",
3661 .of_match_table = of_match_ptr(zx29_spi_of_match),
3662 .owner = THIS_MODULE,
3663 },
3664 .probe = zx29_spi_probe,
3665 #ifdef CONFIG_PM
3666 .suspend = zx29_spi_suspend,
3667 .resume = zx29_spi_resume,
3668 #endif
3669 .remove = __exit_p(zx29_spi_remove),
3670};
3671
3672static int __init zx29_spi_init(void)
3673{
3674 return platform_driver_register(&zx29_spi_driver);
3675}
3676
3677static void __exit zx29_spi_exit(void)
3678{
3679 platform_driver_unregister(&zx29_spi_driver);
3680}
3681
3682module_init(zx29_spi_init);
3683module_exit(zx29_spi_exit);
3684
3685MODULE_DESCRIPTION("zx29 spi controller driver");
3686MODULE_AUTHOR("zte");
3687MODULE_LICENSE("GPL");
3688