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

/*
 * Copyright (c) 1997-2004 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.
 */

/*
 * GAL9/sbdfrom.c: Flash ROM support for Galileo GAL9
 */


#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 cookie */
static struct fromcookie flash_cookie;
static struct fromcookie prom_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
};


#define FLASH_SIZE	0x400000

flashcookie_t
_sbd_flashopen (paddr_t devaddr)
{
    struct fromcookie *fcp;
    paddr_t base;

    if (devaddr == 0
	 || (devaddr >= FLASH_BASE && devaddr < FLASH_BASE + FLASH_SIZE)) {
	fcp = &flash_cookie;
	base = FLASH_BASE;
    }
    else if (devaddr == 1
	     || (devaddr >= PROM_BASE && devaddr < PROM_BASE + FLASH_SIZE)) {
	fcp = &prom_cookie;
	base = PROM_BASE;
    }
    else
	return FLASH_UNKNOWN;

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

	/* probe for the device */
	if (_flashrom_probe (fcp, 0) < 0)
	    return FLASH_MISSING;

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

    /* check we still fit */
    if ((devaddr & (FLASH_SIZE - 1)) >= 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;
    unsigned int offs, eoffs, esize;
    struct flashinfo info;

    memset (parts, 0, sizeof (*parts));
    _flashdev_setpart (parts, FLASHPART_RAW, FLASHPART_RAW, 0, fcp->size);
    _flashdev_setpart (parts, FLASHPART_BOOT, FLASHPART_BOOT, 0, 0x68000);
    _flashdev_setpart (parts, FLASHPART_POST, FLASHPART_POST, 
		       0x68000, 0x18000);

    /* find the smallest writable sector from the top of the flash */
    flash_info (cookie, 0, &info);
    offs = info.size;
    eoffs = esize = 0;
    while (offs > 0) {
	flash_info (cookie, offs - 1, &info);
	offs -= info.ssize;
	if (info.sprot)
	    continue;
	if (eoffs == 0 || info.ssize < esize) {
	    eoffs = info.soffs;
	    esize = info.ssize;
	}
	/* stop when we reach the non-boot sectors */
	if (info.ssize >= info.maxssize)
	    break;
    }

    if (esize != 0) {
	_flashdev_setpart (parts, FLASHPART_ENV, FLASHPART_ENV, 
			   eoffs, esize);
	if (eoffs < 0x80000)
	    /* reduce POST size for environment */
	    parts->part[FLASHPART_POST].size = eoffs
		- parts->part[FLASHPART_POST].offs;
	else
	    /* spare partition */
	    _flashdev_setpart (parts, FLASHPART_FFS, FLASHPART_UNDEF, 
			       0x80000, eoffs - 0x80000);
	if (eoffs + esize < fcp->size)
	    _flashdev_setpart (parts, FLASHPART_FFS+1, FLASHPART_UNDEF, 
			       eoffs + esize, fcp->size - (eoffs + esize));
    }

    return FLASHDEV_OK;
}


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