#ident "kit/NEC41XX/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.
 */

/*
 * NEC41XX/sbdfrom.c: Flash ROM support for NEC Vr41xx UEB
 */


/*
 * Note: assumes DBUS32=1 and 32-bit flash module is fitted.
 */

#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 "sbdfrom.h"
#include "flashdev.h"
#include "flashrom.h"

/* writable cookies */
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)
{
    struct fromcookie *fcp = &cookie;
    unsigned int nbanks;

    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);

	/* flash device geometry */
	
	/* all configurations have 2 banks of LH28F032, which in themselves
	   contain 2 banks of LH28F016 */
	fcp->nbanks = 4;
	
	/* probe for first device */
	if (_flashrom_probe (fcp, 0) < 0)
	    return FLASH_MISSING;
	
	/* now check the number of banks (one may be replaced by ROM) */
	nbanks = 1;
	do {
	    unsigned int offs = (fcp->devsize << FROM_NCOLS_LOG2) * nbanks;
	    if (_flashrom_probe (fcp, offs) < 0)
		break;
	} while (++nbanks < 4);
	
	/* compute total size */
	fcp->nbanks = nbanks;
	fcp->size= (fcp->devsize << FROM_NCOLS_LOG2) * nbanks;
    }

    /* 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;

    /* XXX don't know split, say that first 512K is for boot code */

    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;
}
