#ident "kit/NEC5074L/sbdfrom.c: Copyright (c) MIPS Technologies, Inc. All rights reserved."

/*
 * Copyright (c) 1999-2005 MIPS Technologies, Inc.
 * 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 *      * Redistributions of source code must retain the above copyright
 *        notice, this list of conditions and the following disclaimer.
 *      * Redistributions in binary form must reproduce the above
 *      copyright
 *        notice, this list of conditions and the following disclaimer
 *        in the documentation and/or other materials provided with
 *        the distribution.
 *      * Neither the name of MIPS Technologies Inc. nor the names of its
 *        contributors may be used to endorse or promote products derived
 *        from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * NEC5074/sbdfrom.c: Flash ROM support for NEC DDB-Vrc5074
 */


#ifdef IN_PMON
#include "pmon.h"
#include "sde-compat.h"
#include <assert.h>
#else
#include <sys/types.h>
#include <mips/cpu.h>
#include <stdlib.h>
#include <string.h>
#include "kit_impl.h"
#endif

#include "sbd.h"
#include "vrc5074.h"
#include "sbdfrom.h"
#include "flashdev.h"
#include "flashrom.h"

/* writable cookie */
static struct fromcookie cookie;


/* board-specific code */
static int fromgetparts (flashcookie_t, struct flashparts *);
static int fromsetparts (flashcookie_t, const struct flashparts *);

static const struct flashboard fromboard = {
    fromgetparts,
    fromsetparts
};


flashcookie_t
_sbd_flashopen (paddr_t devaddr)
{
    static volatile struct vrc5074 * const n4 = PA_TO_KVA1 (VRC5074_BASE);
    static unsigned long flash_base = 0;
    static unsigned long flash_size;
    struct fromcookie *fcp = &cookie;

    if (flash_base == 0) {
	/* Check that DCS2 region has been programmed, if
	   not then do it ourselves */
	if ((n4->n4_dcs2 & N4_PDAR_SIZE_MASK) == N4_PDAR_OFF) {
	    /* Set DCS2 base address and size */
	    n4->n4_dcs2 = (FLASH_BASE | N4_PDAR_32BIT | N4_PDAR_4MB
			   | N4_PDAR_MEM);
	    /* Set DCS2 to be (flash rom) chip select */
	    n4->n4_dcsfn = ((n4->n4_dcsfn & ~N4_DCSFN_DCSFN2_MASK)
			    | N4_DCSFN_DCSFN2_BUS);
	    flash_base = FLASH_BASE;
	    flash_size = FLASH_SIZE;
	}
	else {
	    flash_base = n4->n4_dcs2 & N4_PDAR_ADDR_MASK;
	    flash_size = 0x200000 << (N4_PDAR_2MB - 
				      (n4->n4_dcs2 & N4_PDAR_SIZE_MASK));
	}
    }
	
    if (!(devaddr == 0
	  || (devaddr >= flash_base && devaddr < flash_base + flash_size)))
	return FLASH_UNKNOWN;

    if (fcp->size == 0) {
	/* programming base address may be different from read address */
	fcp->pbase = PA_TO_KVA1 (flash_base);
	if (IS_KVA0 (fcp))
	    fcp->rbase = PA_TO_KVA0 (flash_base);
	else
	    fcp->rbase = PA_TO_KVA1 (flash_base);

	/* probe for first bank */
	fcp->nbanks = 1;
	if (_flashrom_probe (fcp, 0) < 0)
	    return FLASH_MISSING;

	/* compute total size */
	fcp->size= (fcp->devsize << FROM_NCOLS_LOG2) * FROM_NBANKS(fcp);
    }

    /* check we still fit */
    if (devaddr >= flash_base + fcp->size)
	return FLASH_MISSING;

    /* plug in board-specific functions */
    fcp->common.board = &fromboard;

    return &fcp->common;
}


/*
 * Return flash partition table 
 */
static int
fromgetparts (flashcookie_t cookie, struct flashparts *parts)
{
    struct fromcookie *fcp = (struct fromcookie *)cookie;

    memset (parts, 0, sizeof (*parts));
    _flashdev_setpart (parts, FLASHPART_RAW, FLASHPART_RAW, 0, fcp->size);
    _flashdev_setpart (parts, FLASHPART_BOOT, FLASHPART_BOOT, 0, 0x80000);
    if (fcp->size > 0x80000)
	_flashdev_setpart (parts, FLASHPART_FFS, FLASHPART_UNDEF, 
			   0x80000, fcp->size - 0x80000);

    return FLASHDEV_OK;
}


static int
fromsetparts (flashcookie_t cookie, const struct flashparts *parts)
{
    /* not supported yet, sorry */
    return FLASHDEV_FAIL;
}
