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)
60 /* linux_spi_deinit(NULL); */
66 static const struct of_device_id wilc1000_of_match[] = {
67 { .compatible = "atmel,wilc_spi", },
70 MODULE_DEVICE_TABLE(of, wilc1000_of_match);
73 struct spi_driver wilc_bus __refdata = {
77 .of_match_table = wilc1000_of_match,
80 .probe = wilc_bus_probe,
81 .remove = __exit_p(wilc_bus_remove),
85 void linux_spi_deinit(void *vp)
88 spi_unregister_driver(&wilc_bus);
91 PRINT_ER("@@@@@@@@@@@@ restore SPI speed to %d @@@@@@@@@\n", SPEED);
97 int linux_spi_init(void *vp)
105 ret = spi_register_driver(&wilc_bus);
108 /* change return value to match WILC interface */
109 (ret < 0) ? (ret = 0) : (ret = 1);
114 #if defined(PLAT_WMS8304)
115 #define TXRX_PHASE_SIZE (4096)
118 #if defined (NM73131_0_BOARD)
120 int linux_spi_write(uint8_t *b, uint32_t len)
125 if (len > 0 && b != NULL) {
126 struct spi_message msg;
127 PRINT_D(BUS_DBG, "Request writing %d bytes\n", len);
128 struct spi_transfer tr = {
135 spi_message_init(&msg);
136 spi_message_add_tail(&tr, &msg);
137 ret = spi_sync(wilc_spi_dev, &msg);
139 PRINT_ER("SPI transaction failed\n");
143 PRINT_ER("can't write data with the following length: %d\n", len);
144 PRINT_ER("FAILED due to NULL buffer or ZERO length check the following length: %d\n", len);
148 /* change return value to match WILC interface */
149 (ret < 0) ? (ret = 0) : (ret = 1);
155 #elif defined(TXRX_PHASE_SIZE)
157 int linux_spi_write(uint8_t *b, uint32_t len)
160 if (len > 0 && b != NULL) {
162 int blk = len / TXRX_PHASE_SIZE;
163 int remainder = len % TXRX_PHASE_SIZE;
165 char *r_buffer = kzalloc(TXRX_PHASE_SIZE, GFP_KERNEL);
167 PRINT_ER("Failed to allocate memory for r_buffer\n");
172 struct spi_message msg;
173 struct spi_transfer tr = {
174 .tx_buf = b + (i * TXRX_PHASE_SIZE),
175 /* .rx_buf = NULL, */
176 .len = TXRX_PHASE_SIZE,
182 * char *r_buffer = (char*) kzalloc(TXRX_PHASE_SIZE, GFP_KERNEL);
184 * PRINT_ER("Failed to allocate memory for r_buffer\n");
187 tr.rx_buf = r_buffer;
189 memset(&msg, 0, sizeof(msg));
190 spi_message_init(&msg);
191 msg.spi = wilc_spi_dev;
192 msg.is_dma_mapped = USE_SPI_DMA;
194 spi_message_add_tail(&tr, &msg);
195 ret = spi_sync(wilc_spi_dev, &msg);
197 PRINT_ER("SPI transaction failed\n");
199 /* i += MJ_WRITE_SIZE; */
205 struct spi_message msg;
206 struct spi_transfer tr = {
207 .tx_buf = b + (blk * TXRX_PHASE_SIZE),
208 /* .rx_buf = NULL, */
215 * char *r_buffer = (char*) kzalloc(remainder, GFP_KERNEL);
217 * PRINT_ER("Failed to allocate memory for r_buffer\n");
220 tr.rx_buf = r_buffer;
222 memset(&msg, 0, sizeof(msg));
223 spi_message_init(&msg);
224 msg.spi = wilc_spi_dev;
225 msg.is_dma_mapped = USE_SPI_DMA; /* rachel */
227 spi_message_add_tail(&tr, &msg);
228 ret = spi_sync(wilc_spi_dev, &msg);
230 PRINT_ER("SPI transaction failed\n");
236 PRINT_ER("can't write data with the following length: %d\n", len);
237 PRINT_ER("FAILED due to NULL buffer or ZERO length check the following length: %d\n", len);
241 /* change return value to match WILC interface */
242 (ret < 0) ? (ret = 0) : (ret = 1);
249 int linux_spi_write(uint8_t *b, uint32_t len)
253 struct spi_message msg;
255 if (len > 0 && b != NULL) {
256 struct spi_transfer tr = {
258 /* .rx_buf = r_buffer, */
263 char *r_buffer = kzalloc(len, GFP_KERNEL);
265 PRINT_ER("Failed to allocate memory for r_buffer\n");
267 tr.rx_buf = r_buffer;
268 PRINT_D(BUS_DBG, "Request writing %d bytes\n", len);
270 memset(&msg, 0, sizeof(msg));
271 spi_message_init(&msg);
273 msg.spi = wilc_spi_dev;
274 msg.is_dma_mapped = USE_SPI_DMA;
276 spi_message_add_tail(&tr, &msg);
278 ret = spi_sync(wilc_spi_dev, &msg);
280 PRINT_ER("SPI transaction failed\n");
285 PRINT_ER("can't write data with the following length: %d\n", len);
286 PRINT_ER("FAILED due to NULL buffer or ZERO length check the following length: %d\n", len);
290 /* change return value to match WILC interface */
291 (ret < 0) ? (ret = 0) : (ret = 1);
299 #if defined (NM73131_0_BOARD)
301 int linux_spi_read(unsigned char *rb, unsigned long rlen)
307 struct spi_message msg;
308 struct spi_transfer tr = {
316 spi_message_init(&msg);
317 spi_message_add_tail(&tr, &msg);
318 ret = spi_sync(wilc_spi_dev, &msg);
320 PRINT_ER("SPI transaction failed\n");
323 PRINT_ER("can't read data with the following length: %ld\n", rlen);
326 /* change return value to match WILC interface */
327 (ret < 0) ? (ret = 0) : (ret = 1);
332 #elif defined(TXRX_PHASE_SIZE)
334 int linux_spi_read(unsigned char *rb, unsigned long rlen)
341 int blk = rlen / TXRX_PHASE_SIZE;
342 int remainder = rlen % TXRX_PHASE_SIZE;
344 char *t_buffer = kzalloc(TXRX_PHASE_SIZE, GFP_KERNEL);
346 PRINT_ER("Failed to allocate memory for t_buffer\n");
351 struct spi_message msg;
352 struct spi_transfer tr = {
353 /* .tx_buf = NULL, */
354 .rx_buf = rb + (i * TXRX_PHASE_SIZE),
355 .len = TXRX_PHASE_SIZE,
360 tr.tx_buf = t_buffer;
362 memset(&msg, 0, sizeof(msg));
363 spi_message_init(&msg);
364 msg.spi = wilc_spi_dev;
365 msg.is_dma_mapped = USE_SPI_DMA;
367 spi_message_add_tail(&tr, &msg);
368 ret = spi_sync(wilc_spi_dev, &msg);
370 PRINT_ER("SPI transaction failed\n");
376 struct spi_message msg;
377 struct spi_transfer tr = {
378 /* .tx_buf = NULL, */
379 .rx_buf = rb + (blk * TXRX_PHASE_SIZE),
386 * char *t_buffer = (char*) kzalloc(remainder, GFP_KERNEL);
388 * PRINT_ER("Failed to allocate memory for t_buffer\n");
391 tr.tx_buf = t_buffer;
393 memset(&msg, 0, sizeof(msg));
394 spi_message_init(&msg);
395 msg.spi = wilc_spi_dev;
396 msg.is_dma_mapped = USE_SPI_DMA; /* rachel */
398 spi_message_add_tail(&tr, &msg);
399 ret = spi_sync(wilc_spi_dev, &msg);
401 PRINT_ER("SPI transaction failed\n");
408 PRINT_ER("can't read data with the following length: %ld\n", rlen);
411 /* change return value to match WILC interface */
412 (ret < 0) ? (ret = 0) : (ret = 1);
418 int linux_spi_read(unsigned char *rb, unsigned long rlen)
424 struct spi_message msg;
425 struct spi_transfer tr = {
426 /* .tx_buf = t_buffer, */
433 char *t_buffer = kzalloc(rlen, GFP_KERNEL);
435 PRINT_ER("Failed to allocate memory for t_buffer\n");
437 tr.tx_buf = t_buffer;
439 memset(&msg, 0, sizeof(msg));
440 spi_message_init(&msg);
442 msg.spi = wilc_spi_dev;
443 msg.is_dma_mapped = USE_SPI_DMA;
445 spi_message_add_tail(&tr, &msg);
447 ret = spi_sync(wilc_spi_dev, &msg);
449 PRINT_ER("SPI transaction failed\n");
453 PRINT_ER("can't read data with the following length: %ld\n", rlen);
456 /* change return value to match WILC interface */
457 (ret < 0) ? (ret = 0) : (ret = 1);
464 int linux_spi_write_read(unsigned char *wb, unsigned char *rb, unsigned int rlen)
470 struct spi_message msg;
471 struct spi_transfer tr = {
481 memset(&msg, 0, sizeof(msg));
482 spi_message_init(&msg);
483 msg.spi = wilc_spi_dev;
484 msg.is_dma_mapped = USE_SPI_DMA;
486 spi_message_add_tail(&tr, &msg);
487 ret = spi_sync(wilc_spi_dev, &msg);
489 PRINT_ER("SPI transaction failed\n");
492 PRINT_ER("can't read data with the following length: %d\n", rlen);
495 /* change return value to match WILC interface */
496 (ret < 0) ? (ret = 0) : (ret = 1);
501 int linux_spi_set_max_speed(void)
505 PRINT_INFO(BUS_DBG, "@@@@@@@@@@@@ change SPI speed to %d @@@@@@@@@\n", SPEED);