uboot/board/v38b/ethaddr.c
<<
>>
Prefs
   1/*
   2 * (C) Copyright 2006
   3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
   4 *
   5 * See file CREDITS for list of people who contributed to this
   6 * project.
   7 *
   8 * This program is free software; you can redistribute it and/or
   9 * modify it under the terms of the GNU General Public License as
  10 * published by the Free Software Foundation; either version 2 of
  11 * the License, or (at your option) any later version.
  12 *
  13 * This program is distributed in the hope that it will be useful,
  14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16 * GNU General Public License for more details.
  17 *
  18 * You should have received a copy of the GNU General Public License
  19 * along with this program; if not, write to the Free Software
  20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21 * MA 02111-1307 USA
  22 */
  23
  24#include <common.h>
  25#include <mpc5xxx.h>
  26
  27/* For the V38B board the pin is GPIO_PSC_6 */
  28#define GPIO_PIN        GPIO_PSC6_0
  29
  30#define NO_ERROR        0
  31#define ERR_NO_NUMBER   1
  32#define ERR_BAD_NUMBER  2
  33
  34static int is_high(void);
  35static int check_device(void);
  36static void io_out(int value);
  37static void io_input(void);
  38static void io_output(void);
  39static void init_gpio(void);
  40static void read_byte(unsigned char *data);
  41static void write_byte(unsigned char command);
  42
  43void read_2501_memory(unsigned char *psernum, unsigned char *perr);
  44void board_get_enetaddr(uchar *enetaddr);
  45
  46
  47static int is_high()
  48{
  49        return (*((vu_long *) MPC5XXX_WU_GPIO_DATA_I) & GPIO_PIN);
  50}
  51
  52static void io_out(int value)
  53{
  54        if (value)
  55                *((vu_long *) MPC5XXX_WU_GPIO_DATA_O) |= GPIO_PIN;
  56        else
  57                *((vu_long *) MPC5XXX_WU_GPIO_DATA_O) &= ~GPIO_PIN;
  58}
  59
  60static void io_input()
  61{
  62        *((vu_long *) MPC5XXX_WU_GPIO_DIR) &= ~GPIO_PIN;
  63        udelay(3);      /* allow input to settle */
  64}
  65
  66static void io_output()
  67{
  68        *((vu_long *) MPC5XXX_WU_GPIO_DIR) |= GPIO_PIN;
  69}
  70
  71static void init_gpio()
  72{
  73        *((vu_long *) MPC5XXX_WU_GPIO_ENABLE) |= GPIO_PIN;      /* Enable appropriate pin */
  74}
  75
  76void read_2501_memory(unsigned char *psernum, unsigned char *perr)
  77{
  78#define NBYTES 28
  79        unsigned char crcval, i;
  80        unsigned char buf[NBYTES];
  81
  82        *perr = 0;
  83        crcval = 0;
  84
  85        for (i = 0; i < NBYTES; i++)
  86                buf[i] = 0;
  87
  88        if (!check_device())
  89                *perr = ERR_NO_NUMBER;
  90        else {
  91                *perr = NO_ERROR;
  92                write_byte(0xCC);               /* skip ROM (0xCC) */
  93                write_byte(0xF0);               /* Read memory command 0xF0 */
  94                write_byte(0x00);               /* Address TA1=0, TA2=0 */
  95                write_byte(0x00);
  96                read_byte(&crcval);             /* Read CRC of address and command */
  97
  98                for (i = 0; i < NBYTES; i++)
  99                        read_byte(&buf[i]);
 100        }
 101        if (strncmp((const char *) &buf[11], "MAREL IEEE 802.3", 16)) {
 102                *perr = ERR_BAD_NUMBER;
 103                psernum[0] = 0x00;
 104                psernum[1] = 0xE0;
 105                psernum[2] = 0xEE;
 106                psernum[3] = 0xFF;
 107                psernum[4] = 0xFF;
 108                psernum[5] = 0xFF;
 109        } else {
 110                psernum[0] = 0x00;
 111                psernum[1] = 0xE0;
 112                psernum[2] = 0xEE;
 113                psernum[3] = buf[7];
 114                psernum[4] = buf[6];
 115                psernum[5] = buf[5];
 116        }
 117}
 118
 119static int check_device()
 120{
 121        int found;
 122
 123        io_output();
 124        io_out(0);
 125        udelay(500);  /* must be at least 480 us low pulse */
 126
 127        io_input();
 128        udelay(60);
 129
 130        found = (is_high() == 0) ? 1 : 0;
 131        udelay(500);  /* must be at least 480 us low pulse */
 132
 133        return found;
 134}
 135
 136static void write_byte(unsigned char command)
 137{
 138        char i;
 139
 140        for (i = 0; i < 8; i++) {
 141                /* 1 us to 15 us low pulse starts bit slot */
 142                /* Start with high pulse for 3 us */
 143                io_input();
 144                udelay(3);
 145
 146                io_out(0);
 147                io_output();
 148                udelay(3);
 149
 150                if (command & 0x01) {
 151                        /* 60 us high for 1-bit */
 152                        io_input();
 153                        udelay(60);
 154                } else
 155                        /* 60 us low for 0-bit */
 156                        udelay(60);
 157                /*  Leave pin as input */
 158                io_input();
 159
 160                command = command >> 1;
 161        }
 162}
 163
 164static void read_byte(unsigned char *data)
 165{
 166        unsigned char i, rdat = 0;
 167
 168        for (i = 0; i < 8; i++) {
 169                /* read one bit from one-wire device */
 170
 171                /* 1 - 15 us low starts bit slot */
 172                io_out(0);
 173                io_output();
 174                udelay(0);
 175
 176                /* allow line to be pulled high */
 177                io_input();
 178
 179                /* delay 10 us */
 180                udelay(10);
 181
 182                /* now sample input status */
 183                if (is_high())
 184                        rdat = (rdat >> 1) | 0x80;
 185                else
 186                        rdat = rdat >> 1;
 187
 188                udelay(60);     /* at least 60 us */
 189        }
 190        /* copy the return value */
 191        *data = rdat;
 192}
 193
 194void board_get_enetaddr(uchar *enetaddr)
 195{
 196        unsigned char sn[6], err = NO_ERROR;
 197
 198        init_gpio();
 199
 200        read_2501_memory(sn, &err);
 201
 202        if (err == NO_ERROR) {
 203                sprintf((char *)enetaddr, "%02x:%02x:%02x:%02x:%02x:%02x",
 204                                sn[0], sn[1], sn[2], sn[3], sn[4], sn[5]);
 205                printf("MAC address: %s\n", enetaddr);
 206                setenv("ethaddr", (char *)enetaddr);
 207        } else {
 208                sprintf((char *)enetaddr, "00:01:02:03:04:05");
 209                printf("Error reading MAC address.\n");
 210                printf("Setting default to %s\n", enetaddr);
 211                setenv("ethaddr", (char *)enetaddr);
 212        }
 213}
 214