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

/*
 * Copyright (c) 1996-2003 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.
 */

/* 
 * display.c: low-level routines for 2x16 LCD front panel display
 */


#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <mips/cpu.h>
#include "kit_impl.h"
#include "sbd.h"
#include "cm1629.h"

/* 250ns delay between each phase of strobe */
#define lcd_delay()		nsdelay (250)

static int	lcd_present;
static char	lcd_slider[17];


static int
lcd_busy ()
{
    volatile cm1629dev *dp = (volatile cm1629dev *)PA_TO_KVA1(LCD_BASE);
    usdelay (1);
    return (dp->lcd_stat & LCD_STAT_BUSY) != 0;
}


static int
lcd_wait ()
{
    int timeout = 2000;		/* ~2ms */
    int busy;

    do {
	busy = lcd_busy ();
    } while (busy && --timeout != 0);
    usdelay (1);
    return !busy;
}


static void
lcd_command (unsigned char cmd) 
{
    volatile cm1629dev *dp = (volatile cm1629dev *)PA_TO_KVA1(LCD_BASE);
    if (!lcd_wait ())
	return;
    dp->lcd_cmd = cmd;
    mips_wbflush ();
    usdelay (40);
}


static void
lcd_data (unsigned char data) 
{
    volatile cm1629dev *dp = (volatile cm1629dev *)PA_TO_KVA1(LCD_BASE);
    if (!lcd_wait ())
	return;
    dp->lcd_data = data;
    mips_wbflush ();
    usdelay (40);
}


void
_sbdpanelmessage (int row, int col, const char *s, size_t nb, int pad)
{
    int addr;

    if (!lcd_present)
	return;

    addr = ((row == 0) ? 0x00 : 0x40) + col;
    lcd_command (LCD_SET_DDADDR | addr);
    
    while (col++ < 16) {
	if (*s && nb > 0) {
	    lcd_data (*s++);
	    nb--;
	}
	else if (pad)
	    lcd_data (' ');
	else
	    break;
    }
}


void
_sbdpanelprogress (int percent)
{
    if (lcd_present) {
	static int lastnb = -1;
	int nb = (percent * 16 + 50) / 100;
	
	if (nb < 0)
	    nb = 0;
	else if (nb > 16) 
	    nb = 16;
	if (nb != lastnb) {
	    _sbdpanelmessage (0, 0, lcd_slider, nb, 1);
	    lastnb = nb;
	}
    }
}

void
_sbdpanelflash (int on)
{
    lcd_command (LCD_SET_DDADDR | 15);
    lcd_data (on ? '*' : ' ');
}


void 
_sbdpanelsize (int *prows, int *pcols)
{
    if (lcd_present) {
	/* 2 x 16 LCD display */
	if (prows) *prows = 2;
	if (pcols) *pcols = 16;
    }
    else {
	if (prows) *prows = 0;
	if (pcols) *pcols = 0;
    }
}


int 
_sbdpanelinit (void)
{
    lcd_present = lcd_wait ();
    if (!lcd_present)
	return 0;

    lcd_command (LCD_CLEAR);

    /* should be busy */
    lcd_present = lcd_busy();
    if (!lcd_present)
	return 0;

    /* initialise LCD display */
    lcd_command (LCD_FUNC | LCD_FUNC_8BIT | LCD_FUNC_2LINE | LCD_FUNC_5x7);
    lcd_command (LCD_CTRL | LCD_CTRL_DISPLAY);
    lcd_command (LCD_MODE | LCD_MODE_INCR);
    lcd_command (LCD_HOME); 

    memset (lcd_slider, 0xff, sizeof(lcd_slider) - 1);
    lcd_slider[sizeof(lcd_slider)-1] = '\0';

    return 1;
}
