/*------------------------------------------------------------------------------------------*\
 *   
 *   Copyright (C) 2006 AVM GmbH <fritzbox_info@avm.de>
 *
 *   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 version 2 of the License.
 *
 *   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 
\*------------------------------------------------------------------------------------------*/
#include <stdarg.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
#include <linux/timer.h>
#include <linux/skbuff.h>
#include <linux/netdevice.h>
#if !defined(CONFIG_NETCHIP_ADM69961)
#define CONFIG_NETCHIP_ADM69961
#endif
#include <linux/avm_event.h>
#include <asm/mips-boards/ohio.h>
#include <asm/mips-boards/prom.h>
#include <asm/mach-ohio/hw_gpio.h>

#include "cpmac_if.h"
#include "cpmac_const.h"
#include "cpmac_debug.h"
#include "cpphy_const.h"
#include "cpphy_types.h"
#include "cpphy_main.h"
#include "cpmac_main.h"
#include "cpphy_adm6996.h"

MODULE_AUTHOR("Maintainer: AVM GmbH");
MODULE_DESCRIPTION("Ethernet driver for FRITZ!Box)");

/*----------------------------------------------------------------------------------*\
  Zugriff auf cpmac_cpphy_global aus anderen Modulen ueber phy_handle
\*----------------------------------------------------------------------------------*/
cpmac_global_t cpmac_global;


/*----------------------------------------------------------------------------------*\
\*----------------------------------------------------------------------------------*/
static int __init cpphy_entry_probe(void) {
    char           *hwrev, buff[20], *p;
    unsigned char   instance;

    /* Initialize administrative array */
    for(instance = 0; instance < CPMAC_MAX_PHY; instance++) {
        cpmac_global.cpphy[instance].is_used = 0;
    }
    instance = 0;

    /*--------------------------------------------------------------------------------------*\
    \*--------------------------------------------------------------------------------------*/
    hwrev = prom_getenv("HWRevision");
    /*--- DEB_TRC("HWRevision=\"%s\"\n", hwrev); ---*/
    if(!hwrev) {
        return ENODEV;
    }

    strcpy(buff, hwrev);
    strcat(buff, " ");
    /*----------------------------------------------------------------------------------*\
     * aus "94.0.0.1 "  -->   "94 " erzeugen
    \*----------------------------------------------------------------------------------*/
    p = strchr(buff, '.');
    if(p) {
        *p++ = ' ';
        *p++ = '\0';
    }

    /* HWREV_LISTs are generated in the init_cpmac script */
    /* Low PHY checks */
    cpmac_global.cpphy[instance].is_used = (strstr(AVM_CPMAC_NO_PHY0_HWREV_LIST, buff)) ? 0 : 1;
    if(cpmac_global.cpphy[instance].is_used != 0) {
        cpmac_global.cpphy[instance].instance = instance;
        cpmac_global.cpphy[instance].high_phy = 0;
#       if defined(CONFIG_AVM_CPMAC_SWITCH)
        cpmac_global.cpphy[instance].cpmac_switch = (strstr(AVM_CPMAC_SWITCH_HWREV_LIST, buff)) ? 1 : 0;
        cpmac_global.cpphy[instance].is_vdsl = strstr(AVM_CPMAC_VDSL_HWREV_LIST, buff) != 0;
#       else /*--- #if defined(CONFIG_AVM_CPMAC_SWITCH) ---*/
        cpmac_global.cpphy[instance].cpmac_switch = 0;
#       endif /*--- #else ---*/ /*--- #if defined(CONFIG_AVM_CPMAC_SWITCH) ---*/
        instance++;
    }

#   if defined(CONFIG_MIPS_AR7)
    cpmac_global.cpphy[instance].is_used = (strstr(AVM_CPMAC_NO_PHY1_HWREV_LIST, buff)) ? 0 : 1;
    if(cpmac_global.cpphy[instance].is_used != 0) {
        cpmac_global.cpphy[instance].instance = instance;
        cpmac_global.cpphy[instance].high_phy = 1;
#       if defined(CONFIG_AVM_CPMAC_SWITCH)
        cpmac_global.cpphy[instance].cpmac_switch = (strstr(AVM_CPMAC_SWITCH_HWREV_LIST, buff)) ? 1 : 0;
        cpmac_global.cpphy[instance].is_vdsl = strstr(AVM_CPMAC_VDSL_HWREV_LIST, buff) != 0;
#       else /*--- #if defined(CONFIG_AVM_CPMAC_SWITCH) ---*/
        cpmac_global.cpphy[instance].cpmac_switch = 0;
#       endif /*--- #else ---*/ /*--- #if defined(CONFIG_AVM_CPMAC_SWITCH) ---*/
        instance++;
    }
#   endif /*--- #if defined(CONFIG_MIPS_AR7) ---*/

    /* initialize the timers for the net device */
    if(cpmac_main_timer_init() == -1) {
        DEB_ERR("cpphy_entry_probe, failed to set timer(s).\n");
    }
    for(instance = 0; instance < CPMAC_MAX_PHY; instance++) {
        if(cpmac_global.cpphy[instance].is_used) {
            DEB_INFO("cpphy_entry_probe, built-in, high: %u, switch: %u vdsl: %u\n",
                     cpmac_global.cpphy[instance].high_phy,
                     cpmac_global.cpphy[instance].cpmac_switch,
                     cpmac_global.cpphy[instance].is_vdsl);
            if(CPMAC_ERR_NOERR != cpphy_main_register(&cpmac_global.cpphy[instance])) {
                return -ENODEV;
            }
        }
    }
    cpmac_main_start_timer();

    /* Continuation via callback in cpphy_if_init() */
    return 0;
}


/*----------------------------------------------------------------------------------*\
\*----------------------------------------------------------------------------------*/
static void cpphy_entry_exit(void) {
    unsigned char instance;

    DEB_INFO("cpphy_entry_exit, init\n");
    for(instance = 0; instance < CPMAC_MAX_PHY; instance++) {
        cpmac_if_release(cpmac_global.cpphy[instance].cpmac_priv);
    }
}


/*------------------------------------------------------------------------------------------*\
\*------------------------------------------------------------------------------------------*/
int cpphy_entry_prepare_reboot(struct notifier_block *self, unsigned long kind, void *cmd) {
    unsigned char instance;

    /* We are neither interested in the kind of powerdown nor the command */

    for(instance = 0; instance < CPMAC_MAX_PHY; instance++) {
        if(cpmac_global.cpphy[instance].mdio.cpmac_switch) {
            DEB_INFO("System is going down. Resetting VLAN.\n");
            DEB_ERR("System is going down. Resetting VLAN.\n");
            adm_prepare_reboot(&cpmac_global.cpphy[instance].mdio);
        }
    }

    return NOTIFY_DONE;
}


module_init(cpphy_entry_probe);
module_exit(cpphy_entry_exit);

