1 #include <linux/module.h>
2 #include <linux/init.h>
3 #include <linux/kernel.h>
5 #include <linux/slab.h>
6 #include <linux/types.h>
7 #include <linux/cdev.h>
8 #include <asm/uaccess.h>
9 #include <linux/device.h>
10 #include <linux/spi/spi.h>
12 #include "linux_wlan_common.h"
14 #define USE_SPI_DMA 0 /* johnny add */
17 #if defined(PLAT_PANDA_ES_OMAP4460)
18 #define MIN_SPEED 12000000
19 #define MAX_SPEED 24000000
20 #elif defined(PLAT_WMS8304)
21 #define MIN_SPEED 12000000
22 #define MAX_SPEED 24000000 /* 4000000 */
23 #elif defined(CUSTOMER_PLATFORM)
25 TODO : define Clock speed under 48M.
28 * #define MIN_SPEED 24000000
29 * #define MAX_SPEED 48000000
32 #define MIN_SPEED 24000000
33 #define MAX_SPEED 48000000
35 #else /* WILC_ASIC_A0 */
36 /* Limit clk to 6MHz on FPGA. */
37 #define MIN_SPEED 6000000
38 #define MAX_SPEED 6000000
39 #endif /* WILC_ASIC_A0 */
41 static uint32_t SPEED
= MIN_SPEED
;
43 struct spi_device
*wilc_spi_dev
;
44 void linux_spi_deinit(void *vp
);
46 static int __init
wilc_bus_probe(struct spi_device
*spi
)
49 PRINT_D(BUS_DBG
, "spiModalias: %s\n", spi
->modalias
);
50 PRINT_D(BUS_DBG
, "spiMax-Speed: %d\n", spi
->max_speed_hz
);
53 printk("Driver Initializing success\n");
57 static int __exit
wilc_bus_remove(struct spi_device
*spi
)
64 static const struct of_device_id wilc1000_of_match
[] = {
65 { .compatible
= "atmel,wilc_spi", },
68 MODULE_DEVICE_TABLE(of
, wilc1000_of_match
);
71 struct spi_driver wilc_bus __refdata
= {
75 .of_match_table
= wilc1000_of_match
,
78 .probe
= wilc_bus_probe
,
79 .remove
= __exit_p(wilc_bus_remove
),
83 void linux_spi_deinit(void *vp
)
86 spi_unregister_driver(&wilc_bus
);
89 PRINT_ER("@@@@@@@@@@@@ restore SPI speed to %d @@@@@@@@@\n", SPEED
);
95 int linux_spi_init(void *vp
)
103 ret
= spi_register_driver(&wilc_bus
);
106 /* change return value to match WILC interface */
107 (ret
< 0) ? (ret
= 0) : (ret
= 1);
112 #if defined(PLAT_WMS8304)
113 #define TXRX_PHASE_SIZE (4096)
116 #if defined (NM73131_0_BOARD)
118 int linux_spi_write(uint8_t *b
, uint32_t len
)
123 if (len
> 0 && b
!= NULL
) {
124 struct spi_message msg
;
125 PRINT_D(BUS_DBG
, "Request writing %d bytes\n", len
);
126 struct spi_transfer tr
= {
133 spi_message_init(&msg
);
134 spi_message_add_tail(&tr
, &msg
);
135 ret
= spi_sync(wilc_spi_dev
, &msg
);
137 PRINT_ER("SPI transaction failed\n");
141 PRINT_ER("can't write data with the following length: %d\n", len
);
142 PRINT_ER("FAILED due to NULL buffer or ZERO length check the following length: %d\n", len
);
146 /* change return value to match WILC interface */
147 (ret
< 0) ? (ret
= 0) : (ret
= 1);
153 #elif defined(TXRX_PHASE_SIZE)
155 int linux_spi_write(uint8_t *b
, uint32_t len
)
158 if (len
> 0 && b
!= NULL
) {
160 int blk
= len
/ TXRX_PHASE_SIZE
;
161 int remainder
= len
% TXRX_PHASE_SIZE
;
163 char *r_buffer
= kzalloc(TXRX_PHASE_SIZE
, GFP_KERNEL
);
165 PRINT_ER("Failed to allocate memory for r_buffer\n");
170 struct spi_message msg
;
171 struct spi_transfer tr
= {
172 .tx_buf
= b
+ (i
* TXRX_PHASE_SIZE
),
173 .len
= TXRX_PHASE_SIZE
,
179 tr
.rx_buf
= r_buffer
;
181 memset(&msg
, 0, sizeof(msg
));
182 spi_message_init(&msg
);
183 msg
.spi
= wilc_spi_dev
;
184 msg
.is_dma_mapped
= USE_SPI_DMA
;
186 spi_message_add_tail(&tr
, &msg
);
187 ret
= spi_sync(wilc_spi_dev
, &msg
);
189 PRINT_ER("SPI transaction failed\n");
196 struct spi_message msg
;
197 struct spi_transfer tr
= {
198 .tx_buf
= b
+ (blk
* TXRX_PHASE_SIZE
),
204 tr
.rx_buf
= r_buffer
;
206 memset(&msg
, 0, sizeof(msg
));
207 spi_message_init(&msg
);
208 msg
.spi
= wilc_spi_dev
;
209 msg
.is_dma_mapped
= USE_SPI_DMA
; /* rachel */
211 spi_message_add_tail(&tr
, &msg
);
212 ret
= spi_sync(wilc_spi_dev
, &msg
);
214 PRINT_ER("SPI transaction failed\n");
219 PRINT_ER("can't write data with the following length: %d\n", len
);
220 PRINT_ER("FAILED due to NULL buffer or ZERO length check the following length: %d\n", len
);
224 /* change return value to match WILC interface */
225 (ret
< 0) ? (ret
= 0) : (ret
= 1);
232 int linux_spi_write(uint8_t *b
, uint32_t len
)
236 struct spi_message msg
;
238 if (len
> 0 && b
!= NULL
) {
239 struct spi_transfer tr
= {
245 char *r_buffer
= kzalloc(len
, GFP_KERNEL
);
247 PRINT_ER("Failed to allocate memory for r_buffer\n");
249 tr
.rx_buf
= r_buffer
;
250 PRINT_D(BUS_DBG
, "Request writing %d bytes\n", len
);
252 memset(&msg
, 0, sizeof(msg
));
253 spi_message_init(&msg
);
255 msg
.spi
= wilc_spi_dev
;
256 msg
.is_dma_mapped
= USE_SPI_DMA
;
258 spi_message_add_tail(&tr
, &msg
);
260 ret
= spi_sync(wilc_spi_dev
, &msg
);
262 PRINT_ER("SPI transaction failed\n");
267 PRINT_ER("can't write data with the following length: %d\n", len
);
268 PRINT_ER("FAILED due to NULL buffer or ZERO length check the following length: %d\n", len
);
272 /* change return value to match WILC interface */
273 (ret
< 0) ? (ret
= 0) : (ret
= 1);
281 #if defined (NM73131_0_BOARD)
283 int linux_spi_read(unsigned char *rb
, unsigned long rlen
)
289 struct spi_message msg
;
290 struct spi_transfer tr
= {
298 spi_message_init(&msg
);
299 spi_message_add_tail(&tr
, &msg
);
300 ret
= spi_sync(wilc_spi_dev
, &msg
);
302 PRINT_ER("SPI transaction failed\n");
305 PRINT_ER("can't read data with the following length: %ld\n", rlen
);
308 /* change return value to match WILC interface */
309 (ret
< 0) ? (ret
= 0) : (ret
= 1);
314 #elif defined(TXRX_PHASE_SIZE)
316 int linux_spi_read(unsigned char *rb
, unsigned long rlen
)
323 int blk
= rlen
/ TXRX_PHASE_SIZE
;
324 int remainder
= rlen
% TXRX_PHASE_SIZE
;
326 char *t_buffer
= kzalloc(TXRX_PHASE_SIZE
, GFP_KERNEL
);
328 PRINT_ER("Failed to allocate memory for t_buffer\n");
333 struct spi_message msg
;
334 struct spi_transfer tr
= {
335 .rx_buf
= rb
+ (i
* TXRX_PHASE_SIZE
),
336 .len
= TXRX_PHASE_SIZE
,
341 tr
.tx_buf
= t_buffer
;
343 memset(&msg
, 0, sizeof(msg
));
344 spi_message_init(&msg
);
345 msg
.spi
= wilc_spi_dev
;
346 msg
.is_dma_mapped
= USE_SPI_DMA
;
348 spi_message_add_tail(&tr
, &msg
);
349 ret
= spi_sync(wilc_spi_dev
, &msg
);
351 PRINT_ER("SPI transaction failed\n");
357 struct spi_message msg
;
358 struct spi_transfer tr
= {
359 .rx_buf
= rb
+ (blk
* TXRX_PHASE_SIZE
),
365 tr
.tx_buf
= t_buffer
;
367 memset(&msg
, 0, sizeof(msg
));
368 spi_message_init(&msg
);
369 msg
.spi
= wilc_spi_dev
;
370 msg
.is_dma_mapped
= USE_SPI_DMA
; /* rachel */
372 spi_message_add_tail(&tr
, &msg
);
373 ret
= spi_sync(wilc_spi_dev
, &msg
);
375 PRINT_ER("SPI transaction failed\n");
381 PRINT_ER("can't read data with the following length: %ld\n", rlen
);
384 /* change return value to match WILC interface */
385 (ret
< 0) ? (ret
= 0) : (ret
= 1);
391 int linux_spi_read(unsigned char *rb
, unsigned long rlen
)
397 struct spi_message msg
;
398 struct spi_transfer tr
= {
405 char *t_buffer
= kzalloc(rlen
, GFP_KERNEL
);
407 PRINT_ER("Failed to allocate memory for t_buffer\n");
409 tr
.tx_buf
= t_buffer
;
411 memset(&msg
, 0, sizeof(msg
));
412 spi_message_init(&msg
);
414 msg
.spi
= wilc_spi_dev
;
415 msg
.is_dma_mapped
= USE_SPI_DMA
;
417 spi_message_add_tail(&tr
, &msg
);
419 ret
= spi_sync(wilc_spi_dev
, &msg
);
421 PRINT_ER("SPI transaction failed\n");
425 PRINT_ER("can't read data with the following length: %ld\n", rlen
);
428 /* change return value to match WILC interface */
429 (ret
< 0) ? (ret
= 0) : (ret
= 1);
436 int linux_spi_write_read(unsigned char *wb
, unsigned char *rb
, unsigned int rlen
)
442 struct spi_message msg
;
443 struct spi_transfer tr
= {
453 memset(&msg
, 0, sizeof(msg
));
454 spi_message_init(&msg
);
455 msg
.spi
= wilc_spi_dev
;
456 msg
.is_dma_mapped
= USE_SPI_DMA
;
458 spi_message_add_tail(&tr
, &msg
);
459 ret
= spi_sync(wilc_spi_dev
, &msg
);
461 PRINT_ER("SPI transaction failed\n");
464 PRINT_ER("can't read data with the following length: %d\n", rlen
);
467 /* change return value to match WILC interface */
468 (ret
< 0) ? (ret
= 0) : (ret
= 1);
473 int linux_spi_set_max_speed(void)
477 PRINT_INFO(BUS_DBG
, "@@@@@@@@@@@@ change SPI speed to %d @@@@@@@@@\n", SPEED
);