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");
220 PRINT_ER("can't write data with the following length: %d\n", len);
221 PRINT_ER("FAILED due to NULL buffer or ZERO length check the following length: %d\n", len);
225 /* change return value to match WILC interface */
226 (ret < 0) ? (ret = 0) : (ret = 1);
233 int linux_spi_write(uint8_t *b, uint32_t len)
237 struct spi_message msg;
239 if (len > 0 && b != NULL) {
240 struct spi_transfer tr = {
246 char *r_buffer = kzalloc(len, GFP_KERNEL);
248 PRINT_ER("Failed to allocate memory for r_buffer\n");
250 tr.rx_buf = r_buffer;
251 PRINT_D(BUS_DBG, "Request writing %d bytes\n", len);
253 memset(&msg, 0, sizeof(msg));
254 spi_message_init(&msg);
256 msg.spi = wilc_spi_dev;
257 msg.is_dma_mapped = USE_SPI_DMA;
259 spi_message_add_tail(&tr, &msg);
261 ret = spi_sync(wilc_spi_dev, &msg);
263 PRINT_ER("SPI transaction failed\n");
268 PRINT_ER("can't write data with the following length: %d\n", len);
269 PRINT_ER("FAILED due to NULL buffer or ZERO length check the following length: %d\n", len);
273 /* change return value to match WILC interface */
274 (ret < 0) ? (ret = 0) : (ret = 1);
282 #if defined (NM73131_0_BOARD)
284 int linux_spi_read(unsigned char *rb, unsigned long rlen)
290 struct spi_message msg;
291 struct spi_transfer tr = {
299 spi_message_init(&msg);
300 spi_message_add_tail(&tr, &msg);
301 ret = spi_sync(wilc_spi_dev, &msg);
303 PRINT_ER("SPI transaction failed\n");
306 PRINT_ER("can't read data with the following length: %ld\n", rlen);
309 /* change return value to match WILC interface */
310 (ret < 0) ? (ret = 0) : (ret = 1);
315 #elif defined(TXRX_PHASE_SIZE)
317 int linux_spi_read(unsigned char *rb, unsigned long rlen)
324 int blk = rlen / TXRX_PHASE_SIZE;
325 int remainder = rlen % TXRX_PHASE_SIZE;
327 char *t_buffer = kzalloc(TXRX_PHASE_SIZE, GFP_KERNEL);
329 PRINT_ER("Failed to allocate memory for t_buffer\n");
334 struct spi_message msg;
335 struct spi_transfer tr = {
336 .rx_buf = rb + (i * TXRX_PHASE_SIZE),
337 .len = TXRX_PHASE_SIZE,
342 tr.tx_buf = t_buffer;
344 memset(&msg, 0, sizeof(msg));
345 spi_message_init(&msg);
346 msg.spi = wilc_spi_dev;
347 msg.is_dma_mapped = USE_SPI_DMA;
349 spi_message_add_tail(&tr, &msg);
350 ret = spi_sync(wilc_spi_dev, &msg);
352 PRINT_ER("SPI transaction failed\n");
358 struct spi_message msg;
359 struct spi_transfer tr = {
360 .rx_buf = rb + (blk * TXRX_PHASE_SIZE),
366 tr.tx_buf = t_buffer;
368 memset(&msg, 0, sizeof(msg));
369 spi_message_init(&msg);
370 msg.spi = wilc_spi_dev;
371 msg.is_dma_mapped = USE_SPI_DMA; /* rachel */
373 spi_message_add_tail(&tr, &msg);
374 ret = spi_sync(wilc_spi_dev, &msg);
376 PRINT_ER("SPI transaction failed\n");
383 PRINT_ER("can't read data with the following length: %ld\n", rlen);
386 /* change return value to match WILC interface */
387 (ret < 0) ? (ret = 0) : (ret = 1);
393 int linux_spi_read(unsigned char *rb, unsigned long rlen)
399 struct spi_message msg;
400 struct spi_transfer tr = {
407 char *t_buffer = kzalloc(rlen, GFP_KERNEL);
409 PRINT_ER("Failed to allocate memory for t_buffer\n");
411 tr.tx_buf = t_buffer;
413 memset(&msg, 0, sizeof(msg));
414 spi_message_init(&msg);
416 msg.spi = wilc_spi_dev;
417 msg.is_dma_mapped = USE_SPI_DMA;
419 spi_message_add_tail(&tr, &msg);
421 ret = spi_sync(wilc_spi_dev, &msg);
423 PRINT_ER("SPI transaction failed\n");
427 PRINT_ER("can't read data with the following length: %ld\n", rlen);
430 /* change return value to match WILC interface */
431 (ret < 0) ? (ret = 0) : (ret = 1);
438 int linux_spi_write_read(unsigned char *wb, unsigned char *rb, unsigned int rlen)
444 struct spi_message msg;
445 struct spi_transfer tr = {
455 memset(&msg, 0, sizeof(msg));
456 spi_message_init(&msg);
457 msg.spi = wilc_spi_dev;
458 msg.is_dma_mapped = USE_SPI_DMA;
460 spi_message_add_tail(&tr, &msg);
461 ret = spi_sync(wilc_spi_dev, &msg);
463 PRINT_ER("SPI transaction failed\n");
466 PRINT_ER("can't read data with the following length: %d\n", rlen);
469 /* change return value to match WILC interface */
470 (ret < 0) ? (ret = 0) : (ret = 1);
475 int linux_spi_set_max_speed(void)
479 PRINT_INFO(BUS_DBG, "@@@@@@@@@@@@ change SPI speed to %d @@@@@@@@@\n", SPEED);