Path: blob/master/arch/mips/pmc-sierra/yosemite/atmel_read_eeprom.c
15118 views
/*1* Copyright (C) 2003 PMC-Sierra Inc.2* Author: Manish Lachwani ([email protected])3*4* This program is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License as published by the6* Free Software Foundation; either version 2 of the License, or (at your7* option) any later version.8*9* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED10* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF11* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN12* NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,13* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT14* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF15* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON16* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT17* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF18* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.19*20* You should have received a copy of the GNU General Public License along21* with this program; if not, write to the Free Software Foundation, Inc.,22* 675 Mass Ave, Cambridge, MA 02139, USA.23*/2425/*26* Description:27*28* This code reads the ATMEL 24CXX EEPROM. The PMC-Sierra Yosemite board uses the ATMEL29* 24C32/24C64 which uses two byte addressing as compared to 24C16. Note that this program30* uses the serial port like /dev/ttyS0, to communicate with the EEPROM. Hence, you are31* expected to have a connectivity from the EEPROM to the serial port. This program does32* __not__ communicate using the I2C protocol33*/3435#include "atmel_read_eeprom.h"3637static void delay(int delay)38{39while (delay--);40}4142static void send_bit(unsigned char bit)43{44scl_lo;45delay(TXX);46if (bit)47sda_hi;48else49sda_lo;5051delay(TXX);52scl_hi;53delay(TXX);54}5556static void send_ack(void)57{58send_bit(0);59}6061static void send_byte(unsigned char byte)62{63int i = 0;6465for (i = 7; i >= 0; i--)66send_bit((byte >> i) & 0x01);67}6869static void send_start(void)70{71sda_hi;72delay(TXX);73scl_hi;74delay(TXX);75sda_lo;76delay(TXX);77}7879static void send_stop(void)80{81sda_lo;82delay(TXX);83scl_hi;84delay(TXX);85sda_hi;86delay(TXX);87}8889static void do_idle(void)90{91sda_hi;92scl_hi;93vcc_off;94}9596static int recv_bit(void)97{98int status;99100scl_lo;101delay(TXX);102sda_hi;103delay(TXX);104scl_hi;105delay(TXX);106107return 1;108}109110static unsigned char recv_byte(void) {111int i;112unsigned char byte=0;113114for (i=7;i>=0;i--)115byte |= (recv_bit() << i);116117return byte;118}119120static int recv_ack(void)121{122unsigned int ack;123124ack = (unsigned int)recv_bit();125scl_lo;126127if (ack) {128do_idle();129printk(KERN_ERR "Error reading the Atmel 24C32/24C64 EEPROM\n");130return -1;131}132133return ack;134}135136/*137* This function does the actual read of the EEPROM. It needs the buffer into which the138* read data is copied, the size of the EEPROM being read and the buffer size139*/140int read_eeprom(char *buffer, int eeprom_size, int size)141{142int i = 0, err;143144send_start();145send_byte(W_HEADER);146recv_ack();147148/* EEPROM with size of more than 2K need two byte addressing */149if (eeprom_size > 2048) {150send_byte(0x00);151recv_ack();152}153154send_start();155send_byte(R_HEADER);156err = recv_ack();157if (err == -1)158return err;159160for (i = 0; i < size; i++) {161*buffer++ = recv_byte();162send_ack();163}164165/* Note : We should do some check if the buffer contains correct information */166167send_stop();168}169170171