/* 2008-10-01: File added and changed by Sony Corporation */
/*
GPL LICENSE SUMMARY

Copyright(c) 2005-2008 Intel Corporation. All rights reserved.

This program is free software; you can redistribute it and/or modify
it under the terms of version 2 of the GNU General Public License as
published by the Free Software Foundation.

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., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
The full GNU General Public License is included in this distribution
in the file called LICENSE.GPL.

Contact Information:
Intel Corporation
2200 Mission College Blvd.
Santa Clara, CA 97052
*/

/* zds_nic_flash.h
 * Structures, enums, and macros for the MAC
 */

#ifndef _ZDS_NIC_FLASH_H_
#define _ZDS_NIC_FLASH_H_

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/pci.h>
#include <linux/pci_ids.h>
#include <linux/interrupt.h>
#include <linux/spinlock.h>
#include <linux/miscdevice.h>
#include <linux/mm.h>
#include <linux/vmalloc.h>
#include <linux/delay.h>
#include <linux/wait.h>
#include <asm/semaphore.h>
#include <asm/uaccess.h>
#include <linux/time.h>

extern	void __iomem * zds_flash_base_virt;
#define ZDS_FLASH_BASE_PHY		0xC0000000
#define ZDS_FLASH_SIZE			0x2000000
#define ZDS_FLASH_BASE_VIRT  	zds_flash_base_virt

#ifndef ZDS_FLASH_BASE_VIRT
#error  ZDS_FLASH_BASE_VIRT has not been defined!
#endif

#define ZDS_FLASH_BLOCK    		0x20000
#define ZDS_FLASH_BLOCK_MASK		0xfffe0000

/* Flash commands and status */
#define FLASH_BLOCK_ERASE       0x20
#define FLASH_PROGRAM           0x40
#define FLASH_UNLOCK            0x60
#define FLASH_STATUS_READY      0x80
#define FLASH_CMD_CONFIRM       0xD0
#define FLASH_READ_STATUS       0x70
#define FLASH_READ_ARRAY_CMD    0xFF
#define FLASH_READ_INFO         0x90
#define FLASH_PROGRAM_BUF       0xE8

#define MAX_BUFSIZE				32

#define ZDS_NIC_EEPROM_OFFSET	0x1F00000
#define ZDS_NIC_EEPROM_BASE		((unsigned int)(ZDS_FLASH_BASE_VIRT + ZDS_NIC_EEPROM_OFFSET))
#define ZDS_NIC_EEPROM_SIZE		0x200
#define ZDS_NIC_EEPROM_END       ((unsigned int)(ZDS_NIC_EEPROM_BASE + ZDS_NIC_EEPROM_SIZE - 1 ))
#define ZDS_NIC_FLASH_READ(base,offset,count,data)		flash_read_words(base,offset,count,data)
#define ZDS_NIC_FLASH_WRITE(base,offset,count,data)		flash_write_words(base,offset,count,data)

#define ZDS_PERROR(fmt, args...) printk("[ERROR] %s: " fmt, __FUNCTION__ , ## args)

static int inline flash_write_words(unsigned int flash_base_t, unsigned short offset_t, unsigned short count, unsigned short *buf_t)
{
	return 0;
}

/*  write a word  */
static int inline flash_read_one_word(unsigned int flash_base_t, unsigned short offset, unsigned short *buf)
{
    int ret = 0;
    volatile unsigned int flashBase;
	volatile unsigned short * src;

	if(!buf){
		ZDS_PERROR("Invalid buffer address!\n");
		return -EINVAL;
	}

	if( offset > 0x3f)
	{
		ZDS_PERROR("invalid block number: %x\n", offset);
		return -EINVAL;
	}

	// translate the address from eeporm address (in word) into the flash address (in byte);
	offset<<=1;

	flashBase = (volatile unsigned int)flash_base_t;
	src = (volatile unsigned short *)(flashBase + (unsigned int)offset);
	*buf = *src;

	return ret;
}

static int inline flash_read_words(unsigned int flash_base_t, unsigned short offset_t, unsigned short count, unsigned short *buf_t)
{
	int ret = 0 ;
	unsigned short i, offset, * buf;

	if( (count + offset_t - 1 ) > 0x3f){
		printk("Invalid words number, eeprom space limited to 0x3f word size\n");
	}

	offset = offset_t;
	buf = buf_t;

	for(i=0;i<count;i++){
		flash_read_one_word(flash_base_t, offset, buf);
		offset ++;
		buf ++;
	}

	return ret;
}

#endif /* _ZDS_NIC_FLASH_H_ */
