123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- /*****************************************************************************
- Copyright(c) 2012 FCI Inc. All Rights Reserved
- File name : fc8150_spi.c
- Description : fc8150 host interface
- *******************************************************************************/
- #include <linux/module.h>
- #include <linux/spi/spi.h>
- #include <linux/slab.h>
- #include "fci_types.h"
- #include "fc8150_regs.h"
- #include "fci_oal.h"
- #define SPI_BMODE 0x00
- #define SPI_WMODE 0x10
- #define SPI_LMODE 0x20
- #define SPI_READ 0x40
- #define SPI_WRITE 0x00
- #define SPI_AINC 0x80
- #define CHIPID (0 << 3)
- #define DRIVER_NAME "fc8150_spi"
- #define DMA_OFFSET_ALIGN 64
- struct spi_device *fc8150_spi;
- static u8 tx_data[10];
- static u8 rdata_buf[8192];
- static u8 wdata_buf[8192];
- static DEFINE_MUTEX(lock);
- static int __devinit fc8150_spi_probe(struct spi_device *spi)
- {
- s32 ret;
- PRINTF(0, "(SJINU) fc8150_spi_probe\n");
- spi->max_speed_hz = 24000000;
- spi->bits_per_word = 8;
- spi->mode = SPI_MODE_0;
- ret = spi_setup(spi);
- if (ret < 0)
- return ret;
- fc8150_spi = spi;
- return ret;
- }
- static int fc8150_spi_remove(struct spi_device *spi)
- {
- return 0;
- }
- static struct spi_driver fc8150_spi_driver = {
- .driver = {
- .name = DRIVER_NAME,
- .owner = THIS_MODULE,
- },
- .probe = fc8150_spi_probe,
- .remove = __devexit_p(fc8150_spi_remove),
- };
- static int fc8150_spi_write_then_read(struct spi_device *spi
- , u8 *txbuf, u16 tx_length, u8 *rxbuf, u16 rx_length)
- {
- int res = 0;
- int offsetRx =0, offsetTx = 0;
- struct spi_message message;
- struct spi_transfer x;
- spi_message_init(&message);
- memset(&x, 0, sizeof x);
- spi_message_add_tail(&x, &message);
- offsetRx = DMA_OFFSET_ALIGN - (((unsigned int)&rdata_buf[0]) % DMA_OFFSET_ALIGN);
- offsetTx = DMA_OFFSET_ALIGN - (((unsigned int)&wdata_buf[0]) % DMA_OFFSET_ALIGN);
- memcpy(&wdata_buf[offsetTx], txbuf, tx_length);
- x.tx_buf = &wdata_buf[offsetTx];
- x.rx_buf = &rdata_buf[offsetRx];
- // PRINTF(0, "(SJINU %s) wdatabuf = %x, rdatabuf = %x\n", __func__, &wdata_buf[offsetTx], &rdata_buf[offsetRx]);
- x.len = tx_length + rx_length;
- x.cs_change = 0;
- x.bits_per_word = 8;
- res = spi_sync(spi, &message);
- memcpy(rxbuf, x.rx_buf + tx_length, rx_length);
- return res;
- }
- static int spi_bulkread(HANDLE hDevice, u16 addr
- , u8 command, u8 *data, u16 length)
- {
- int res;
- tx_data[0] = addr & 0xff;
- tx_data[1] = (addr >> 8) & 0xff;
- tx_data[2] = (command & 0xf0) | CHIPID | ((length >> 16) & 0x07);
- tx_data[3] = (length >> 8) & 0xff;
- tx_data[4] = length & 0xff;
- res = fc8150_spi_write_then_read(fc8150_spi, &tx_data[0]
- , 5, data, length);
- if (res) {
- PRINTF(0, "fc8150_spi_bulkread fail : %d\n", res);
- return BBM_NOK;
- }
- return BBM_OK;
- }
- static int spi_bulkwrite(HANDLE hDevice, u16 addr
- , u8 command, u8 *data, u16 length)
- {
- int i;
- int res;
- tx_data[0] = addr & 0xff;
- tx_data[1] = (addr >> 8) & 0xff;
- tx_data[2] = (command & 0xf0) | CHIPID | ((length >> 16) & 0x07);
- tx_data[3] = (length >> 8) & 0xff;
- tx_data[4] = length & 0xff;
- for (i = 0; i < length; i++)
- tx_data[5+i] = data[i];
- res = fc8150_spi_write_then_read(fc8150_spi
- , &tx_data[0], length+5, NULL, 0);
- if (res) {
- PRINTF(0, "fc8150_spi_bulkwrite fail : %d\n", res);
- return BBM_NOK;
- }
- return BBM_OK;
- }
- static int spi_dataread(HANDLE hDevice, u16 addr
- , u8 command, u8 *data, u32 length)
- {
- int res;
- tx_data[0] = addr & 0xff;
- tx_data[1] = (addr >> 8) & 0xff;
- tx_data[2] = (command & 0xf0) | CHIPID | ((length >> 16) & 0x07);
- tx_data[3] = (length >> 8) & 0xff;
- tx_data[4] = length & 0xff;
- res = fc8150_spi_write_then_read(fc8150_spi
- , &tx_data[0], 5, data, length);
- if (res) {
- PRINTF(0, "(SJINU) fc8150_spi_dataread fail : %d\n", res);
- return BBM_NOK;
- }
- return BBM_OK;
- }
- int fc8150_spi_init(HANDLE hDevice, u16 param1, u16 param2)
- {
- int res = 0;
- PRINTF(0, "fc8150_spi_init : %d\n", res);
- res = spi_register_driver(&fc8150_spi_driver);
- if (res) {
- PRINTF(0, "fc8150_spi register fail : %d\n", res);
- return BBM_NOK;
- }
- return res;
- }
- int fc8150_spi_byteread(HANDLE hDevice, u16 addr, u8 *data)
- {
- int res;
- u8 command = SPI_READ;
- mutex_lock(&lock);
- res = spi_bulkread(hDevice, addr, command, data, 1);
- mutex_unlock(&lock);
- return res;
- }
- int fc8150_spi_wordread(HANDLE hDevice, u16 addr, u16 *data)
- {
- int res;
- u8 command = SPI_READ | SPI_AINC;
- mutex_lock(&lock);
- res = spi_bulkread(hDevice, addr, command, (u8 *)data, 2);
- mutex_unlock(&lock);
- // PRINTF(0, "(SJINU) fc8150_spi_wordread : value=%x res=%d\n", *data, res);
- return res;
- }
- int fc8150_spi_longread(HANDLE hDevice, u16 addr, u32 *data)
- {
- int res;
- u8 command = SPI_READ | SPI_AINC;
- mutex_lock(&lock);
- res = spi_bulkread(hDevice, addr, command, (u8 *)data, 4);
- mutex_unlock(&lock);
- return res;
- }
- int fc8150_spi_bulkread(HANDLE hDevice, u16 addr, u8 *data, u16 length)
- {
- int res;
- u8 command = SPI_READ | SPI_AINC;
- mutex_lock(&lock);
- res = spi_bulkread(hDevice, addr, command, data, length);
- mutex_unlock(&lock);
- return res;
- }
- int fc8150_spi_bytewrite(HANDLE hDevice, u16 addr, u8 data)
- {
- int res;
- u8 command = SPI_WRITE;
- mutex_lock(&lock);
- res = spi_bulkwrite(hDevice, addr, command, (u8 *)&data, 1);
- mutex_unlock(&lock);
- return res;
- }
- int fc8150_spi_wordwrite(HANDLE hDevice, u16 addr, u16 data)
- {
- int res;
- u8 command = SPI_WRITE | SPI_AINC;
- mutex_lock(&lock);
- res = spi_bulkwrite(hDevice, addr, command, (u8 *)&data, 2);
- mutex_unlock(&lock);
- // PRINTF(0, "(SJINU) fc8150_spi_wordwrite : %d\n", res);
- return res;
- }
- int fc8150_spi_longwrite(HANDLE hDevice, u16 addr, u32 data)
- {
- int res;
- u8 command = SPI_WRITE | SPI_AINC;
- mutex_lock(&lock);
- res = spi_bulkwrite(hDevice, addr, command, (u8 *)&data, 4);
- mutex_unlock(&lock);
- return res;
- }
- int fc8150_spi_bulkwrite(HANDLE hDevice, u16 addr, u8 *data, u16 length)
- {
- int res;
- u8 command = SPI_WRITE | SPI_AINC;
- mutex_lock(&lock);
- res = spi_bulkwrite(hDevice, addr, command, data, length);
- mutex_unlock(&lock);
- return res;
- }
- int fc8150_spi_dataread(HANDLE hDevice, u16 addr, u8 *data, u32 length)
- {
- int res;
- u8 command = SPI_READ;
- // PRINTF(0, "(SJINU) %s entered\n", __func__);
- mutex_lock(&lock);
- res = spi_dataread(hDevice, addr, command, data, length);
- mutex_unlock(&lock);
- return res;
- }
- int fc8150_spi_deinit(HANDLE hDevice)
- {
- return BBM_OK;
- }
|