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 <linux/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 u32 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(TXRX_PHASE_SIZE)
118 int linux_spi_write(u8 *b, u32 len)
122 if (len > 0 && b != NULL) {
124 int blk = len / TXRX_PHASE_SIZE;
125 int remainder = len % TXRX_PHASE_SIZE;
127 char *r_buffer = kzalloc(TXRX_PHASE_SIZE, GFP_KERNEL);
130 PRINT_ER("Failed to allocate memory for r_buffer\n");
135 struct spi_message msg;
136 struct spi_transfer tr = {
137 .tx_buf = b + (i * TXRX_PHASE_SIZE),
138 .len = TXRX_PHASE_SIZE,
144 tr.rx_buf = r_buffer;
146 memset(&msg, 0, sizeof(msg));
147 spi_message_init(&msg);
148 msg.spi = wilc_spi_dev;
149 msg.is_dma_mapped = USE_SPI_DMA;
151 spi_message_add_tail(&tr, &msg);
152 ret = spi_sync(wilc_spi_dev, &msg);
154 PRINT_ER("SPI transaction failed\n");
161 struct spi_message msg;
162 struct spi_transfer tr = {
163 .tx_buf = b + (blk * TXRX_PHASE_SIZE),
169 tr.rx_buf = r_buffer;
171 memset(&msg, 0, sizeof(msg));
172 spi_message_init(&msg);
173 msg.spi = wilc_spi_dev;
174 msg.is_dma_mapped = USE_SPI_DMA; /* rachel */
176 spi_message_add_tail(&tr, &msg);
177 ret = spi_sync(wilc_spi_dev, &msg);
179 PRINT_ER("SPI transaction failed\n");
184 PRINT_ER("can't write data with the following length: %d\n", len);
185 PRINT_ER("FAILED due to NULL buffer or ZERO length check the following length: %d\n", len);
189 /* change return value to match WILC interface */
190 (ret < 0) ? (ret = 0) : (ret = 1);
197 int linux_spi_write(u8 *b, u32 len)
201 struct spi_message msg;
203 if (len > 0 && b != NULL) {
204 struct spi_transfer tr = {
210 char *r_buffer = kzalloc(len, GFP_KERNEL);
213 PRINT_ER("Failed to allocate memory for r_buffer\n");
215 tr.rx_buf = r_buffer;
216 PRINT_D(BUS_DBG, "Request writing %d bytes\n", len);
218 memset(&msg, 0, sizeof(msg));
219 spi_message_init(&msg);
221 msg.spi = wilc_spi_dev;
222 msg.is_dma_mapped = USE_SPI_DMA;
224 spi_message_add_tail(&tr, &msg);
226 ret = spi_sync(wilc_spi_dev, &msg);
228 PRINT_ER("SPI transaction failed\n");
233 PRINT_ER("can't write data with the following length: %d\n", len);
234 PRINT_ER("FAILED due to NULL buffer or ZERO length check the following length: %d\n", len);
238 /* change return value to match WILC interface */
239 (ret < 0) ? (ret = 0) : (ret = 1);
247 #if defined(TXRX_PHASE_SIZE)
249 int linux_spi_read(unsigned char *rb, unsigned long rlen)
256 int blk = rlen / TXRX_PHASE_SIZE;
257 int remainder = rlen % TXRX_PHASE_SIZE;
259 char *t_buffer = kzalloc(TXRX_PHASE_SIZE, GFP_KERNEL);
262 PRINT_ER("Failed to allocate memory for t_buffer\n");
267 struct spi_message msg;
268 struct spi_transfer tr = {
269 .rx_buf = rb + (i * TXRX_PHASE_SIZE),
270 .len = TXRX_PHASE_SIZE,
275 tr.tx_buf = t_buffer;
277 memset(&msg, 0, sizeof(msg));
278 spi_message_init(&msg);
279 msg.spi = wilc_spi_dev;
280 msg.is_dma_mapped = USE_SPI_DMA;
282 spi_message_add_tail(&tr, &msg);
283 ret = spi_sync(wilc_spi_dev, &msg);
285 PRINT_ER("SPI transaction failed\n");
291 struct spi_message msg;
292 struct spi_transfer tr = {
293 .rx_buf = rb + (blk * TXRX_PHASE_SIZE),
299 tr.tx_buf = t_buffer;
301 memset(&msg, 0, sizeof(msg));
302 spi_message_init(&msg);
303 msg.spi = wilc_spi_dev;
304 msg.is_dma_mapped = USE_SPI_DMA; /* rachel */
306 spi_message_add_tail(&tr, &msg);
307 ret = spi_sync(wilc_spi_dev, &msg);
309 PRINT_ER("SPI transaction failed\n");
315 PRINT_ER("can't read data with the following length: %ld\n", rlen);
318 /* change return value to match WILC interface */
319 (ret < 0) ? (ret = 0) : (ret = 1);
325 int linux_spi_read(unsigned char *rb, unsigned long rlen)
331 struct spi_message msg;
332 struct spi_transfer tr = {
339 char *t_buffer = kzalloc(rlen, GFP_KERNEL);
342 PRINT_ER("Failed to allocate memory for t_buffer\n");
344 tr.tx_buf = t_buffer;
346 memset(&msg, 0, sizeof(msg));
347 spi_message_init(&msg);
349 msg.spi = wilc_spi_dev;
350 msg.is_dma_mapped = USE_SPI_DMA;
352 spi_message_add_tail(&tr, &msg);
354 ret = spi_sync(wilc_spi_dev, &msg);
356 PRINT_ER("SPI transaction failed\n");
360 PRINT_ER("can't read data with the following length: %ld\n", rlen);
363 /* change return value to match WILC interface */
364 (ret < 0) ? (ret = 0) : (ret = 1);
371 int linux_spi_write_read(unsigned char *wb, unsigned char *rb, unsigned int rlen)
377 struct spi_message msg;
378 struct spi_transfer tr = {
388 memset(&msg, 0, sizeof(msg));
389 spi_message_init(&msg);
390 msg.spi = wilc_spi_dev;
391 msg.is_dma_mapped = USE_SPI_DMA;
393 spi_message_add_tail(&tr, &msg);
394 ret = spi_sync(wilc_spi_dev, &msg);
396 PRINT_ER("SPI transaction failed\n");
399 PRINT_ER("can't read data with the following length: %d\n", rlen);
402 /* change return value to match WILC interface */
403 (ret < 0) ? (ret = 0) : (ret = 1);
408 int linux_spi_set_max_speed(void)
412 PRINT_INFO(BUS_DBG, "@@@@@@@@@@@@ change SPI speed to %d @@@@@@@@@\n", SPEED);