123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- /*****************************************************************************
- Copyright(c) 2013 FCI Inc. All Rights Reserved
- File name : fc8300_spi.c
- Description : source of SPI interface
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- History :
- ----------------------------------------------------------------------
- *******************************************************************************/
- #include <linux/spi/spi.h>
- #include <linux/slab.h>
- #include <linux/module.h>
- #include <linux/kernel.h>
- #include "fci_types.h"
- #include "fc8300_regs.h"
- #include "fci_oal.h"
- //#include "isdbt_port_fc8300.h"
- #define SPI_LEN 0x00 /* or 0x10 */
- #define SPI_REG 0x20
- #define SPI_THR 0x30
- #define SPI_READ 0x40
- #define SPI_WRITE 0x00
- #define SPI_AINC 0x80
- //#define DRIVER_NAME "isdbt"
- struct spi_device *fc8300_spi;
- static u8 tx_data[10];
- static u8 wdata_buf[32] __cacheline_aligned;
- static u8 rdata_buf[65536] __cacheline_aligned;
- static DEFINE_MUTEX(fci_spi_lock);
- void fc8300_set_port_if(unsigned long interface)
- {
- fc8300_spi = (struct spi_device *)interface;
- print_log(0, "fc8300_spi : 0x%x\n", fc8300_spi);
- }
- static int fc8300_spi_write_then_read(struct spi_device *spi
- , u8 *txbuf, u16 tx_length, u8 *rxbuf, u16 rx_length)
- {
- int res = 0;
- struct spi_message message;
- struct spi_transfer x;
- if (spi == NULL) {
- print_log(0, "[ERROR] FC8300_SPI Handle Fail...........\n");
- return BBM_NOK;
- }
- spi_message_init(&message);
- memset(&x, 0, sizeof x);
- spi_message_add_tail(&x, &message);
- memcpy(&wdata_buf[0], txbuf, tx_length);
- x.tx_buf = &wdata_buf[0];
- x.rx_buf = &rdata_buf[0];
- 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 s32 spi_bulkread(HANDLE handle, u8 devid,
- 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 | devid;
- tx_data[3] = length & 0xff;
- res = fc8300_spi_write_then_read(fc8300_spi
- , &tx_data[0], 4, data, length);
- if (res) {
- print_log(0, "fc8300_spi_bulkread fail : %d\n", res);
- return BBM_NOK;
- }
- return res;
- }
- static s32 spi_bulkwrite(HANDLE handle, u8 devid,
- 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 | devid;
- tx_data[3] = length & 0xff;
- for (i = 0 ; i < length ; i++)
- tx_data[4+i] = data[i];
- res = fc8300_spi_write_then_read(fc8300_spi
- , &tx_data[0], length+4, NULL, 0);
- if (res) {
- print_log(0, "fc8300_spi_bulkwrite fail : %d\n", res);
- return BBM_NOK;
- }
- return res;
- }
- static s32 spi_dataread(HANDLE handle, u8 devid,
- 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 | devid;
- tx_data[3] = length & 0xff;
- res = fc8300_spi_write_then_read(fc8300_spi
- , &tx_data[0], 4, data, length);
- if (res) {
- print_log(0, "fc8300_spi_dataread fail : %d\n", res);
- return BBM_NOK;
- }
- return res;
- }
- s32 fc8300_spi_init(HANDLE handle, u16 param1, u16 param2)
- {
- print_log(0, "fc8300_spi_init\n");
- if(fc8300_spi == NULL) {
- print_log(0, "fc8300_spi register fail\n");
- return BBM_NOK;
- }
- return 0;
- }
- s32 fc8300_spi_byteread(HANDLE handle, DEVICEID devid, u16 addr, u8 *data)
- {
- s32 res;
- u8 command = SPI_READ;
- mutex_lock(&fci_spi_lock);
- res = spi_bulkread(handle, (u8) (devid & 0x000f), addr, command,
- data, 1);
- mutex_unlock(&fci_spi_lock);
- return res;
- }
- s32 fc8300_spi_wordread(HANDLE handle, DEVICEID devid, u16 addr, u16 *data)
- {
- s32 res;
- u8 command = SPI_READ | SPI_AINC;
- mutex_lock(&fci_spi_lock);
- res = spi_bulkread(handle, (u8) (devid & 0x000f), addr, command,
- (u8 *)data, 2);
- mutex_unlock(&fci_spi_lock);
- return res;
- }
- s32 fc8300_spi_longread(HANDLE handle, DEVICEID devid, u16 addr, u32 *data)
- {
- s32 res;
- u8 command = SPI_READ | SPI_AINC;
- mutex_lock(&fci_spi_lock);
- res = spi_bulkread(handle, (u8) (devid & 0x000f), addr, command,
- (u8 *)data, 4);
- mutex_unlock(&fci_spi_lock);
- return res;
- }
- s32 fc8300_spi_bulkread(HANDLE handle, DEVICEID devid,
- u16 addr, u8 *data, u16 length)
- {
- s32 res;
- u8 command = SPI_READ | SPI_AINC;
- mutex_lock(&fci_spi_lock);
- res = spi_bulkread(handle, (u8) (devid & 0x000f), addr, command,
- data, length);
- mutex_unlock(&fci_spi_lock);
- return res;
- }
- s32 fc8300_spi_bytewrite(HANDLE handle, DEVICEID devid, u16 addr, u8 data)
- {
- s32 res;
- u8 command = SPI_WRITE;
- mutex_lock(&fci_spi_lock);
- res = spi_bulkwrite(handle, (u8) (devid & 0x000f), addr, command,
- (u8 *)&data, 1);
- mutex_unlock(&fci_spi_lock);
- return res;
- }
- s32 fc8300_spi_wordwrite(HANDLE handle, DEVICEID devid, u16 addr, u16 data)
- {
- s32 res;
- u8 command = SPI_WRITE | SPI_AINC;
- mutex_lock(&fci_spi_lock);
- res = spi_bulkwrite(handle, (u8) (devid & 0x000f), addr, command,
- (u8 *)&data, 2);
- mutex_unlock(&fci_spi_lock);
- return res;
- }
- s32 fc8300_spi_longwrite(HANDLE handle, DEVICEID devid, u16 addr, u32 data)
- {
- s32 res;
- u8 command = SPI_WRITE | SPI_AINC;
- mutex_lock(&fci_spi_lock);
- res = spi_bulkwrite(handle, (u8) (devid & 0x000f), addr, command,
- (u8 *) &data, 4);
- mutex_unlock(&fci_spi_lock);
- return res;
- }
- s32 fc8300_spi_bulkwrite(HANDLE handle, DEVICEID devid,
- u16 addr, u8 *data, u16 length)
- {
- s32 res;
- u8 command = SPI_WRITE | SPI_AINC;
- mutex_lock(&fci_spi_lock);
- res = spi_bulkwrite(handle, (u8) (devid & 0x000f), addr, command,
- data, length);
- mutex_unlock(&fci_spi_lock);
- return res;
- }
- s32 fc8300_spi_dataread(HANDLE handle, DEVICEID devid,
- u16 addr, u8 *data, u32 length)
- {
- s32 res;
- u8 command = SPI_READ | SPI_THR;
- mutex_lock(&fci_spi_lock);
- res = spi_dataread(handle, (u8) (devid & 0x000f), addr, command,
- data, length);
- mutex_unlock(&fci_spi_lock);
- return res;
- }
- s32 fc8300_spi_deinit(HANDLE handle)
- {
- return BBM_OK;
- }
|