/*
 * SerialICE 
 *
 * Copyright (C) 2009 coresystems GmbH
 *
 * 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 */

/* This is a chipset init file for the ConRoeXFire-eSATA2 mainboard */

const char boardname[33]="ConRoeXFire-eSATA2              ";

/* Hardware specific functions */

#define RCBA	0xfed1c000
#define   GCS	0x3410
#define RCBA32(x) *((volatile u32 *)(RCBA + x))

#define W83627EHG_SP1              2   /* Com1 */
#define PNP_DEV(PORT, FUNC) (((PORT) << 8) | (FUNC))
#define SERIAL_DEV PNP_DEV(0x2e, W83627EHG_SP1)

static void southbridge_init(void)
{
	u16 reg16;
	u32 reg32;

	// Set up RCBA
	pci_write_config32(PCI_ADDR(0, 0x1f, 0, 0xf0), RCBA | 1);

#if 0
	// port80 writes go to LPC:
	reg32 = RCBA32(GCS);
	reg32 = reg32 & ~0x04;
	RCBA32(GCS) = reg32;
	outb(0x23, 0x80);
#endif

	// Enable Serial IRQ
	pci_write_config8(PCI_ADDR(0, 0x1f, 0, 0x64), 0xd0);
	// Set COM1/COM2 decode range
	pci_write_config16(PCI_ADDR(0, 0x1f, 0, 0x80), 0x0010);
	// Enable COM1/COM2/KBD/SuperIO1+2
	pci_write_config16(PCI_ADDR(0, 0x1f, 0, 0x82), 0x340b);
	// Enable HWM at 0xa00
	pci_write_config32(PCI_ADDR(0, 0x1f, 0, 0x84), 0x00fc0a01);
	// COM3 decode
	pci_write_config32(PCI_ADDR(0, 0x1f, 0, 0x88), 0x000403e9);
	// COM4 decode
	pci_write_config32(PCI_ADDR(0, 0x1f, 0, 0x8c), 0x000402e9);
	// io 0x300 decode 
	pci_write_config32(PCI_ADDR(0, 0x1f, 0, 0x90), 0x00000301);

	// Disable watchdog
#define PMBASE 0x500
#define TCOBASE (PMBASE + 0x60)
	pci_write_config32(PCI_ADDR(0, 0x1f, 0, 0x40), PMBASE | 1);
	pci_write_config8(PCI_ADDR(0, 0x1f, 0, 0x44), 0x80);
	reg16 = inw(TCOBASE + 0x08);
	reg16 |= (1 << 11);
	outw(reg16, TCOBASE + 0x08);
	outw(0x0008, TCOBASE + 0x04);
	outw(0x0002, TCOBASE + 0x06);
}

static void superio_init(void)
{
	u8 reg;

	pnp_enter_ext_func_mode(0x2e);

	reg = pnp_read_register(0x2e, 0x24);
	reg |= (1 << 6); /* Set CLKSEL (clock input on pin 1) to 48MHz. */
	pnp_write_register(0x2e, 0x24, reg);

	/* We have GPIO for KB/MS pin. */
	reg = pnp_read_register(0x2e, 0x2a);
	pnp_write_register(0x2e, 0x2a, (reg | 1));
	/* We have all RESTOUT and even some reserved bits, too. */
	reg = pnp_read_register(0x2e, 0x2c);
	pnp_write_register(0x2e, 0x2c, (reg | 0xf0));
	pnp_exit_ext_func_mode(SERIAL_DEV);
#if 0
	pnp_enter_ext_func_mode(ACPI_DEV);
	pnp_set_logical_device(ACPI_DEV);
	/*
	 * Set the delay rising time from PWROK_LP to PWROK_ST to
	 * 300 - 600ms, and 0 to vice versa.
	 */

	reg = pnp_read_config(ACPI_DEV, 0xe6);
	pnp_write_config(ACPI_DEV, 0xe6, (reg & 0xf0));
	/* 1 Use external suspend clock source 32.768KHz. Undocumented?? */
	reg = pnp_read_config(ACPI_DEV, 0xe4);
	pnp_write_config(ACPI_DEV, 0xe4, (reg | 0x10));
	pnp_exit_ext_func_mode(ACPI_DEV);
#endif
	pnp_enter_ext_func_mode(0x2e);
	pnp_set_logical_device(0x2e, 9);
	/* Set memory voltage to 2.75V, vcore offset + 100mV, 1.5V chipset voltage. */
	pnp_write_register(0x2e, 0x30, 0x09);	/* Enable GPIO 2 & GPIO 5. */
	pnp_write_register(0x2e, 0xe2, 0x00);	/* No inversion */
	pnp_write_register(0x2e, 0xe5, 0x00);	/* No inversion */
	pnp_write_register(0x2e, 0xe3, 0x03);	/* 0000 0011, 0=output 1=input */
	pnp_write_register(0x2e, 0xe0, 0xde);	/* 1101 1110, 0=output 1=input */
	pnp_write_register(0x2e, 0xe1, 0x01);	/* Set output val. */
	pnp_write_register(0x2e, 0xe4, 0xb4);	/* Set output val (1011 0100). */
	pnp_exit_ext_func_mode(0x2e);
}

static void chipset_init(void)
{
	southbridge_init();
	superio_init();
}

