blob: 5f19b19a5fe7d03dc89c703ac592e84eb315b5e7 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
2 * zx297520v2v3 spi controller driver
3 * Author: zhou.tianbao@sanechips.com.cn
4 * from original zx297520v2v3 driver
5 *
6 * Copyright (C) 2016 Sanechips Corporation
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23#include <linux/kernel.h>
24#include <linux/init.h>
25#include <linux/interrupt.h>
26#include <linux/module.h>
27#include <linux/device.h>
28#include <linux/delay.h>
29#include <linux/platform_device.h>
30#include <linux/err.h>
31#include <linux/clk.h>
32#include <linux/io.h>
33#include <linux/gpio.h>
34#include <linux/slab.h>
35#include <linux/dmaengine.h>
36#include <linux/dma-mapping.h>
37#include <linux/scatterlist.h>
38#include <linux/pm_runtime.h>
39#include <linux/semaphore.h>
40#include <linux/wakelock.h>
41
42#include <linux/debugfs.h>
43
44#include <linux/spi/spi.h>
45#include <linux/soc/zte/pm/drv_idle.h>
46
47#include <mach/clk.h>
48#include <mach/spi.h>
49#include <mach/gpio.h>
50#include <mach/dma.h>
51#include <mach/iomap.h>
52
53#include <linux/ramdump/ramdump.h>
54
55#define CONFIG_SPI_DMA_ENGINE
56#define SPI_PSM_CONTROL (1)
57
58/*
59 * This macro is used to define some register default values.
60 * reg is masked with mask, the OR:ed with an (again masked)
61 * val shifted sb steps to the left.
62 */
63#define SPI_WRITE_BITS(reg, val, mask, sb) \
64 ((reg) = (((reg) & ~(mask)) | (((val)<<(sb)) & (mask))))
65
66/*
67 * This macro is also used to define some default values.
68 * It will just shift val by sb steps to the left and mask
69 * the result with mask.
70 */
71#define GEN_MASK_BITS(val, mask, sb) \
72 (((val)<<(sb)) & (mask))
73
74
75#define SPI_GPIO_HIGH 1
76#define SPI_GPIO_LOW 0
77
78#define ZX29_CS_ACTIVE 1 /* normally nCS, active low */
79#define ZX29_CS_INACTIVE 0
80
81#define DRIVE_TX 0
82#define DO_NOT_DRIVE_TX 1
83
84#define DO_NOT_QUEUE_DMA 0
85#define QUEUE_DMA 1
86
87#define RX_TRANSFER BIT(0)
88#define TX_TRANSFER BIT(1)
89
90/* registers */
91/*
92#define SPI_VER_REG(r) (r + 0x00)
93#define SPI_COM_CTRL(r) (r + 0x04)
94#define SPI_FMT_CTRL(r) (r + 0x08)
95#define SPI_DR(r) (r + 0x0C)
96#define SPI_FIFO_CTRL(r) (r + 0x10)
97#define SPI_FIFO_SR(r) (r + 0x14)
98#define SPI_INTR_EN(r) (r + 0x18)
99#define SPI_INTR_SR_SCLR(r) (r + 0x1C)
100#define SPI_TIMING_SCLR(r) (r + 0x20)
101*/
102#define SPI_VER_REG_OFFSET (0x00)
103#define SPI_COM_CTRL_OFFSET (0x04)
104#define SPI_FMT_CTRL_OFFSET (0x08)
105#define SPI_DR_OFFSET (0x0C)
106#define SPI_FIFO_CTRL_OFFSET (0x10)
107#define SPI_FIFO_SR_OFFSET (0x14)
108#define SPI_INTR_EN_OFFSET (0x18)
109#define SPI_INTR_SR_OFFSET (0x1C)
110#define SPI_TIMING_OFFSET (0x20)
111
112/*
113 * SPI Version Register - SPI_VER_REG
114 */
115#define SPI_VER_REG_MASK_Y (0xFFUL << 16)
116#define SPI_VER_REG_MASK_X (0xFFUL << 24)
117
118/*
119 * SPI Common Control Register - SPI_COM_CTRL
120 */
121#define SPI_COM_CTRL_MASK_LBM (0x1UL << 0)
122#define SPI_COM_CTRL_MASK_SSPE (0x1UL << 1)
123#define SPI_COM_CTRL_MASK_MS (0x1UL << 2)
124#define SPI_COM_CTRL_MASK_SOD (0x1UL << 3)
125#define SPI_COM_CTRL_MASK_SSPE_BACK (0x1UL << 4)
126
127/*
128 * SPI Format Control Register - SPI_FMT_CTRL
129 */
130#define SPI_FMT_CTRL_MASK_FRF (0x3UL << 0)
131#define SPI_FMT_CTRL_MASK_POL (0x1UL << 2)
132#define SPI_FMT_CTRL_MASK_PHA (0x1UL << 3)
133#define SPI_FMT_CTRL_MASK_DSS (0x1FUL << 4)
134
135/*
136 * SPI FIFO Control Register - SPI_FIFO_CTRL
137 */
138#define SPI_FIFO_CTRL_MASK_RX_DMA_EN (0x1UL << 2)
139#define SPI_FIFO_CTRL_MASK_TX_DMA_EN (0x1UL << 3)
140#define SPI_FIFO_CTRL_MASK_RX_FIFO_THRES (0xFUL << 4)
141#define SPI_FIFO_CTRL_MASK_TX_FIFO_THRES (0xFUL << 8)
142/*
143 * SPI FIFO Status Register - SPI_FIFO_SR
144 */
145
146#define SPI_FIFO_SR_MASK_RX_BEYOND_THRES (0x1UL << 0)
147#define SPI_FIFO_SR_MASK_TX_BEYOND_THRES (0x1UL << 1)
148#define SPI_FIFO_SR_MASK_RX_FIFO_FULL (0x1UL << 2)
149#define SPI_FIFO_SR_MASK_TX_FIFO_EMPTY (0x1UL << 3)
150#define SPI_FIFO_SR_MASK_BUSY (0x1UL << 4)
151#define SPI_FIFO_SR_SHIFT_RX_CNT 5
152#define SPI0_FIFO_SR_MASK_RX_FIFO_CNTR (0x1fUL << SPI_FIFO_SR_SHIFT_RX_CNT)
153#define SPI1_FIFO_SR_MASK_RX_FIFO_CNTR (0x7fUL << SPI_FIFO_SR_SHIFT_RX_CNT)
154#define SPI0_FIFO_SR_SHIFT_TX_CNT 10
155#define SPI0_FIFO_SR_MASK_TX_FIFO_CNTR (0x1fUL << SPI0_FIFO_SR_SHIFT_TX_CNT)
156#define SPI1_FIFO_SR_SHIFT_TX_CNT 12
157#define SPI1_FIFO_SR_MASK_TX_FIFO_CNTR (0x1fUL << SPI1_FIFO_SR_SHIFT_TX_CNT)
158
159/*
160 * SPI Interrupt Enable Register - SPI_INTR_EN
161 */
162#define SPI_INTR_EN_MASK_RX_OVERRUN_IE (0x1UL << 0)
163#define SPI_INTR_EN_MASK_TX_UNDERRUN_IE (0x1UL << 1)
164#define SPI_INTR_EN_MASK_RX_FULL_IE (0x1UL << 2)
165#define SPI_INTR_EN_MASK_TX_EMPTY_IE (0x1UL << 3)
166#define SPI_INTR_EN_MASK_RX_THRES_IE (0x1UL << 4)
167#define SPI_INTR_EN_MASK_TX_THRES_IE (0x1UL << 5)
168
169/*
170 * SPI Interrupt Status Register OR Interrupt Clear Register - SPI_INTR_SR_SCLR
171 */
172
173#define SPI_INTR_SR_SCLR_MASK_RX_OVERRUN_INTR (0x1UL << 0)
174#define SPI_INTR_SR_SCLR_MASK_TX_UNDERRUN_INTR (0x1UL << 1)
175#define SPI_INTR_SR_SCLR_MASK_RX_FULL_INTR (0x1UL << 2)
176#define SPI_INTR_SR_SCLR_MASK_TX_EMPTY_INTR (0x1UL << 3)
177#define SPI_INTR_SR_SCLR_MASK_RX_THRES_INTR (0x1UL << 4)
178#define SPI_INTR_SR_SCLR_MASK_TX_THRES_INTR (0x1UL << 5)
179
180/* SPI WCLK Freqency */
181#define SPI_SPICLK_FREQ_26M (26*1000*1000)
182#define SPI_SPICLK_FREQ_104M (104*1000*1000)
183#define SPI_SPICLK_FREQ_156M (156*1000*1000)
184
185#define CLEAR_ALL_INTERRUPTS 0x3FUL
186#define ENABLE_ALL_INTERRUPTS 0x3FUL
187#define ENABLE_INTERRUPTS 0x03UL
188#define DISABLE_ALL_INTERRUPTS 0x0UL
189/*
190 * Message State
191 * we use the spi_message.state (void *) pointer to
192 * hold a single state value, that's why all this
193 * (void *) casting is done here.
194 */
195
196enum zx29_spi_state {
197 STATE_START,
198 STATE_RUNNING,
199 STATE_DONE,
200 STATE_ERROR
201};
202
203/*
204 * SPI State - Whether Enabled or Disabled
205 */
206#define SPI_DISABLED (0)
207#define SPI_ENABLED (1)
208
209/*
210 * SPI DMA State - Whether DMA Enabled or Disabled
211 */
212#define SPI_DMA_DISABLED (0)
213#define SPI_DMA_ENABLED (1)
214
215/*
216 * SPI SOD State - Whether SOD Enabled or Disabled
217 */
218#define SPI_SOD_DISABLED (1)
219#define SPI_SOD_ENABLED (0)
220
221#if 0
222#define GPIO_AP_SPI0_CS 30
223#define GPIO_AP_SPI0_CLK 31
224#define GPIO_AP_SPI0_RXD 32
225#define GPIO_AP_SPI0_TXD 33
226#define GPIO_AP_SPI1_CS 7
227#define GPIO_AP_SPI1_CLK 8
228#define GPIO_AP_SPI1_RXD 13
229#define GPIO_AP_SPI1_TXD 14
230//#else
231#define GPIO_AP_SPI0_CS ZX29_GPIO_25
232#define GPIO_AP_SPI0_CLK ZX29_GPIO_26
233#define GPIO_AP_SPI0_RXD ZX29_GPIO_27
234#define GPIO_AP_SPI0_TXD ZX29_GPIO_28
235
236
237#define GPIO_AP_SPI0_CS_FUN GPIO25_SSP0_CS
238#define GPIO_AP_SPI0_CLK_FUN GPIO26_SSP0_CLK
239#define GPIO_AP_SPI0_RXD_FUN GPIO27_SSP0_RXD
240#define GPIO_AP_SPI0_TXD_FUN GPIO28_SSP0_TXD
241#endif
242enum spi_fifo_threshold_level {
243 SPI_FIFO_THRES_1,
244 SPI_FIFO_THRES_2,
245 SPI_FIFO_THRES_3,
246 SPI_FIFO_THRES_4,
247 SPI_FIFO_THRES_5,
248 SPI_FIFO_THRES_6,
249 SPI_FIFO_THRES_7,
250 SPI_FIFO_THRES_8,
251 SPI_FIFO_THRES_9,
252 SPI_FIFO_THRES_10,
253 SPI_FIFO_THRES_11,
254 SPI_FIFO_THRES_12,
255 SPI_FIFO_THRES_13,
256 SPI_FIFO_THRES_14,
257 SPI_FIFO_THRES_15,
258 SPI_FIFO_THRES_16
259
260};
261
262/*
263 * SPI Clock Parameter ranges
264 */
265#define DIV_MIN 0x00
266#define DIV_MAX 0x0F
267
268#define SPI_POLLING_TIMEOUT 1000
269
270/*
271 * The type of reading going on on this chip
272 */
273enum spi_reading {
274 READING_NULL,
275 READING_U8,
276 READING_U16,
277 READING_U32
278};
279
280enum spi_writing {
281 WRITING_NULL,
282 WRITING_U8,
283 WRITING_U16,
284 WRITING_U32
285};
286
287
288struct vendor_data {
289 int fifodepth;
290 int max_bpw;
291 bool loopback;
292};
293
294
295struct zx29_spi {
296 char name[16];
297 struct platform_device *pdev;
298 struct vendor_data *vendor;
299 resource_size_t phybase;
300 void __iomem *virtbase;
301 unsigned int irq;
302 struct clk *pclk;/* spi controller work clock */
303 struct clk *spi_clk;/* spi clk line clock */
304 u32 clkfreq;
305 struct spi_master *master;
306 struct zx29_spi_controller *master_info;
307 /* Message per-transfer pump */
308 struct tasklet_struct pump_transfers;
309 struct spi_message *cur_msg;
310 struct spi_transfer *cur_transfer;
311 struct chip_data *cur_chip;
312 bool next_msg_cs_active;
313 void *tx;
314 void *tx_end;
315 void *rx;
316 void *rx_end;
317 enum spi_reading read;
318 enum spi_writing write;
319 u32 exp_fifo_level;
320 enum spi_rx_level_trig rx_lev_trig;
321 enum spi_tx_level_trig tx_lev_trig;
322 /* DMA settings */
323#ifdef CONFIG_SPI_DMA_ENGINE
324 struct dma_chan *dma_rx_channel;
325 struct dma_chan *dma_tx_channel;
326 struct sg_table sgt_rx;
327 struct sg_table sgt_tx;
328 char *dummypage;
329 unsigned int dma_running;
330// struct mutex spi_lock;
331#endif
332#if defined(CONFIG_DEBUG_FS)
333 struct dentry * spi_root;
334 struct debugfs_regset32 spi_regset;
335 u32 spi_poll_cnt;
336 u32 spi_dma_cnt;
337#endif
338#if SPI_PSM_CONTROL
339 struct wake_lock psm_lock;
340#endif
341 struct semaphore sema_dma;
342};
343
344
345struct chip_data {
346 u32 ver_reg;
347 u32 com_ctrl;
348 u32 fmt_ctrl;
349 u32 fifo_ctrl;
350// u32 intr_en;
351 u8 n_bytes;
352 u8 clk_div;/* spi clk divider */
353 bool enable_dma;
354 enum spi_reading read;
355 enum spi_writing write;
356 void (*cs_control) (u32 command);
357 int xfer_type;
358};
359//struct semaphore g_SpiTransferSemaphore;
360
361static struct zx29_spi *g_zx29_spi[2];
362
363#if SPI_PSM_CONTROL
364static volatile unsigned int spi_active_count = 0;
365
366static void zx29_spi_set_active(struct wake_lock *lock)
367{
368 unsigned long flags;
369
370 local_irq_save(flags);
371
372 if(spi_active_count == 0)
373 {
374 zx_cpuidle_set_busy(IDLE_FLAG_SPI);
375 }
376 spi_active_count++;
377
378 local_irq_restore(flags);
379
380 wake_lock(lock);
381}
382
383static void zx29_spi_set_idle(struct wake_lock *lock)
384{
385 unsigned long flags;
386
387 local_irq_save(flags);
388
389 spi_active_count--;
390 if(spi_active_count == 0)
391 {
392 zx_cpuidle_set_free(IDLE_FLAG_SPI);
393 }
394
395 local_irq_restore(flags);
396
397 wake_unlock(lock);
398}
399#endif
400
401static int zx29_do_interrupt_dma_transfer(struct zx29_spi *zx29spi);
402/**
403 * default_cs_control - Dummy chip select function
404 * @command: select/delect the chip
405 *
406 * If no chip select function is provided by client this is used as dummy
407 * chip select
408 */
409static void default_cs0_control(u32 command)
410{
411 gpio_set_value(GPIO_AP_SPI0_CS, !command);
412}
413
414#ifdef CONFIG_ARCH_ZX297520V3
415static void default_cs1_control(u32 command)
416{
417 gpio_set_value(GPIO_AP_SPI1_CS, !command);
418}
419#endif
420
421
422static int flush(struct zx29_spi *zx29spi)
423{
424 unsigned long limit = loops_per_jiffy << 1;
425 uint32_t rx_fifo_cnt_msk = (zx29spi->pdev->id == 1) ?
426 SPI1_FIFO_SR_MASK_RX_FIFO_CNTR :
427 SPI0_FIFO_SR_MASK_RX_FIFO_CNTR;
428
429 dev_dbg(&zx29spi->pdev->dev, "flush\n");
430 /* Flushing FIFO by software cannot clear RX DMA Request. */
431 do {
432 while (readl((SPI_FIFO_SR_OFFSET+zx29spi->virtbase)) & rx_fifo_cnt_msk)
433 readl((SPI_DR_OFFSET+zx29spi->virtbase));
434 } while ((readl((SPI_FIFO_SR_OFFSET+zx29spi->virtbase)) & SPI_FIFO_SR_MASK_BUSY) && limit--);
435
436 zx29spi->exp_fifo_level = 0;
437
438 return limit;
439}
440
441
442static void restore_state(struct zx29_spi *zx29spi)
443{
444 struct chip_data *chip = zx29spi->cur_chip;
445
446 /* disable all interrupts */
447 writel(ENABLE_INTERRUPTS, (SPI_INTR_EN_OFFSET+zx29spi->virtbase));
448 writel(CLEAR_ALL_INTERRUPTS, (SPI_INTR_SR_OFFSET+zx29spi->virtbase));
449
450 writel(chip->fmt_ctrl, (SPI_FMT_CTRL_OFFSET+zx29spi->virtbase));
451 writel(chip->fifo_ctrl, (SPI_FIFO_CTRL_OFFSET+zx29spi->virtbase));
452// writel(chip->intr_en, SPI_INTR_EN(zx297520v2spi->virtbase));
453 writel(chip->com_ctrl, (zx29spi->virtbase + SPI_COM_CTRL_OFFSET));
454}
455
456/*
457 * Default spi Register Values
458 */
459#define DEFAULT_SPI_COM_CTRL ( \
460 GEN_MASK_BITS(LOOPBACK_DISABLED, SPI_COM_CTRL_MASK_LBM, 0) | \
461 GEN_MASK_BITS(SPI_DISABLED, SPI_COM_CTRL_MASK_SSPE, 1) | \
462 GEN_MASK_BITS(SPI_MASTER, SPI_COM_CTRL_MASK_MS, 2) \
463)
464
465#define DEFAULT_SPI_FMT_CTRL ( \
466 GEN_MASK_BITS(SPI_INTERFACE_MOTOROLA_SPI, SPI_FMT_CTRL_MASK_FRF, 0) | \
467 GEN_MASK_BITS(SPI_CLK_POL_IDLE_LOW, SPI_FMT_CTRL_MASK_POL, 2) | \
468 GEN_MASK_BITS(SPI_CLK_FIRST_EDGE, SPI_FMT_CTRL_MASK_PHA, 3) | \
469 GEN_MASK_BITS(SPI_DATA_BITS_8, SPI_FMT_CTRL_MASK_DSS, 4) \
470)
471
472#define DEFAULT_SPI_FIFO_CTRL ( \
473 GEN_MASK_BITS(SPI_DMA_DISABLED, SPI_FIFO_CTRL_MASK_RX_DMA_EN, 2) | \
474 GEN_MASK_BITS(SPI_DMA_DISABLED, SPI_FIFO_CTRL_MASK_TX_DMA_EN, 3) | \
475 GEN_MASK_BITS(SPI_FIFO_THRES_8, SPI_FIFO_CTRL_MASK_RX_FIFO_THRES, 4) | \
476 GEN_MASK_BITS(SPI_FIFO_THRES_8, SPI_FIFO_CTRL_MASK_TX_FIFO_THRES, 8) \
477)
478
479
480
481static void load_spi_default_config(struct zx29_spi *zx29spi)
482{
483 writel(CLEAR_ALL_INTERRUPTS, (SPI_INTR_SR_OFFSET+zx29spi->virtbase));
484 writel(ENABLE_INTERRUPTS, (SPI_INTR_EN_OFFSET+zx29spi->virtbase));
485
486 writel(DEFAULT_SPI_FMT_CTRL, (SPI_FMT_CTRL_OFFSET+zx29spi->virtbase));
487 writel(DEFAULT_SPI_FIFO_CTRL, (SPI_FIFO_CTRL_OFFSET+zx29spi->virtbase));
488 writel(DEFAULT_SPI_COM_CTRL, (SPI_COM_CTRL_OFFSET+zx29spi->virtbase));
489}
490
491
492
493static void readwriter(struct zx29_spi *zx29spi)
494{
495 uint32_t fifo_sr;
496 uint32_t rd_max, wr_max;
497 uint32_t rx_fifo_cnt_msk;
498 uint32_t tx_fifo_cnt_msk;
499 uint32_t tx_fifo_cnt_pos;
500
501 if (zx29spi->pdev->id == 1) {
502 rx_fifo_cnt_msk = SPI1_FIFO_SR_MASK_RX_FIFO_CNTR;
503 tx_fifo_cnt_msk = SPI1_FIFO_SR_MASK_TX_FIFO_CNTR;
504 tx_fifo_cnt_pos = SPI1_FIFO_SR_SHIFT_TX_CNT;
505 } else {
506 rx_fifo_cnt_msk = SPI0_FIFO_SR_MASK_RX_FIFO_CNTR;
507 tx_fifo_cnt_msk = SPI0_FIFO_SR_MASK_TX_FIFO_CNTR;
508 tx_fifo_cnt_pos = SPI0_FIFO_SR_SHIFT_TX_CNT;
509 }
510
511 /*
512 * The FIFO depth is different between primecell variants.
513 * I believe filling in too much in the FIFO might cause
514 * errons in 8bit wide transfers on ARM variants (just 8 words
515 * FIFO, means only 8x8 = 64 bits in FIFO) at least.
516 *
517 * To prevent this issue, the TX FIFO is only filled to the
518 * unused RX FIFO fill length, regardless of what the TX
519 * FIFO status flag indicates.
520 */
521 //printk("[yuwei]%s, rx: %p, rxend: %p, tx: %p, txend: %p\n",
522 // __func__, zx29spi->rx, zx29spi->rx_end, zx29spi->tx, zx29spi->tx_end);
523
524 fifo_sr = readl((SPI_FIFO_SR_OFFSET+zx29spi->virtbase));
525 rd_max = (fifo_sr & rx_fifo_cnt_msk) >> SPI_FIFO_SR_SHIFT_RX_CNT;
526 wr_max = (fifo_sr & tx_fifo_cnt_msk) >> tx_fifo_cnt_pos;
527 if ((fifo_sr & SPI_FIFO_SR_MASK_BUSY) && wr_max) {
528 wr_max--;
529 }
530
531 //read rx fifo to empty first
532 while ((zx29spi->rx < zx29spi->rx_end) && rd_max--) {
533 switch (zx29spi->read) {
534 case READING_NULL:
535 readw((SPI_DR_OFFSET+zx29spi->virtbase));
536 break;
537 case READING_U8:
538 *(u8 *) (zx29spi->rx) =
539 readw((SPI_DR_OFFSET+zx29spi->virtbase)) & 0xFFU;
540 break;
541 case READING_U16:
542 *(u16 *) (zx29spi->rx) =
543 (u16) readw((SPI_DR_OFFSET+zx29spi->virtbase));
544 break;
545 case READING_U32:
546 *(u32 *) (zx29spi->rx) =
547 readl((SPI_DR_OFFSET+zx29spi->virtbase));
548 break;
549 }
550 zx29spi->rx += (zx29spi->cur_chip->n_bytes);
551 zx29spi->exp_fifo_level--;
552 }
553
554 //write
555 while ((zx29spi->tx < zx29spi->tx_end) && wr_max--) {
556 switch (zx29spi->write) {
557 case WRITING_NULL:
558 writew(0x0, (SPI_DR_OFFSET+zx29spi->virtbase));
559 break;
560 case WRITING_U8:
561 writew(*(u8 *) (zx29spi->tx), (SPI_DR_OFFSET+zx29spi->virtbase));
562 break;
563 case WRITING_U16:
564 writew((*(u16 *) (zx29spi->tx)), (SPI_DR_OFFSET+zx29spi->virtbase));
565 break;
566 case WRITING_U32:
567 writel(*(u32 *) (zx29spi->tx), (SPI_DR_OFFSET+zx29spi->virtbase));
568 break;
569 }
570 zx29spi->tx += (zx29spi->cur_chip->n_bytes);
571 zx29spi->exp_fifo_level++;
572 }
573
574 cpu_relax();
575
576 /*
577 * When we exit here the TX FIFO should be full and the RX FIFO
578 * should be empty
579 */
580}
581
582/*
583 * This DMA functionality is only compiled in if we have
584 * access to the generic DMA devices/DMA engine.
585 */
586#ifdef CONFIG_SPI_DMA_ENGINE
587
588static void zx29_fill_txfifo(struct zx29_spi *zx29spi)
589{
590 uint32_t fifo_sr;
591 int32_t rd_max, wr_max;
592 uint32_t rx_fifo_cnt_msk;
593 uint32_t tx_fifo_cnt_msk;
594 uint32_t tx_fifo_cnt_pos;
595
596 if (zx29spi->pdev->id == 1) {
597 rx_fifo_cnt_msk = SPI1_FIFO_SR_MASK_RX_FIFO_CNTR;
598 tx_fifo_cnt_msk = SPI1_FIFO_SR_MASK_TX_FIFO_CNTR;
599 tx_fifo_cnt_pos = SPI1_FIFO_SR_SHIFT_TX_CNT;
600 } else {
601 rx_fifo_cnt_msk = SPI0_FIFO_SR_MASK_RX_FIFO_CNTR;
602 tx_fifo_cnt_msk = SPI0_FIFO_SR_MASK_TX_FIFO_CNTR;
603 tx_fifo_cnt_pos = SPI0_FIFO_SR_SHIFT_TX_CNT;
604 }
605
606 while (zx29spi->tx < zx29spi->tx_end) {
607 fifo_sr = readl((SPI_FIFO_SR_OFFSET+zx29spi->virtbase));
608 rd_max = (fifo_sr & rx_fifo_cnt_msk) >> SPI_FIFO_SR_SHIFT_RX_CNT;
609 wr_max = (fifo_sr & tx_fifo_cnt_msk) >> tx_fifo_cnt_pos;
610 if (fifo_sr & SPI_FIFO_SR_MASK_BUSY) {
611 wr_max--;
612 }
613 wr_max -= rd_max;
614 wr_max = (wr_max > 0) ? wr_max : 0;
615
616 //write
617 while ((zx29spi->tx < zx29spi->tx_end) && wr_max--) {
618 writew(0x0, (SPI_DR_OFFSET+zx29spi->virtbase));
619 zx29spi->tx += (zx29spi->cur_chip->n_bytes);
620 }
621
622 cpu_relax();
623 }
624}
625
626static void dma_callback(void *data)
627{
628 struct zx29_spi *zx29spi = (struct zx29_spi *)data;
629 //printk(KERN_INFO "spi:dma transfer complete\n");//YXY
630 up(&zx29spi->sema_dma);
631}
632
633/*
634static void dma_callback_tx(void *data)
635{
636 struct zx29_spi *zx29spi = (struct zx29_spi *)data;
637 // printk(KERN_INFO "spi:dma transfer complete tx\n");//YXY
638 printk("[yuwei]%s",__func__);
639 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)));
640
641 //up(&g_SpiTransferSemaphore);
642}
643*/
644
645/**
646 * configure_dma - configures the channels for the next transfer
647 */
648static int configure_dma(struct zx29_spi *zx29spi)
649{
650// unsigned int pages;
651// int ret;
652// int rx_sglen, tx_sglen;
653 dma_channel_def rx_conf;
654 dma_channel_def tx_conf;
655 struct dma_chan *rxchan = zx29spi->dma_rx_channel;
656 struct dma_chan *txchan = zx29spi->dma_tx_channel;
657 struct dma_async_tx_descriptor *rxdesc;
658 struct dma_async_tx_descriptor *txdesc;
659 struct spi_transfer *transfer = zx29spi->cur_transfer;
660
661
662 rx_conf.src_addr = (SPI_DR_OFFSET+zx29spi->phybase);
663 rx_conf.dest_addr = (unsigned int)zx29spi->rx;
664 rx_conf.dma_control.tran_mode = TRAN_PERI_TO_MEM;
665 rx_conf.dma_control.irq_mode = DMA_ALL_IRQ_ENABLE;
666 rx_conf.link_addr = 0;
667
668 tx_conf.src_addr = (unsigned int)zx29spi->tx;
669 tx_conf.dest_addr = (SPI_DR_OFFSET+zx29spi->phybase);
670 tx_conf.dma_control.tran_mode = TRAN_MEM_TO_PERI;
671 tx_conf.dma_control.irq_mode = DMA_ALL_IRQ_ENABLE;
672 tx_conf.link_addr = 0;
673
674
675 /* Check that the channels are available */
676 if (!rxchan || !txchan)
677 return -ENODEV;
678
679 /*
680 * If supplied, the DMA burstsize should equal the FIFO trigger level.
681 * Notice that the DMA engine uses one-to-one mapping. Since we can
682 * not trigger on 2 elements this needs explicit mapping rather than
683 * calculation.
684 */
685 switch (zx29spi->rx_lev_trig) {
686 case SPI_RX_1_OR_MORE_ELEM:
687 rx_conf.dma_control.src_burst_len = DMA_BURST_LEN_1;
688 rx_conf.dma_control.dest_burst_len = DMA_BURST_LEN_1;
689 break;
690 case SPI_RX_4_OR_MORE_ELEM:
691 rx_conf.dma_control.src_burst_len = DMA_BURST_LEN_4;
692 rx_conf.dma_control.dest_burst_len = DMA_BURST_LEN_4;
693 break;
694 case SPI_RX_8_OR_MORE_ELEM:
695 rx_conf.dma_control.src_burst_len = DMA_BURST_LEN_8;
696 rx_conf.dma_control.dest_burst_len = DMA_BURST_LEN_8;
697 break;
698 case SPI_RX_16_OR_MORE_ELEM:
699 rx_conf.dma_control.src_burst_len = DMA_BURST_LEN_16;
700 rx_conf.dma_control.dest_burst_len = DMA_BURST_LEN_16;
701 break;
702 case SPI_RX_32_OR_MORE_ELEM:
703 rx_conf.dma_control.src_burst_len = DMA_BURST_LEN_ALL;
704 rx_conf.dma_control.dest_burst_len = DMA_BURST_LEN_ALL;
705 break;
706 default:
707 rx_conf.dma_control.src_burst_len = zx29spi->vendor->fifodepth >> 1;
708 rx_conf.dma_control.dest_burst_len = zx29spi->vendor->fifodepth >> 1;
709 break;
710 }
711
712 switch (zx29spi->tx_lev_trig) {
713 case SPI_TX_1_OR_MORE_EMPTY_LOC:
714 tx_conf.dma_control.src_burst_len = DMA_BURST_LEN_1;
715 tx_conf.dma_control.dest_burst_len = DMA_BURST_LEN_1;
716 break;
717 case SPI_TX_4_OR_MORE_EMPTY_LOC:
718 tx_conf.dma_control.src_burst_len = DMA_BURST_LEN_4;
719 tx_conf.dma_control.dest_burst_len = DMA_BURST_LEN_4;
720 break;
721 case SPI_TX_8_OR_MORE_EMPTY_LOC:
722 tx_conf.dma_control.src_burst_len = DMA_BURST_LEN_8;
723 tx_conf.dma_control.dest_burst_len = DMA_BURST_LEN_8;
724 break;
725 case SPI_TX_16_OR_MORE_EMPTY_LOC:
726 tx_conf.dma_control.src_burst_len = DMA_BURST_LEN_16;
727 tx_conf.dma_control.dest_burst_len = DMA_BURST_LEN_16;
728 break;
729 case SPI_TX_32_OR_MORE_EMPTY_LOC:
730 tx_conf.dma_control.src_burst_len = DMA_BURST_LEN_ALL;
731 tx_conf.dma_control.dest_burst_len = DMA_BURST_LEN_ALL;
732 break;
733 default:
734 tx_conf.dma_control.src_burst_len = zx29spi->vendor->fifodepth >> 1;
735 tx_conf.dma_control.dest_burst_len = zx29spi->vendor->fifodepth >> 1;
736 break;
737 }
738
739 switch (zx29spi->read) {
740 case READING_NULL:
741 /* Use the same as for writing */
742 rx_conf.dma_control.src_burst_size = DMA_BURST_SIZE_8BIT;
743 rx_conf.dma_control.dest_burst_size = DMA_BURST_SIZE_8BIT;
744 rx_conf.count = zx29spi->cur_transfer->len;
745 break;
746 case READING_U8:
747 rx_conf.dma_control.src_burst_size = DMA_BURST_SIZE_8BIT;
748 rx_conf.dma_control.dest_burst_size = DMA_BURST_SIZE_8BIT;
749 rx_conf.count = zx29spi->cur_transfer->len;
750 break;
751 case READING_U16:
752 rx_conf.dma_control.src_burst_size = DMA_BURST_SIZE_16BIT;
753 rx_conf.dma_control.dest_burst_size = DMA_BURST_SIZE_16BIT;
754 rx_conf.count = zx29spi->cur_transfer->len;
755 break;
756 case READING_U32:
757 rx_conf.dma_control.src_burst_size = DMA_BURST_SIZE_32BIT;
758 rx_conf.dma_control.dest_burst_size = DMA_BURST_SIZE_32BIT;
759 rx_conf.count = zx29spi->cur_transfer->len;
760 break;
761 }
762
763 switch (zx29spi->write) {
764 case WRITING_NULL:
765 /* Use the same as for reading */
766 tx_conf.dma_control.src_burst_size = DMA_BURST_SIZE_8BIT;
767 tx_conf.dma_control.dest_burst_size = DMA_BURST_SIZE_8BIT;
768 tx_conf.count = zx29spi->cur_transfer->len;
769 break;
770 case WRITING_U8:
771 tx_conf.dma_control.src_burst_size = DMA_BURST_SIZE_8BIT;
772 tx_conf.dma_control.dest_burst_size = DMA_BURST_SIZE_8BIT;
773 tx_conf.count = zx29spi->cur_transfer->len;
774 break;
775 case WRITING_U16:
776 tx_conf.dma_control.src_burst_size = DMA_BURST_SIZE_16BIT;
777 tx_conf.dma_control.dest_burst_size = DMA_BURST_SIZE_16BIT;
778 tx_conf.count = zx29spi->cur_transfer->len;
779 break;
780 case WRITING_U32:
781 tx_conf.dma_control.src_burst_size = DMA_BURST_SIZE_32BIT;
782 tx_conf.dma_control.dest_burst_size = DMA_BURST_SIZE_32BIT;
783 tx_conf.count = zx29spi->cur_transfer->len;
784 break;
785 }
786
787 dmaengine_slave_config(rxchan,(struct dma_slave_config*)&rx_conf);
788 dmaengine_slave_config(txchan,(struct dma_slave_config*)&tx_conf);
789
790 /* Submit and fire RX and TX with TX last so we're ready to read! */
791 if (zx29spi->rx) {
792 //printk("[yuwei]%s,tx=%p,rx=%p,len=%d\n",__func__,zx29spi->tx,zx29spi->rx,zx29spi->cur_transfer->len);
793 //printk("[yuwei]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);
794 //printk("[yuwei]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);
795
796 rxdesc= rxchan->device->device_prep_interleaved_dma(rxchan,NULL,0);
797 txdesc= txchan->device->device_prep_interleaved_dma(txchan,NULL,0);
798 /* Put the callback on the RX transfer only, that should finish last */
799 rxdesc->callback = dma_callback;
800 rxdesc->callback_param = zx29spi;
801 // txdesc->callback = dma_callback_tx;
802 // txdesc->callback_param = zx29spi;
803
804 dmaengine_submit(rxdesc);
805 dma_async_issue_pending(rxchan);
806 if (transfer->tx_dma) {
807 /* SPI RX buffer may overflow in DMA busy situation. */
808 dmaengine_submit(txdesc);
809 dma_async_issue_pending(txchan);
810 zx29spi->dma_running = TX_TRANSFER | RX_TRANSFER;
811 enable_irq(zx29spi->irq); /* detect overflow through interrupt */
812 } else {
813 zx29_fill_txfifo(zx29spi);
814 zx29spi->dma_running = RX_TRANSFER;
815 }
816 }
817 else if (zx29spi->tx){
818 txdesc = txchan->device->device_prep_interleaved_dma(txchan,NULL,0);
819 txdesc->callback = dma_callback;
820 txdesc->callback_param = zx29spi;
821 dmaengine_submit(txdesc);
822 dma_async_issue_pending(txchan);
823 zx29spi->dma_running = TX_TRANSFER;
824 }
825
826 return 0;
827}
828
829extern bool zx29_dma_filter_fn(struct dma_chan *chan, void *param);
830static int __devinit zx29_dma_probe(struct zx29_spi *zx29spi)
831{
832 dma_cap_mask_t mask;
833
834 /* Try to acquire a generic DMA engine slave channel */
835 dma_cap_zero(mask);
836 dma_cap_set(DMA_SLAVE, mask);
837 /*
838 * We need both RX and TX channels to do DMA, else do none
839 * of them.
840 */
841 zx29spi->dma_rx_channel = dma_request_channel(mask,
842 zx29_dma_filter_fn,
843 zx29spi->master_info->dma_rx_param);
844 if (!zx29spi->dma_rx_channel) {
845 dev_dbg(&zx29spi->pdev->dev, "no RX DMA channel!\n");
846 goto err_no_rxchan;
847 }
848
849 zx29spi->dma_tx_channel = dma_request_channel(mask,
850 zx29_dma_filter_fn,
851 zx29spi->master_info->dma_tx_param);
852 if (!zx29spi->dma_tx_channel) {
853 dev_dbg(&zx29spi->pdev->dev, "no TX DMA channel!\n");
854 goto err_no_txchan;
855 }
856
857 zx29spi->dummypage = kmalloc(PAGE_SIZE, GFP_KERNEL);
858 if (!zx29spi->dummypage) {
859 dev_dbg(&zx29spi->pdev->dev, "no DMA dummypage!\n");
860 goto err_no_dummypage;
861 }
862
863 dev_info(&zx29spi->pdev->dev, "setup for DMA on RX %s, TX %s\n",
864 dma_chan_name(zx29spi->dma_rx_channel),
865 dma_chan_name(zx29spi->dma_tx_channel));
866
867 return 0;
868
869err_no_dummypage:
870 dma_release_channel(zx29spi->dma_tx_channel);
871err_no_txchan:
872 dma_release_channel(zx29spi->dma_rx_channel);
873 zx29spi->dma_rx_channel = NULL;
874err_no_rxchan:
875 dev_err(&zx29spi->pdev->dev,
876 "Failed to work in dma mode, work without dma!\n");
877 return -ENODEV;
878}
879
880static void terminate_dma(struct zx29_spi *zx29spi)
881{
882 struct dma_chan *rxchan = zx29spi->dma_rx_channel;
883 struct dma_chan *txchan = zx29spi->dma_tx_channel;
884
885 dmaengine_terminate_all(rxchan);
886 dmaengine_terminate_all(txchan);
887// unmap_free_dma_scatter(zx29spi);
888 zx29spi->dma_running = 0;
889}
890
891static void zx29_dma_remove(struct zx29_spi *zx29spi)
892{
893 if (zx29spi->dma_running)
894 terminate_dma(zx29spi);
895 if (zx29spi->dma_tx_channel)
896 dma_release_channel(zx29spi->dma_tx_channel);
897 if (zx29spi->dma_rx_channel)
898 dma_release_channel(zx29spi->dma_rx_channel);
899 kfree(zx29spi->dummypage);
900}
901
902#endif
903
904static irqreturn_t zx29_spi_irq(int irqno, void *dev_id)
905{
906 struct zx29_spi *zx29spi = dev_id;
907
908 disable_irq_nosync(zx29spi->irq);
909 up(&zx29spi->sema_dma);
910
911 //pr_info("spi_irq %X-%X\n", zx29spi->dma_running, readl((SPI_INTR_SR_OFFSET+zx29spi->virtbase)));
912 return IRQ_HANDLED;
913}
914
915static int zx29_do_interrupt_dma_transfer(struct zx29_spi *zx29spi)
916{
917 u32 irqflags = ENABLE_ALL_INTERRUPTS;
918 struct spi_transfer *transfer = zx29spi->cur_transfer;
919 int ret = 0;
920
921 if((void *)transfer->tx_dma != NULL){
922 zx29spi->tx = (void *)transfer->tx_dma;
923 zx29spi->tx_end = zx29spi->tx + zx29spi->cur_transfer->len;
924 }
925 if((void *)transfer->rx_dma != NULL){
926 zx29spi->rx = (void *)transfer->rx_dma;
927 zx29spi->rx_end = zx29spi->rx + zx29spi->cur_transfer->len;
928
929 /*if tx is null, use rx buffer as a dummy tx buffer.*/
930 if((void *)transfer->tx_dma == NULL){
931 zx29spi->tx = (void *)transfer->rx_dma;
932 zx29spi->tx_end = zx29spi->tx + zx29spi->cur_transfer->len;
933 }
934 }
935
936 zx29spi->write = zx29spi->tx ? zx29spi->cur_chip->write : WRITING_NULL;
937 zx29spi->read = zx29spi->rx ? zx29spi->cur_chip->read : READING_NULL;
938
939 /* If we're using DMA, set up DMA here */
940 if (zx29spi->cur_chip->enable_dma) {
941 /* Configure DMA transfer */
942 ret = configure_dma(zx29spi);
943 if (ret) {
944 dev_err(&zx29spi->pdev->dev, "configuration of DMA failed, fall back to interrupt mode\n");
945 goto err_config_dma;
946 }
947 /* Disable interrupts in DMA mode, IRQ from DMA controller */
948 irqflags = DISABLE_ALL_INTERRUPTS;
949 }
950
951 /* config interrupts */
952 /* writel(irqflags, (SPI_INTR_EN_OFFSET+zx29spi->virtbase)); //spi interrupt mode is not supported. */
953
954 /* Enable SSP, turn on interrupts */
955// writel(readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase)) | SPI_COM_CTRL_MASK_SSPE, (SPI_COM_CTRL_OFFSET+zx29spi->virtbase));
956
957 if (zx29spi->cur_chip->enable_dma)
958 {
959 ret = down_timeout(&zx29spi->sema_dma, msecs_to_jiffies(1500));
960 //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)));
961 if (ret < 0) {
962 panic("spi transfer timeout\n");
963 }
964
965 while (readl((SPI_FIFO_SR_OFFSET+zx29spi->virtbase)) & SPI_FIFO_SR_MASK_BUSY)
966 {
967 cpu_relax();
968 }
969
970 if (zx29spi->dma_running == (TX_TRANSFER | RX_TRANSFER)) {
971 u32 intr_status;
972 intr_status = readl((SPI_INTR_SR_OFFSET+zx29spi->virtbase));
973 if (intr_status & SPI_INTR_SR_SCLR_MASK_RX_OVERRUN_INTR) {
974 terminate_dma(zx29spi);
975 dev_err(&zx29spi->cur_msg->spi->dev, "spi rx fifo overflow status = %X!!\n", intr_status);
976 ret = -EIO;
977 } else
978 disable_irq_nosync(zx29spi->irq);
979 }
980 zx29spi->dma_running = 0;
981 }
982
983err_config_dma:
984 if(ret)
985 {
986 dev_err(&zx29spi->pdev->dev, "down_interruptible, ret=%d\n",ret);
987 }
988// writel(readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase)) & ~ SPI_COM_CTRL_MASK_SSPE, (SPI_COM_CTRL_OFFSET+zx29spi->virtbase));
989 return ret;
990}
991
992
993static int zx29_do_polling_transfer(struct zx29_spi *zx29spi)
994{
995 struct spi_transfer *transfer = zx29spi->cur_transfer;
996/* unsigned long time, timeout; */
997 int ret = 0;
998
999 //printk("[yuwei]%s,tx=%p,tx_dma=%p,rx=%p,rx_dma=%p,len=%d\n",__func__,transfer->tx_buf,transfer->tx_dma,transfer->rx_buf,transfer->rx_dma,transfer->len);
1000
1001
1002 /* Flush FIFOs and enable SSP */
1003 //flush(zx29spi);
1004 //writel((readl(SSP_CR1(zx297520v27502ssp->virtbase)) | SSP_CR1_MASK_SSE),
1005 // SSP_CR1(zx297520v27502ssp->virtbase));
1006/*
1007 while (readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase)) & SPI_COM_CTRL_MASK_SSPE_BACK);
1008 writel(readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase)) | SPI_COM_CTRL_MASK_SSPE, (SPI_COM_CTRL_OFFSET+zx29spi->virtbase));
1009*/
1010 dev_dbg(&zx29spi->pdev->dev, "polling transfer ongoing ...\n");
1011
1012 /* timeout = jiffies + msecs_to_jiffies(SPI_POLLING_TIMEOUT); */
1013
1014 if (!zx29spi->tx && !zx29spi->rx) {
1015 return ret;
1016 }
1017
1018 /*read and write*/
1019 while ((zx29spi->tx < zx29spi->tx_end) || (zx29spi->rx < zx29spi->rx_end)) {
1020 readwriter(zx29spi);
1021#if 0
1022 time = jiffies;
1023 if (time_after(time, timeout)) {
1024 dev_warn(&zx29spi->pdev->dev, "%s: read write timeout!\n", __func__);
1025 ret = -EIO;
1026 goto out;
1027 }
1028#endif
1029 }
1030 while (readl((SPI_FIFO_SR_OFFSET+zx29spi->virtbase)) & SPI_FIFO_SR_MASK_BUSY) {
1031 cpu_relax();
1032#if 0
1033 time = jiffies;
1034 if (time_after(time, timeout)) {
1035 dev_warn(&zx29spi->pdev->dev, "%s: wait busy timeout!\n", __func__);
1036 ret = -EIO;
1037 goto out;
1038 }
1039#endif
1040 }
1041
1042out:
1043/*
1044 while (~ readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase)) & SPI_COM_CTRL_MASK_SSPE_BACK);
1045 writel(readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase)) & ~ SPI_COM_CTRL_MASK_SSPE, (SPI_COM_CTRL_OFFSET+zx29spi->virtbase));
1046*/
1047 return ret;
1048}
1049
1050static int zx29_spi_map_mssg(struct zx29_spi *zx29spi,
1051 struct spi_message *msg)
1052{
1053 struct device *dev = &msg->spi->dev;
1054 struct spi_transfer *transfer;
1055 int ret = 0;
1056
1057 if (msg->is_dma_mapped)
1058 return 0;
1059
1060 /* Map until end or first fail */
1061 list_for_each_entry(transfer, &msg->transfers, transfer_list) {
1062
1063 if(!virt_addr_valid(transfer->tx_buf)){
1064 transfer->tx_dma = (dma_addr_t)transfer->tx_buf;
1065 transfer->tx_buf = 0;
1066 }
1067
1068 if(!virt_addr_valid(transfer->rx_buf)){
1069 transfer->rx_dma = (dma_addr_t)transfer->rx_buf;
1070 transfer->rx_buf = 0;
1071 }
1072
1073 if (transfer->len <= zx29spi->vendor->fifodepth || transfer->tx_dma || transfer->rx_dma )
1074 continue;
1075
1076 if (transfer->tx_buf != NULL) {
1077 transfer->tx_dma = dma_map_single(dev,(void *)transfer->tx_buf, transfer->len, DMA_TO_DEVICE);
1078 if (dma_mapping_error(dev, transfer->tx_dma)) {
1079 dev_err(dev, "dma_map_single spi Tx failed\n");
1080 transfer->tx_dma = 0;
1081 ret |= -ENOMEM;
1082 }
1083 }
1084
1085 if (transfer->rx_buf != NULL) {
1086 transfer->rx_dma = dma_map_single(dev, transfer->rx_buf, transfer->len, DMA_FROM_DEVICE);
1087 if (dma_mapping_error(dev, transfer->rx_dma)) {
1088 dev_err(dev, "dma_map_single spi Rx failed\n");
1089 transfer->rx_dma = 0;
1090 ret |= -ENOMEM;
1091 }
1092
1093 if (!transfer->rx_dma && transfer->tx_dma && transfer->tx_buf) {
1094 dma_unmap_single(dev, transfer->tx_dma, transfer->len, DMA_TO_DEVICE);
1095 transfer->tx_dma = 0;
1096 }
1097 }
1098 }
1099
1100 return ret;
1101}
1102
1103static void zx29_spi_unmap_mssg(struct zx29_spi *zx29spi,
1104 struct spi_message *msg)
1105{
1106 struct device *dev = &msg->spi->dev;
1107 struct spi_transfer *transfer;
1108
1109 if (msg->is_dma_mapped)
1110 return;
1111
1112 list_for_each_entry(transfer, &msg->transfers, transfer_list) {
1113
1114 if ( (!transfer->tx_buf && transfer->tx_dma) || (! transfer->rx_buf && transfer->rx_dma) )
1115 continue;
1116
1117 if (transfer->rx_buf != NULL && transfer->rx_dma)
1118 dma_unmap_single(dev, transfer->rx_dma, transfer->len, DMA_FROM_DEVICE);
1119
1120 if (transfer->tx_buf != NULL && transfer->tx_dma)
1121 dma_unmap_single(dev, transfer->tx_dma, transfer->len, DMA_TO_DEVICE);
1122 }
1123}
1124
1125static int zx29_transfer_one_message(struct spi_master *master,
1126 struct spi_message *msg)
1127{
1128 struct zx29_spi *zx29spi = spi_master_get_devdata(master);
1129 struct spi_device *spi = msg->spi;
1130 struct spi_transfer *transfer;
1131 unsigned cs_change = 1;
1132 const int nsecs = 100;
1133 int ret = 0;
1134
1135 if(!zx29spi)
1136 return -EINVAL;
1137 //printk(KERN_INFO "ssp:in function %s \n", __FUNCTION__);
1138#if SPI_PSM_CONTROL
1139 zx29_spi_set_active(&zx29spi->psm_lock);
1140#endif
1141 //mutex_lock(&zx29spi->spi_lock);
1142 //printk(KERN_INFO "ssp:lock \n");
1143 /* Initial message state */
1144 zx29spi->cur_msg = msg;
1145 /* Setup the SPI using the per chip configuration */
1146 zx29spi->cur_chip = spi_get_ctldata(msg->spi);
1147
1148 if ((clk_get_rate(zx29spi->spi_clk) / 2) != spi->max_speed_hz) {
1149 clk_set_rate(zx29spi->spi_clk, spi->max_speed_hz * 2);
1150 }
1151
1152 restore_state(zx29spi);
1153
1154 ret = zx29_spi_map_mssg(zx29spi, msg);
1155 /* continue with polling mode
1156 if(ret){
1157 goto out;
1158 }
1159 */
1160
1161 while (readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase)) & SPI_COM_CTRL_MASK_SSPE_BACK);
1162 writel(readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase)) | SPI_COM_CTRL_MASK_SSPE, (SPI_COM_CTRL_OFFSET+zx29spi->virtbase));
1163
1164 list_for_each_entry(transfer, &msg->transfers, transfer_list) {
1165
1166 zx29spi->cur_transfer = transfer;
1167 if (transfer->bits_per_word || transfer->speed_hz)
1168 dev_warn(&msg->spi->dev, "ignore bits & speed setting in transfer.");
1169
1170 if((void *)transfer->tx_buf != NULL){
1171 zx29spi->tx = (void *)transfer->tx_buf;
1172 zx29spi->tx_end = zx29spi->tx + zx29spi->cur_transfer->len;
1173 }
1174 else
1175 zx29spi->tx = zx29spi->tx_end = NULL;
1176
1177 if((void *)transfer->rx_buf != NULL){
1178 zx29spi->rx = (void *)transfer->rx_buf;
1179 zx29spi->rx_end = zx29spi->rx + zx29spi->cur_transfer->len;
1180
1181 /*if tx is null, use rx buffer as a dummy tx buffer.*/
1182 if((void *)transfer->tx_buf == NULL){
1183 zx29spi->tx = (void *)transfer->rx_buf;
1184 zx29spi->tx_end = zx29spi->tx + zx29spi->cur_transfer->len;
1185 }
1186 }
1187 else
1188 zx29spi->rx = zx29spi->rx_end = NULL;
1189
1190 zx29spi->write = zx29spi->tx ? zx29spi->cur_chip->write : WRITING_NULL;
1191 zx29spi->read = zx29spi->rx ? zx29spi->cur_chip->read : READING_NULL;
1192
1193 if (transfer->rx_buf || transfer->rx_dma)
1194 flush(zx29spi);
1195 writel(CLEAR_ALL_INTERRUPTS, (SPI_INTR_SR_OFFSET+zx29spi->virtbase));
1196
1197 if (cs_change) {
1198 zx29spi->cur_chip->cs_control(ZX29_CS_ACTIVE);
1199 }
1200 cs_change = transfer->cs_change;
1201
1202 if (zx29spi->cur_chip->xfer_type == POLLING_TRANSFER || (!transfer->tx_dma && !transfer->rx_dma)) {
1203 ret = zx29_do_polling_transfer(zx29spi);
1204 #if defined(CONFIG_DEBUG_FS)
1205 zx29spi->spi_poll_cnt ++;
1206 #endif
1207 } else {
1208 struct chip_data *chip = zx29spi->cur_chip;
1209
1210 if (transfer->rx_buf || transfer->rx_dma) {
1211 writel((chip->fifo_ctrl | (SPI_FIFO_CTRL_MASK_RX_DMA_EN | SPI_FIFO_CTRL_MASK_TX_DMA_EN)),
1212 (SPI_FIFO_CTRL_OFFSET+zx29spi->virtbase));
1213 } else {
1214 writel((chip->fifo_ctrl | SPI_FIFO_CTRL_MASK_TX_DMA_EN), (SPI_FIFO_CTRL_OFFSET+zx29spi->virtbase));
1215 }
1216
1217 ret = zx29_do_interrupt_dma_transfer(zx29spi);
1218 #if defined(CONFIG_DEBUG_FS)
1219 zx29spi->spi_dma_cnt ++;
1220 #endif
1221
1222 /* clear TX/RX DMA Enable */
1223 writel(chip->fifo_ctrl, (SPI_FIFO_CTRL_OFFSET+zx29spi->virtbase));
1224 }
1225
1226 if (ret) {
1227 pr_info("ssp:transfer error,transfer=%p\n", transfer);
1228 break;
1229 }
1230
1231 /* Update total byte transferred */
1232 msg->actual_length += zx29spi->cur_transfer->len;
1233
1234 if (transfer->delay_usecs)
1235 udelay(transfer->delay_usecs);
1236
1237 if (cs_change) {
1238 zx29spi->cur_chip->cs_control(ZX29_CS_INACTIVE);
1239 ndelay(nsecs);
1240 }
1241 }
1242 if (ret || !cs_change) {
1243 zx29spi->cur_chip->cs_control(ZX29_CS_INACTIVE);
1244 }
1245 while (~ readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase)) & SPI_COM_CTRL_MASK_SSPE_BACK);
1246 writel(readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase)) & ~ SPI_COM_CTRL_MASK_SSPE, (SPI_COM_CTRL_OFFSET+zx29spi->virtbase));
1247
1248out:
1249 zx29_spi_unmap_mssg(zx29spi, msg);
1250 //mutex_unlock(&zx29spi->spi_lock);
1251 //printk(KERN_INFO "ssp:unlock \n");
1252
1253 msg->status = ret;
1254 spi_finalize_current_message(master);
1255
1256#if SPI_PSM_CONTROL
1257 zx29_spi_set_idle(&zx29spi->psm_lock);
1258#endif
1259
1260 return ret;
1261}
1262
1263
1264
1265static int zx29_prepare_transfer_hardware(struct spi_master *master)
1266{
1267
1268 return 0;
1269}
1270
1271static int zx29_unprepare_transfer_hardware(struct spi_master *master)
1272{
1273 //struct zx29_spi *zx29spi = spi_master_get_devdata(master);
1274
1275 //dev_warn(&zx29spi->pdev->dev,"in function %s\n", __FUNCTION__);
1276
1277 /* nothing more to do - disable spi/ssp and power off */
1278 //writel(readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase)) & ~ SPI_COM_CTRL_MASK_SSPE, (SPI_COM_CTRL_OFFSET+zx29spi->virtbase));
1279
1280 return 0;
1281}
1282
1283static int verify_controller_parameters(struct zx29_spi *zx29spi,
1284 struct spi_config_chip const *chip_info)
1285{
1286 if ((chip_info->iface < SPI_INTERFACE_MOTOROLA_SPI)
1287 || (chip_info->iface > SPI_INTERFACE_TI_SYNC_SERIAL)) {
1288 dev_err(&zx29spi->pdev->dev,
1289 "interface is configured incorrectly\n");
1290 return -EINVAL;
1291 }
1292
1293 if ((chip_info->hierarchy != SPI_MASTER)
1294 && (chip_info->hierarchy != SPI_SLAVE)) {
1295 dev_err(&zx29spi->pdev->dev,
1296 "hierarchy is configured incorrectly\n");
1297 return -EINVAL;
1298 }
1299 if ((chip_info->com_mode != INTERRUPT_TRANSFER)
1300 && (chip_info->com_mode != DMA_TRANSFER)
1301 && (chip_info->com_mode != POLLING_TRANSFER)) {
1302 dev_err(&zx29spi->pdev->dev,
1303 "Communication mode is configured incorrectly\n");
1304 return -EINVAL;
1305 }
1306 switch (chip_info->rx_lev_trig) {
1307 case SPI_RX_1_OR_MORE_ELEM:
1308 case SPI_RX_4_OR_MORE_ELEM:
1309 case SPI_RX_8_OR_MORE_ELEM:
1310 /* These are always OK, all variants can handle this */
1311 break;
1312 case SPI_RX_16_OR_MORE_ELEM:
1313 if (zx29spi->vendor->fifodepth < 16) {
1314 dev_err(&zx29spi->pdev->dev,
1315 "RX FIFO Trigger Level is configured incorrectly\n");
1316 return -EINVAL;
1317 }
1318 break;
1319 case SPI_RX_32_OR_MORE_ELEM:
1320 if (zx29spi->vendor->fifodepth < 32) {
1321 dev_err(&zx29spi->pdev->dev,
1322 "RX FIFO Trigger Level is configured incorrectly\n");
1323 return -EINVAL;
1324 }
1325 break;
1326 default:
1327 dev_err(&zx29spi->pdev->dev,
1328 "RX FIFO Trigger Level is configured incorrectly\n");
1329 return -EINVAL;
1330 break;
1331 }
1332 switch (chip_info->tx_lev_trig) {
1333 case SPI_TX_1_OR_MORE_EMPTY_LOC:
1334 case SPI_TX_4_OR_MORE_EMPTY_LOC:
1335 case SPI_TX_8_OR_MORE_EMPTY_LOC:
1336 /* These are always OK, all variants can handle this */
1337 break;
1338 case SPI_TX_16_OR_MORE_EMPTY_LOC:
1339 if (zx29spi->vendor->fifodepth < 16) {
1340 dev_err(&zx29spi->pdev->dev,
1341 "TX FIFO Trigger Level is configured incorrectly\n");
1342 return -EINVAL;
1343 }
1344 break;
1345 case SPI_TX_32_OR_MORE_EMPTY_LOC:
1346 if (zx29spi->vendor->fifodepth < 32) {
1347 dev_err(&zx29spi->pdev->dev,
1348 "TX FIFO Trigger Level is configured incorrectly\n");
1349 return -EINVAL;
1350 }
1351 break;
1352 default:
1353 dev_err(&zx29spi->pdev->dev,
1354 "TX FIFO Trigger Level is configured incorrectly\n");
1355 return -EINVAL;
1356 break;
1357 }
1358
1359 return 0;
1360}
1361
1362static struct vendor_data vendor_arm = {
1363 .fifodepth = 16,
1364 .max_bpw = 32,
1365 .loopback = true,
1366};
1367
1368static struct resource spi0_gpio_resources[] ={
1369 [0]={
1370 .start = GPIO_AP_SPI0_TXD,
1371 .end = GPIO_AP_SPI0_TXD_FUN,
1372 .name = "txd",
1373 .flags = IORESOURCE_IO,
1374 },
1375 [1]={
1376 .start = GPIO_AP_SPI0_CLK,
1377 .end = GPIO_AP_SPI0_CLK_FUN,
1378 .name = "clk",
1379 .flags = IORESOURCE_IO,
1380 },
1381 [2]={
1382 .start = GPIO_AP_SPI0_CS,
1383 .end = GPIO_AP_CS_GPIO_FUN,
1384 .name = "cs",
1385 .flags = IORESOURCE_IO,
1386 },
1387#if 1
1388 [3]={
1389 .start = GPIO_AP_SPI0_RXD,
1390 .end = GPIO_AP_SPI0_RXD_FUN,
1391 .name = "rxd",
1392 .flags = IORESOURCE_IO,
1393 }
1394#endif
1395};
1396#ifdef CONFIG_ARCH_ZX297520V3
1397static struct resource spi1_gpio_resources[] ={
1398 [0]={
1399 .start = GPIO_AP_SPI1_TXD,
1400 .end = GPIO_AP_SPI1_TXD_FUN,
1401 .name = "txd",
1402 .flags = IORESOURCE_IO,
1403 },
1404 [1]={
1405 .start = GPIO_AP_SPI1_CLK,
1406 .end = GPIO_AP_SPI1_CLK_FUN,
1407 .name = "clk",
1408 .flags = IORESOURCE_IO,
1409 },
1410 [2]={
1411 .start = GPIO_AP_SPI1_CS,
1412 .end = GPIO_AP_CS1_GPIO_FUN,
1413 .name = "cs",
1414 .flags = IORESOURCE_IO,
1415 },
1416#if 1
1417 [3]={
1418 .start = GPIO_AP_SPI1_RXD,
1419 .end = GPIO_AP_SPI1_RXD_FUN,
1420 .name = "rxd",
1421 .flags = IORESOURCE_IO,
1422 }
1423#endif
1424};
1425#endif
1426/*
1427 * A piece of default chip info unless the platform
1428 * supplies it.
1429 */
1430static const struct spi_config_chip spi_default_chip_info = {
1431 .com_mode = POLLING_TRANSFER,
1432 .iface = SPI_INTERFACE_MOTOROLA_SPI,
1433 .hierarchy = SPI_MASTER,
1434 .slave_tx_disable = DO_NOT_DRIVE_TX,
1435 .rx_lev_trig = SPI_RX_4_OR_MORE_ELEM,
1436 .tx_lev_trig = SPI_TX_4_OR_MORE_EMPTY_LOC,
1437// .ctrl_len = SSP_BITS_8,
1438// .wait_state = SSP_MWIRE_WAIT_ZERO,
1439// .duplex = SSP_MICROWIRE_CHANNEL_FULL_DUPLEX,
1440// .cs_control = default_cs_control,
1441};
1442
1443/*
1444* spi ʹÓÃGPIOģʽ¶ÁÈ¡LCD µÄID Begin
1445*/
1446static void spi_set_gpio_function(int id)
1447{
1448 //TODO:ÉèÖÃGPIOΪ¹¦ÄܽÅ
1449 if(id == 0){
1450 /* zx29_gpio_function_sel(GPIO_AP_SPI0_CS, GPIO_AP_SPI0_CS_FUN); */
1451 zx29_gpio_function_sel(GPIO_AP_SPI0_CLK, GPIO_AP_SPI0_CLK_FUN);
1452 zx29_gpio_function_sel(GPIO_AP_SPI0_RXD, GPIO_AP_SPI0_RXD_FUN);
1453 zx29_gpio_function_sel(GPIO_AP_SPI0_TXD, GPIO_AP_SPI0_TXD_FUN);
1454 }
1455 else if(id==1){
1456
1457 }
1458}
1459static void spi_set_gpio_gpio(int id)
1460{
1461 //TODO:ÉèÖÃGPIOΪGPIO½Å
1462 if(id == 0){
1463 /* zx29_gpio_function_sel(GPIO_AP_SPI0_CS, GPIO_AP_CS_GPIO_FUN); */
1464 zx29_gpio_function_sel(GPIO_AP_SPI0_CLK, GPIO_AP_CLK_GPIO_FUN);
1465 zx29_gpio_function_sel(GPIO_AP_SPI0_RXD, GPIO_AP_RXD_GPIO_FUN);
1466 zx29_gpio_function_sel(GPIO_AP_SPI0_TXD, GPIO_AP_TXD_GPIO_FUN);
1467 }
1468 else if(id==1){
1469
1470 }
1471}
1472static void spi_set_gpio_val(int gpio_num, int val)
1473{
1474 zx29_gpio_output_data(gpio_num, val);
1475}
1476
1477static int spi_get_gpio_val(int gpio_num)
1478{
1479 //zx29_gpio_set_direction(gpio,GPIO_IN);
1480
1481 return zx29_gpio_input_data(gpio_num);
1482}
1483
1484static void spi_time_delay(int delay/*us*/)
1485{
1486 udelay(delay);
1487}
1488
1489void spi_gpio_mode_start(void)
1490{
1491 //mutex_lock(&g_zx29_spi->spi_lock); //spi control function mutex.
1492 /* set clk tx rx cs to gpio */
1493 spi_set_gpio_gpio(0);
1494
1495 zx29_gpio_set_direction(GPIO_AP_SPI0_CS, GPIO_OUT);
1496 zx29_gpio_set_direction(GPIO_AP_SPI0_CLK, GPIO_OUT);
1497 zx29_gpio_set_direction(GPIO_AP_SPI0_TXD, GPIO_OUT);
1498 zx29_gpio_set_direction(GPIO_AP_SPI0_RXD, GPIO_IN);
1499
1500 spi_set_gpio_val(GPIO_AP_SPI0_CS, SPI_GPIO_HIGH);/* CSµÍÓÐЧ */
1501 spi_set_gpio_val(GPIO_AP_SPI0_CLK, SPI_GPIO_LOW);/* clk¿ÕÏÐʱΪµÍ */
1502#if 0
1503 spi_set_gpio_val(GPIO_AP_SPI0_CLK, SPI_GPIO_LOW);
1504 spi_set_gpio_val(GPIO_AP_SPI0_CLK, SPI_GPIO_LOW);
1505#endif
1506}
1507EXPORT_SYMBOL(spi_gpio_mode_start);
1508void spi_gpio_mode_stop(void)
1509{
1510 /* set clk tx rx cs to function */
1511 spi_set_gpio_function(0);
1512 //mutex_unlock(&g_zx29_spi->spi_lock); //spi control function mutex.
1513}
1514EXPORT_SYMBOL(spi_gpio_mode_stop);
1515
1516void spi_gpio_write_single8(unsigned char data)
1517{
1518 int i;
1519
1520 //printk("howard spi_gpio_write_single8 %x\n", data);
1521
1522 spi_set_gpio_val(GPIO_AP_SPI0_CLK, SPI_GPIO_HIGH);
1523 spi_set_gpio_val(GPIO_AP_SPI0_CS, SPI_GPIO_LOW);/* CSµÍÓÐЧ */
1524
1525 for( i=7; i>=0; i-- )
1526 {
1527 spi_set_gpio_val(GPIO_AP_SPI0_CLK, SPI_GPIO_LOW);
1528 if ((data >> i) & 0x1)
1529 {
1530 spi_set_gpio_val(GPIO_AP_SPI0_TXD, SPI_GPIO_HIGH);
1531 }
1532 else
1533 {
1534 spi_set_gpio_val(GPIO_AP_SPI0_TXD, SPI_GPIO_LOW);
1535 }
1536 spi_time_delay(1);
1537 spi_set_gpio_val(GPIO_AP_SPI0_CLK, SPI_GPIO_HIGH);
1538 spi_time_delay(1);
1539 }
1540 spi_set_gpio_val(GPIO_AP_SPI0_CS, SPI_GPIO_HIGH);
1541 spi_set_gpio_val(GPIO_AP_SPI0_TXD, SPI_GPIO_LOW);
1542
1543}
1544EXPORT_SYMBOL(spi_gpio_write_single8);
1545/*******************************************************************************
1546 * Function:
1547 * Description:
1548 * Parameters:
1549 * Input:
1550 *
1551 * Output:
1552 *
1553 * Returns:
1554 *
1555 *
1556 * Others:
1557 ********************************************************************************/
1558unsigned char spi_gpio_read_single8(void)
1559{
1560 int i;
1561 unsigned char readData = 0;
1562
1563 spi_set_gpio_val(GPIO_AP_SPI0_CLK, SPI_GPIO_HIGH);
1564 spi_set_gpio_val(GPIO_AP_SPI0_CS, SPI_GPIO_LOW);/* CSµÍÓÐЧ */
1565
1566 for( i=7; i>=0; i-- )
1567 {
1568 spi_set_gpio_val(GPIO_AP_SPI0_CLK, SPI_GPIO_LOW);
1569 spi_time_delay(1);
1570 spi_set_gpio_val(GPIO_AP_SPI0_CLK, SPI_GPIO_HIGH);
1571 if( spi_get_gpio_val(GPIO_AP_SPI0_RXD) )/* lcd ¸´ÓÃtx rx */
1572 {
1573 readData |= (1 << i);
1574 }
1575 spi_time_delay(1);
1576 }
1577 spi_set_gpio_val(GPIO_AP_SPI0_CS, SPI_GPIO_HIGH);
1578
1579 //printk("howard spi_gpio_read_single8 %x\n", readData);
1580 return readData;
1581}
1582EXPORT_SYMBOL(spi_gpio_read_single8);
1583
1584/**
1585 * @brief spi gpio mode, cs control
1586 *
1587 * This function used for lcd 3-wires spi mode.
1588 * before cs pull down, spi pads will change to gpio mode.
1589 * after cs pull high, spi pads gpio mode recovery to spi mode.
1590 *
1591 * @param level 0: cs line pull down, no-zero: cs line pull up.
1592 *
1593 * @retval none
1594 */
1595void spi_gpio_3wire_cs(unsigned char level)
1596{
1597 if(level){
1598 spi_set_gpio_val(GPIO_AP_SPI0_CS, SPI_GPIO_HIGH);
1599 gpio_direction_input(GPIO_AP_SPI0_TXD);
1600
1601 /* zx29_gpio_function_sel(GPIO_AP_SPI0_CS, GPIO_AP_SPI0_CS_FUN); */
1602 zx29_gpio_function_sel(GPIO_AP_SPI0_CLK, GPIO_AP_SPI0_CLK_FUN);
1603 zx29_gpio_function_sel(GPIO_AP_SPI0_TXD, GPIO_AP_SPI0_TXD_FUN);
1604
1605 //mutex_unlock(&g_zx29_spi->spi_lock); //spi control function mutex.
1606 }
1607 else{
1608 //mutex_lock(&g_zx29_spi->spi_lock);
1609
1610 /* zx29_gpio_function_sel(GPIO_AP_SPI0_CS, GPIO_AP_CS_GPIO_FUN); */
1611 zx29_gpio_function_sel(GPIO_AP_SPI0_CLK, GPIO_AP_CLK_GPIO_FUN);
1612 zx29_gpio_function_sel(GPIO_AP_SPI0_TXD, GPIO_AP_TXD_GPIO_FUN);
1613
1614 gpio_direction_output(GPIO_AP_SPI0_CS, SPI_GPIO_LOW);
1615 gpio_direction_output(GPIO_AP_SPI0_CLK, SPI_GPIO_LOW);
1616 gpio_direction_output(GPIO_AP_SPI0_TXD, SPI_GPIO_LOW);
1617
1618 spi_set_gpio_val(GPIO_AP_SPI0_CLK, SPI_GPIO_LOW);/* CSµÍÓÐЧ */
1619 spi_set_gpio_val(GPIO_AP_SPI0_CS, SPI_GPIO_LOW);
1620 }
1621}
1622EXPORT_SYMBOL(spi_gpio_3wire_cs);
1623
1624/**
1625 * @brief spi gpio mode, one byte write.
1626 *
1627 * This function used for lcd 3-wires spi mode.
1628 * txd line used tx function and rx function at different time.
1629 *
1630 * @param reg one byte write data.
1631 *
1632 * @retval none
1633 */
1634void spi_gpio_3wire_write8(unsigned char reg)
1635{
1636 int i;
1637 //unsigned char readData = 0;
1638
1639 //write
1640 spi_time_delay(50);
1641 for (i = 0; i < 8; i++)
1642 {
1643 gpio_set_value(GPIO_AP_SPI0_CLK, SPI_GPIO_LOW);
1644 spi_time_delay(50);
1645
1646 if ((reg & 0x80)==0x80)
1647 {
1648 gpio_set_value(GPIO_AP_SPI0_TXD, SPI_GPIO_HIGH);
1649 }
1650 else
1651 {
1652 gpio_set_value(GPIO_AP_SPI0_TXD, SPI_GPIO_LOW);
1653 }
1654 spi_time_delay(50);
1655
1656 gpio_set_value(GPIO_AP_SPI0_CLK, SPI_GPIO_HIGH);
1657 spi_time_delay(50);
1658
1659 reg <<= 1;
1660 }
1661 //spi_time_delay(50);
1662}
1663EXPORT_SYMBOL(spi_gpio_3wire_write8);
1664
1665/**
1666 * @brief spi gpio mode, one byte read.
1667 *
1668 * This function used for lcd 3-wires spi mode.
1669 * txd line used tx function and rx function at different time.
1670 *
1671 * @param none.
1672 *
1673 * @retval one byte readed data.
1674 */
1675unsigned char spi_gpio_3wire_read8(void)
1676{
1677 int i;
1678 unsigned char readData = 0;
1679 //read
1680 gpio_direction_input(GPIO_AP_SPI0_TXD);
1681 spi_time_delay(50);
1682
1683 readData = 0;
1684 for (i = 0; i < 8; i++)
1685 {
1686 readData <<= 1;
1687 gpio_set_value(GPIO_AP_SPI0_CLK, SPI_GPIO_LOW);
1688 spi_time_delay(50);
1689
1690 if (GPIO_HIGH == gpio_get_value(GPIO_AP_SPI0_TXD))
1691 {
1692 readData |= 0x01;
1693 }
1694
1695 gpio_set_value(GPIO_AP_SPI0_CLK, SPI_GPIO_HIGH);
1696 spi_time_delay(50);
1697 }
1698 //spi_time_delay(50);
1699
1700 //printk("howard spi_gpio_read_single8 %x\n", readData);
1701 return readData;
1702}
1703EXPORT_SYMBOL(spi_gpio_3wire_read8);
1704
1705
1706
1707static int zx29_setup(struct spi_device *spi)
1708{
1709 struct spi_config_chip const *chip_info;
1710 struct chip_data *chip;
1711 unsigned speed_hz;
1712 int status = 0;
1713 struct zx29_spi *zx29spi = spi_master_get_devdata(spi->master);
1714 unsigned int bits = spi->bits_per_word;
1715 u32 tmp;
1716
1717 if ((!spi->max_speed_hz)||(!zx29spi))
1718 return -EINVAL;
1719
1720 chip = spi_get_ctldata(spi);
1721
1722 if (chip == NULL) {
1723 chip = kzalloc(sizeof(struct chip_data), GFP_KERNEL);
1724 if (!chip) {
1725 dev_err(&spi->dev, "cannot alloc chip_data\n");
1726 return -ENOMEM;
1727 }
1728 dev_dbg(&spi->dev, "allocated memory for controller's runtime state\n");
1729 }
1730
1731 chip_info = spi->controller_data;
1732
1733 if (chip_info == NULL) {
1734 chip_info = &spi_default_chip_info;
1735 /* spi_board_info.controller_data not is supplied */
1736 dev_dbg(&spi->dev, "using default chip_info_data settings\n");
1737 } else
1738 dev_dbg(&spi->dev, "using user suppliedchip_info_data settings\n");
1739
1740 /* set spi clock source at 104MHz/1 */
1741 // zx297520v2spi->spi_clk->ops->set_division(zx297520v2spi->spi_clk,chip ->clk_div-1);
1742 //writel(chip ->clk_div-1, M0_SSP_CLKDIV_REG_VA);
1743 speed_hz = spi->max_speed_hz;
1744// clk_set_rate(zx29spi->spi_clk, speed_hz * 2); /* f(ssp_clk) = 2*f(ssp_sclk_out) */
1745 spi->max_speed_hz = clk_round_rate(zx29spi->spi_clk, speed_hz * 2) / 2;
1746 if (spi->max_speed_hz != speed_hz)
1747 dev_warn(&spi->dev, "round speed %dHz differs from requested %dHz.", spi->max_speed_hz, speed_hz);
1748
1749 status = verify_controller_parameters(zx29spi, chip_info);
1750 if (status) {
1751 dev_err(&spi->dev, "controller data is incorrect");
1752 goto err_config_params;
1753 }
1754
1755 zx29spi->rx_lev_trig = chip_info->rx_lev_trig;
1756 zx29spi->tx_lev_trig = chip_info->tx_lev_trig;
1757
1758 chip->xfer_type = chip_info->com_mode;
1759
1760 if (!chip_info->cs_control) {
1761 if (spi->master->bus_num == 0)
1762 chip->cs_control = default_cs0_control;
1763#ifdef CONFIG_ARCH_ZX297520V3
1764 if (spi->master->bus_num == 1)
1765 chip->cs_control = default_cs1_control;
1766#endif
1767 if (spi->master->num_chipselect != 1)
1768 dev_err(&spi->dev, "chip select function is NULL!\n");
1769 } else
1770 chip->cs_control = chip_info->cs_control;
1771
1772 /* Check bits per word with vendor specific range */
1773 if ((bits <= 3) || (bits > zx29spi->vendor->max_bpw)) {
1774 status = -ENOTSUPP;
1775 dev_err(&spi->dev, "illegal data size for this controller!\n");
1776 dev_err(&spi->dev, "This controller can only handle 4 <= n <= %d bit words\n",
1777 zx29spi->vendor->max_bpw);
1778 goto err_config_params;
1779 } else if (bits <= 8) {
1780 dev_dbg(&spi->dev, "4 <= n <=8 bits per word\n");
1781 chip->n_bytes = 1;
1782 chip->read = READING_U8;
1783 chip->write = WRITING_U8;
1784 } else if (bits <= 16) {
1785 dev_dbg(&spi->dev, "9 <= n <= 16 bits per word\n");
1786 chip->n_bytes = 2;
1787 chip->read = READING_U16;
1788 chip->write = WRITING_U16;
1789 } else {
1790 dev_dbg(&spi->dev, "17 <= n <= 32 bits per word\n");
1791 chip->n_bytes = 4;
1792 chip->read = READING_U32;
1793 chip->write = WRITING_U32;
1794 }
1795
1796 /* Now Initialize all register settings required for this chip */
1797 chip->com_ctrl = 0;
1798 chip->fmt_ctrl = 0;
1799 chip->fifo_ctrl = 0;
1800
1801 if ((chip_info->com_mode == DMA_TRANSFER)
1802 && ((zx29spi->master_info)->enable_dma)) {
1803 chip->enable_dma = true;
1804 dev_dbg(&spi->dev, "DMA mode set in controller state\n");
1805 } else {
1806 chip->enable_dma = false;
1807 dev_dbg(&spi->dev, "DMA mode NOT set in controller state\n");
1808 }
1809 SPI_WRITE_BITS(chip->fifo_ctrl, SPI_DMA_DISABLED, SPI_FIFO_CTRL_MASK_RX_DMA_EN, 2);
1810 SPI_WRITE_BITS(chip->fifo_ctrl, SPI_DMA_DISABLED, SPI_FIFO_CTRL_MASK_TX_DMA_EN, 3);
1811
1812 if (zx29spi->rx_lev_trig == SPI_RX_8_OR_MORE_ELEM)
1813 SPI_WRITE_BITS(chip->fifo_ctrl, SPI_FIFO_THRES_8, SPI_FIFO_CTRL_MASK_RX_FIFO_THRES, 4);
1814 else
1815 SPI_WRITE_BITS(chip->fifo_ctrl, SPI_FIFO_THRES_4, SPI_FIFO_CTRL_MASK_RX_FIFO_THRES, 4);
1816 if (zx29spi->tx_lev_trig == SPI_TX_8_OR_MORE_EMPTY_LOC)
1817 SPI_WRITE_BITS(chip->fifo_ctrl, SPI_FIFO_THRES_8, SPI_FIFO_CTRL_MASK_TX_FIFO_THRES, 8);
1818 else
1819 SPI_WRITE_BITS(chip->fifo_ctrl, SPI_FIFO_THRES_4, SPI_FIFO_CTRL_MASK_TX_FIFO_THRES, 8);
1820
1821 SPI_WRITE_BITS(chip->fmt_ctrl, bits - 1, SPI_FMT_CTRL_MASK_DSS, 4);
1822 SPI_WRITE_BITS(chip->fmt_ctrl, chip_info->iface, SPI_FMT_CTRL_MASK_FRF, 0);
1823
1824 /* Stuff that is common for all versions */
1825 if (spi->mode & SPI_CPOL)
1826 tmp = SPI_CLK_POL_IDLE_HIGH;
1827 else
1828 tmp = SPI_CLK_POL_IDLE_LOW;
1829 SPI_WRITE_BITS(chip->fmt_ctrl, tmp, SPI_FMT_CTRL_MASK_POL, 2);
1830
1831 if (spi->mode & SPI_CPHA)
1832 tmp = SPI_CLK_SECOND_EDGE;
1833 else
1834 tmp = SPI_CLK_FIRST_EDGE;
1835
1836 SPI_WRITE_BITS(chip->fmt_ctrl, tmp, SPI_FMT_CTRL_MASK_PHA, 3);
1837
1838 if (zx29spi->vendor->loopback) {
1839 if (spi->mode & SPI_LOOP)
1840 tmp = LOOPBACK_ENABLED;
1841 else
1842 tmp = LOOPBACK_DISABLED;
1843 SPI_WRITE_BITS(chip->com_ctrl, tmp, SPI_COM_CTRL_MASK_LBM, 0);
1844 }
1845// SPI_WRITE_BITS(chip->com_ctrl, SPI_ENABLED, SPI_COM_CTRL_MASK_SSPE, 1);
1846 SPI_WRITE_BITS(chip->com_ctrl, chip_info->hierarchy, SPI_COM_CTRL_MASK_MS, 2);
1847// SPI_WRITE_BITS(chip->com_ctrl, chip_info->slave_tx_disable, SPI_COM_CTRL_MASK_SOD, 3);
1848
1849 /* Save controller_state */
1850 spi_set_ctldata(spi, chip);
1851 return status;
1852 err_config_params:
1853 spi_set_ctldata(spi, NULL);
1854 kfree(chip);
1855 return status;
1856}
1857
1858
1859static void zx29_cleanup(struct spi_device *spi)
1860{
1861 struct chip_data *chip = spi_get_ctldata(spi);
1862
1863 spi_set_ctldata(spi, NULL);
1864 kfree(chip);
1865}
1866
1867static int zx29_spi_clock_init(struct zx29_spi *zx29spi)
1868{
1869 int status=0;
1870 struct platform_device *pdev = zx29spi->pdev;
1871 /* work clock */
1872 zx29spi->spi_clk = clk_get(&pdev->dev, "work_clk");//clk_get_sys("zx297520v2_ssp.0", const char * con_id);//
1873 if (IS_ERR(zx29spi->spi_clk)) {
1874 status = PTR_ERR(zx29spi->spi_clk);
1875 dev_err(&pdev->dev, "could not retrieve SPI work clock\n");
1876 return status;
1877 }
1878 /* enable ssp clock source */
1879 clk_enable(zx29spi->spi_clk);
1880
1881 /* enable spiclk at function zx297520v2_setup */
1882 zx29spi->clkfreq = SPI_SPICLK_FREQ_104M;
1883
1884 /* apb clock */
1885 zx29spi->pclk = clk_get(&pdev->dev, "apb_clk");
1886 if (IS_ERR(zx29spi->pclk)) {
1887 status = PTR_ERR(zx29spi->pclk);
1888 dev_err(&pdev->dev, "could not retrieve SPI work clock\n");
1889 return status;
1890 }
1891 clk_enable(zx29spi->pclk);
1892
1893 return status;
1894}
1895
1896
1897#if defined(CONFIG_DEBUG_FS)
1898#define dump_register(reg) \
1899{ \
1900 .name = __stringify(reg), \
1901 .offset = SPI_ ##reg##_OFFSET, \
1902}
1903
1904
1905static const struct debugfs_reg32 spi_regs[] = {
1906 dump_register(VER_REG),
1907 dump_register(COM_CTRL),
1908 dump_register(FMT_CTRL),
1909 dump_register(DR),
1910 dump_register(FIFO_CTRL),
1911 dump_register(FIFO_SR),
1912 dump_register(INTR_EN),
1913 dump_register(INTR_SR),
1914 dump_register(TIMING),
1915};
1916
1917//#define Strcat(x, fmt, ...) sprintf(x, "%s" #fmt, x, __VA_ARGS__)
1918#ifdef CONFIG_DEBUG_FS
1919static void debugfs_spi_init(struct zx29_spi *zx29spi)
1920{
1921 struct dentry *root;
1922 struct dentry *node;
1923 char tmp[32];
1924
1925 if(!zx29spi)
1926 return;
1927
1928 //create root
1929 sprintf(tmp,"spi%d_zx29", zx29spi->pdev->id);
1930 root = debugfs_create_dir(tmp, NULL);
1931 if (!root) {
1932 dev_err(&zx29spi->pdev->dev, "debugfs_create_dir %s err\n", tmp);
1933 goto err;
1934 }
1935
1936 //create regs
1937 zx29spi->spi_regset.regs = (struct debugfs_reg32 *)spi_regs;
1938 zx29spi->spi_regset.nregs = sizeof(spi_regs)/sizeof(struct debugfs_reg32);
1939 zx29spi->spi_regset.base = zx29spi->virtbase;
1940 node = debugfs_create_regset32("spi_regs", S_IRUGO, root, &zx29spi->spi_regset);
1941 if (!node){
1942 dev_err(&zx29spi->pdev->dev, "debugfs_create_regset32 err, base=%p, num=%d\n", zx29spi->spi_regset.base, zx29spi->spi_regset.nregs);
1943 goto err;
1944 }
1945
1946 //create info
1947 node = debugfs_create_u32("poll_cnt", S_IRUGO, root, &zx29spi->spi_poll_cnt);
1948 if (!node ){
1949 dev_err(&zx29spi->pdev->dev, "debugfs_create_u32 poll_cnt err\n");
1950 goto err;
1951 }
1952
1953 node = debugfs_create_u32("dma_cnt", S_IRUGO, root, &zx29spi->spi_dma_cnt);
1954 if (!node ){
1955 dev_err(&zx29spi->pdev->dev, "debugfs_create_u32 dma_cnt err\n");
1956 goto err;
1957 }
1958
1959 zx29spi->spi_root = (void *)root;
1960 return;
1961err:
1962 dev_err(&zx29spi->pdev->dev, "debugfs_spi_init err\n");
1963}
1964#endif
1965#endif
1966
1967static int __devinit zx29_spi_probe(struct platform_device *pdev)
1968{
1969 struct device *dev = &pdev->dev;
1970 struct zx29_spi_controller *platform_info = pdev->dev.platform_data;
1971 struct spi_master *master;
1972 struct zx29_spi *zx29spi = NULL; /*Data for this driver */
1973 struct resource *regs = NULL;
1974 struct resource *gpio = NULL;
1975 struct resource *irq = NULL;
1976 int status = 0, i;
1977 u32 regval = 0;
1978
1979 if (platform_info == NULL) {
1980 dev_err(&pdev->dev, "probe - no platform data supplied\n");
1981 status = -ENODEV;
1982 goto err_no_pdata;
1983 }
1984
1985 /* Allocate master with space for data */
1986 master = spi_alloc_master(dev, sizeof(struct zx29_spi));
1987 if (master == NULL) {
1988 dev_err(&pdev->dev, "probe - cannot alloc SPI master\n");
1989 status = -ENOMEM;
1990 goto err_no_master;
1991 }
1992
1993 zx29spi = spi_master_get_devdata(master);
1994 if(!zx29spi)
1995 return -EINVAL;
1996
1997 snprintf(zx29spi->name, sizeof(zx29spi->name), "zx29-spi%d", pdev->id);
1998 //mutex_init(&zx29spi->spi_lock);
1999 g_zx29_spi[pdev->id] = zx29spi;
2000 zx29spi->master = master;
2001 zx29spi->master_info = platform_info;
2002 zx29spi->pdev = pdev;
2003 zx29spi->vendor = &vendor_arm;
2004 sema_init(&zx29spi->sema_dma, 0);
2005
2006 dev_set_drvdata(&pdev->dev, zx29spi);
2007 /*
2008 * Bus Number Which has been Assigned to this SSP controller
2009 * on this board
2010 */
2011 master->bus_num = platform_info->bus_id;
2012 master->num_chipselect = platform_info->num_chipselect;
2013 master->cleanup = zx29_cleanup;
2014 master->setup = zx29_setup;
2015 master->prepare_transfer_hardware = zx29_prepare_transfer_hardware;
2016 master->transfer_one_message = zx29_transfer_one_message;
2017 master->unprepare_transfer_hardware = zx29_unprepare_transfer_hardware;
2018 master->rt = platform_info->rt;
2019
2020 /*
2021 * Supports mode 0-3, loopback, and active low CS.
2022 */
2023 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_NO_CS;
2024
2025 dev_dbg(&pdev->dev, "BUSNO: %d\n", master->bus_num);
2026
2027 /* registers */
2028 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2029 if ( regs == NULL ){
2030 dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
2031 status = -ENOENT;
2032 goto err_no_registers;
2033 }
2034 if (master->bus_num == 0){
2035 zx29spi->phybase = ZX29_SSP0_PHYS;
2036 }
2037 else if (master->bus_num == 1){
2038 zx29spi->phybase = ZX29_SSP1_PHYS;
2039 }
2040 zx29spi->virtbase = (void *)regs->start;
2041
2042 if (zx29spi->virtbase == NULL) {
2043 status = -ENOMEM;
2044 goto err_no_ioremap;
2045 }
2046 dev_dbg( &pdev->dev," mapped registers from 0x%08x to 0x%p\n",
2047 regs->start, zx29spi->virtbase);
2048#ifdef CONFIG_DEBUG_FS
2049 debugfs_spi_init(zx29spi);//yuwei
2050#endif
2051
2052 if(0 == pdev->id) {
2053 ramdump_ram_conf_table_add("ssp0_reg_0x140A010.bin", zx29spi->phybase + 0x10, 0x3C, (unsigned long)zx29spi->virtbase + 0x10, 0, 0);
2054 /* gpios txd rxd sclk cs */
2055 for(i = 0; i < ARRAY_SIZE(spi0_gpio_resources); i++){
2056 //gpio = platform_get_resource(pdev, IORESOURCE_IO, i);
2057 gpio = &spi0_gpio_resources[i];
2058 /*
2059 if( gpio == NULL )
2060 {
2061 dev_err(&pdev->dev, "Cannot get IORESOURCE_IO\n");
2062 status = -ENOENT;
2063 goto err_gpios;
2064 }
2065 */
2066 dev_dbg(&pdev->dev, "used gpio num %d as %s \n", gpio->start, gpio ->name);
2067
2068 status = gpio_request(gpio->start,gpio->name);
2069 if( status < 0 )
2070 goto err_gpios;
2071 //zte_gpio_config(gpio->start, SET_FUNCTION);
2072 //zx29_gpio_function_sel(gpio->start,1);
2073 zx29_gpio_function_sel(gpio->start, gpio->end);
2074
2075 }
2076 gpio_direction_output(GPIO_AP_SPI0_CS, 1);
2077 //spi_set_gpio_function(pdev->id);
2078 } else if(1 == pdev->id) {
2079#ifdef CONFIG_ARCH_ZX297520V3
2080 ramdump_ram_conf_table_add("ssp0_reg_0x1410010.bin", zx29spi->phybase + 0x10, 0x3C, (unsigned long)zx29spi->virtbase + 0x10, 0, 0);
2081 /* gpios txd rxd sclk cs */
2082 for(i = 0; i < ARRAY_SIZE(spi1_gpio_resources); i++){
2083 //gpio = platform_get_resource(pdev, IORESOURCE_IO, i);
2084 gpio = &spi1_gpio_resources[i];
2085 /*
2086 if( gpio == NULL )
2087 {
2088 dev_err(&pdev->dev, "Cannot get IORESOURCE_IO\n");
2089 status = -ENOENT;
2090 goto err_gpios;
2091 }
2092 */
2093 dev_dbg(&pdev->dev, "used gpio num %d as %s \n", gpio->start, gpio ->name);
2094
2095 status = gpio_request(gpio->start,gpio->name);
2096 if( status < 0 )
2097 goto err_gpios;
2098 //zte_gpio_config(gpio->start, SET_FUNCTION);
2099 zx29_gpio_function_sel(gpio->start, gpio->end);
2100 }
2101 gpio_direction_output(GPIO_AP_SPI1_CS, 1);
2102#else
2103 printk(KERN_WARNING "spi1 unsupported yet.\n");
2104#endif
2105 }
2106
2107 /*clock init*/
2108 status = zx29_spi_clock_init(zx29spi);
2109 if(status)
2110 goto err_no_clk;
2111
2112 /* Initialize transfer pump */
2113 //tasklet_init(&zx29spi->pump_transfers, pump_transfers,(unsigned long)zx29spi);
2114
2115 /* Disable SPI */
2116 regval = readl((SPI_COM_CTRL_OFFSET+zx29spi->virtbase)) & (~SPI_COM_CTRL_MASK_SSPE);
2117
2118 writel(regval, (SPI_COM_CTRL_OFFSET+zx29spi->virtbase));
2119
2120 load_spi_default_config(zx29spi);
2121 writel(0, (SPI_TIMING_OFFSET + zx29spi->virtbase));
2122
2123 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2124 if (irq == NULL) {
2125 dev_err(&pdev->dev, "Cannot get IORESOURCE_IRQ\n");
2126 status = -ENOENT;
2127 goto err_no_irq;
2128 }
2129 zx29spi->irq = irq->start;
2130
2131 dev_dbg(&pdev->dev, "used interrupt num is %d\n", zx29spi->irq);
2132
2133 status = devm_request_irq(&pdev->dev, zx29spi->irq, zx29_spi_irq,
2134 IRQF_TRIGGER_HIGH | IRQF_NO_THREAD | IRQF_ONESHOT, dev_name(&pdev->dev), zx29spi);
2135 if (status < 0) {
2136 dev_err(&pdev->dev, "probe - cannot get IRQ (%d)\n", status);
2137 goto err_no_irq;
2138 }
2139 disable_irq_nosync(zx29spi->irq);
2140
2141 /* Get DMA channels */
2142 if (platform_info->enable_dma) {
2143 status = zx29_dma_probe(zx29spi);
2144 if (status != 0)
2145 platform_info->enable_dma = 0;
2146 }
2147
2148#if SPI_PSM_CONTROL
2149 wake_lock_init(&zx29spi->psm_lock, WAKE_LOCK_SUSPEND, zx29spi->name);
2150#endif
2151
2152 status = spi_register_master(master);
2153 if (status != 0) {
2154 dev_err(&pdev->dev, "probe - problem registering spi master\n");
2155 goto err_spi_register;
2156 }
2157 dev_dbg(&pdev->dev," probe succeeded\n");
2158
2159 /* let runtime pm put suspend */
2160 if (platform_info->autosuspend_delay > 0) {
2161 dev_info(&pdev->dev, "will use autosuspend for runtime pm, delay %dms\n", platform_info->autosuspend_delay);
2162 pm_runtime_set_autosuspend_delay(dev, platform_info->autosuspend_delay);
2163 pm_runtime_use_autosuspend(dev);
2164 pm_runtime_put_autosuspend(dev);
2165 } else {
2166 pm_runtime_put(dev);
2167 }
2168 return 0;
2169
2170 err_spi_register:
2171#if SPI_PSM_CONTROL
2172 wake_lock_destroy(&zx29spi->psm_lock);
2173#endif
2174 if (platform_info->enable_dma)
2175 zx29_dma_remove(zx29spi);
2176
2177 err_no_irq:
2178 clk_disable(zx29spi->spi_clk);
2179// err_no_clk_en:
2180 //err_clk_prep:
2181 clk_put(zx29spi->spi_clk);
2182 err_no_clk:
2183// iounmap(zx29spi->virtbase);
2184 err_gpios:
2185 /* add */
2186 err_no_ioremap:
2187 err_no_registers:
2188 spi_master_put(master);
2189 err_no_master:
2190 err_no_pdata:
2191 return status;
2192}
2193
2194static int __exit zx29_spi_remove(struct platform_device *pdev)
2195{
2196 struct zx29_spi *zx29spi = dev_get_drvdata(&pdev->dev);
2197 struct resource * gpio = NULL;
2198 //struct resource * irq = NULL;
2199 int i;
2200
2201 if (!zx29spi)
2202 return 0;
2203
2204 /*
2205 * undo pm_runtime_put() in probe. I assume that we're not
2206 * accessing the primecell here.
2207 */
2208 pm_runtime_get_noresume(&pdev->dev);
2209
2210 spi_unregister_master(zx29spi->master);
2211
2212 load_spi_default_config(zx29spi);
2213 if (zx29spi->master_info->enable_dma)
2214 zx29_dma_remove(zx29spi);
2215/*
2216 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
2217 if( irq != NULL )
2218 {
2219 free_irq(irq->start, zx29spi);
2220 }
2221*/
2222 devm_free_irq(&pdev->dev, zx29spi->irq, zx29spi);
2223
2224 clk_disable(zx29spi->spi_clk);
2225 clk_put(zx29spi->spi_clk);
2226
2227 clk_disable(zx29spi->pclk);
2228 clk_put(zx29spi->pclk);
2229
2230 #if defined(CONFIG_DEBUG_FS)
2231 if(zx29spi->spi_root){
2232 printk(KERN_INFO "spi:debugfs_remove_recursive \n");
2233 debugfs_remove_recursive(zx29spi->spi_root);
2234 }
2235 #endif
2236
2237 #if 0
2238 if(0 == pdev->id) {
2239 /* gpios txd rxd sclk sfr */
2240 for(i = 0; i < ARRAY_SIZE(spi0_gpio_resources); i++){
2241 //gpio = platform_get_resource(pdev, IORESOURCE_IO, i);
2242 gpio = &spi0_gpio_resources[i];
2243
2244 if( gpio != NULL )
2245 {
2246 gpio_free(gpio->start);
2247 }
2248 }
2249 }
2250
2251 else if(1 == pdev->id){
2252 /* gpios txd rxd sclk sfr */
2253 for(i = 0; i < ARRAY_SIZE(spi1_gpio_resources); i++){
2254 //gpio = platform_get_resource(pdev, IORESOURCE_IO, i);
2255 gpio = &spi1_gpio_resources[i];
2256
2257 if( gpio != NULL )
2258 {
2259 gpio_free(gpio->start);
2260 }
2261 }
2262 }
2263 #endif
2264
2265// iounmap(zx29spi->virtbase);
2266 //amba_release_regions(adev);
2267 //tasklet_disable(&zx29spi->pump_transfers);
2268
2269 spi_master_put(zx29spi->master);
2270 //amba_set_drvdata(adev, NULL);
2271 dev_set_drvdata(&pdev->dev, NULL);
2272
2273#if SPI_PSM_CONTROL
2274 wake_lock_destroy(&zx29spi->psm_lock);
2275#endif
2276
2277 return 0;
2278}
2279
2280static struct platform_driver zx29_spi_driver = {
2281 .driver = {
2282 //.name = "zx297520_ssp",
2283 .name = "zx29_ssp",
2284 .owner = THIS_MODULE,
2285 },
2286 .probe = zx29_spi_probe,
2287 .remove = __exit_p(zx29_spi_remove),
2288};
2289
2290static int __init zx29_spi_init(void)
2291{
2292 return platform_driver_register(&zx29_spi_driver);
2293}
2294
2295static void __exit zx29_spi_exit(void)
2296{
2297 platform_driver_unregister(&zx29_spi_driver);
2298}
2299
2300module_init(zx29_spi_init);
2301module_exit(zx29_spi_exit);
2302
2303MODULE_DESCRIPTION("zx29 spi controller driver");
2304MODULE_AUTHOR("Sanechips");
2305MODULE_LICENSE("GPL");
2306