18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/**
38c2ecf20Sopenharmony_ci *	i2c-ali1563.c - i2c driver for the ALi 1563 Southbridge
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *	Copyright (C) 2004 Patrick Mochel
68c2ecf20Sopenharmony_ci *		      2005 Rudolf Marek <r.marek@assembler.cz>
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci *	The 1563 southbridge is deceptively similar to the 1533, with a
98c2ecf20Sopenharmony_ci *	few notable exceptions. One of those happens to be the fact they
108c2ecf20Sopenharmony_ci *	upgraded the i2c core to be 2.0 compliant, and happens to be almost
118c2ecf20Sopenharmony_ci *	identical to the i2c controller found in the Intel 801 south
128c2ecf20Sopenharmony_ci *	bridges.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci *	This driver is based on a mix of the 15x3, 1535, and i801 drivers,
158c2ecf20Sopenharmony_ci *	with a little help from the ALi 1563 spec.
168c2ecf20Sopenharmony_ci */
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#include <linux/module.h>
198c2ecf20Sopenharmony_ci#include <linux/delay.h>
208c2ecf20Sopenharmony_ci#include <linux/i2c.h>
218c2ecf20Sopenharmony_ci#include <linux/pci.h>
228c2ecf20Sopenharmony_ci#include <linux/acpi.h>
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#define ALI1563_MAX_TIMEOUT	500
258c2ecf20Sopenharmony_ci#define	ALI1563_SMBBA		0x80
268c2ecf20Sopenharmony_ci#define ALI1563_SMB_IOEN	1
278c2ecf20Sopenharmony_ci#define ALI1563_SMB_HOSTEN	2
288c2ecf20Sopenharmony_ci#define ALI1563_SMB_IOSIZE	16
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#define SMB_HST_STS	(ali1563_smba + 0)
318c2ecf20Sopenharmony_ci#define SMB_HST_CNTL1	(ali1563_smba + 1)
328c2ecf20Sopenharmony_ci#define SMB_HST_CNTL2	(ali1563_smba + 2)
338c2ecf20Sopenharmony_ci#define SMB_HST_CMD	(ali1563_smba + 3)
348c2ecf20Sopenharmony_ci#define SMB_HST_ADD	(ali1563_smba + 4)
358c2ecf20Sopenharmony_ci#define SMB_HST_DAT0	(ali1563_smba + 5)
368c2ecf20Sopenharmony_ci#define SMB_HST_DAT1	(ali1563_smba + 6)
378c2ecf20Sopenharmony_ci#define SMB_BLK_DAT	(ali1563_smba + 7)
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#define HST_STS_BUSY	0x01
408c2ecf20Sopenharmony_ci#define HST_STS_INTR	0x02
418c2ecf20Sopenharmony_ci#define HST_STS_DEVERR	0x04
428c2ecf20Sopenharmony_ci#define HST_STS_BUSERR	0x08
438c2ecf20Sopenharmony_ci#define HST_STS_FAIL	0x10
448c2ecf20Sopenharmony_ci#define HST_STS_DONE	0x80
458c2ecf20Sopenharmony_ci#define HST_STS_BAD	0x1c
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci#define HST_CNTL1_TIMEOUT	0x80
498c2ecf20Sopenharmony_ci#define HST_CNTL1_LAST		0x40
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci#define HST_CNTL2_KILL		0x04
528c2ecf20Sopenharmony_ci#define HST_CNTL2_START		0x40
538c2ecf20Sopenharmony_ci#define HST_CNTL2_QUICK		0x00
548c2ecf20Sopenharmony_ci#define HST_CNTL2_BYTE		0x01
558c2ecf20Sopenharmony_ci#define HST_CNTL2_BYTE_DATA	0x02
568c2ecf20Sopenharmony_ci#define HST_CNTL2_WORD_DATA	0x03
578c2ecf20Sopenharmony_ci#define HST_CNTL2_BLOCK		0x05
588c2ecf20Sopenharmony_ci
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci#define HST_CNTL2_SIZEMASK	0x38
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistatic struct pci_driver ali1563_pci_driver;
638c2ecf20Sopenharmony_cistatic unsigned short ali1563_smba;
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_cistatic int ali1563_transaction(struct i2c_adapter *a, int size)
668c2ecf20Sopenharmony_ci{
678c2ecf20Sopenharmony_ci	u32 data;
688c2ecf20Sopenharmony_ci	int timeout;
698c2ecf20Sopenharmony_ci	int status = -EIO;
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	dev_dbg(&a->dev, "Transaction (pre): STS=%02x, CNTL1=%02x, "
728c2ecf20Sopenharmony_ci		"CNTL2=%02x, CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n",
738c2ecf20Sopenharmony_ci		inb_p(SMB_HST_STS), inb_p(SMB_HST_CNTL1), inb_p(SMB_HST_CNTL2),
748c2ecf20Sopenharmony_ci		inb_p(SMB_HST_CMD), inb_p(SMB_HST_ADD), inb_p(SMB_HST_DAT0),
758c2ecf20Sopenharmony_ci		inb_p(SMB_HST_DAT1));
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	data = inb_p(SMB_HST_STS);
788c2ecf20Sopenharmony_ci	if (data & HST_STS_BAD) {
798c2ecf20Sopenharmony_ci		dev_err(&a->dev, "ali1563: Trying to reset busy device\n");
808c2ecf20Sopenharmony_ci		outb_p(data | HST_STS_BAD, SMB_HST_STS);
818c2ecf20Sopenharmony_ci		data = inb_p(SMB_HST_STS);
828c2ecf20Sopenharmony_ci		if (data & HST_STS_BAD)
838c2ecf20Sopenharmony_ci			return -EBUSY;
848c2ecf20Sopenharmony_ci	}
858c2ecf20Sopenharmony_ci	outb_p(inb_p(SMB_HST_CNTL2) | HST_CNTL2_START, SMB_HST_CNTL2);
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci	timeout = ALI1563_MAX_TIMEOUT;
888c2ecf20Sopenharmony_ci	do {
898c2ecf20Sopenharmony_ci		msleep(1);
908c2ecf20Sopenharmony_ci	} while (((data = inb_p(SMB_HST_STS)) & HST_STS_BUSY) && --timeout);
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci	dev_dbg(&a->dev, "Transaction (post): STS=%02x, CNTL1=%02x, "
938c2ecf20Sopenharmony_ci		"CNTL2=%02x, CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n",
948c2ecf20Sopenharmony_ci		inb_p(SMB_HST_STS), inb_p(SMB_HST_CNTL1), inb_p(SMB_HST_CNTL2),
958c2ecf20Sopenharmony_ci		inb_p(SMB_HST_CMD), inb_p(SMB_HST_ADD), inb_p(SMB_HST_DAT0),
968c2ecf20Sopenharmony_ci		inb_p(SMB_HST_DAT1));
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	if (timeout && !(data & HST_STS_BAD))
998c2ecf20Sopenharmony_ci		return 0;
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	if (!timeout) {
1028c2ecf20Sopenharmony_ci		dev_err(&a->dev, "Timeout - Trying to KILL transaction!\n");
1038c2ecf20Sopenharmony_ci		/* Issue 'kill' to host controller */
1048c2ecf20Sopenharmony_ci		outb_p(HST_CNTL2_KILL, SMB_HST_CNTL2);
1058c2ecf20Sopenharmony_ci		data = inb_p(SMB_HST_STS);
1068c2ecf20Sopenharmony_ci		status = -ETIMEDOUT;
1078c2ecf20Sopenharmony_ci	}
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci	/* device error - no response, ignore the autodetection case */
1108c2ecf20Sopenharmony_ci	if (data & HST_STS_DEVERR) {
1118c2ecf20Sopenharmony_ci		if (size != HST_CNTL2_QUICK)
1128c2ecf20Sopenharmony_ci			dev_err(&a->dev, "Device error!\n");
1138c2ecf20Sopenharmony_ci		status = -ENXIO;
1148c2ecf20Sopenharmony_ci	}
1158c2ecf20Sopenharmony_ci	/* bus collision */
1168c2ecf20Sopenharmony_ci	if (data & HST_STS_BUSERR) {
1178c2ecf20Sopenharmony_ci		dev_err(&a->dev, "Bus collision!\n");
1188c2ecf20Sopenharmony_ci		/* Issue timeout, hoping it helps */
1198c2ecf20Sopenharmony_ci		outb_p(HST_CNTL1_TIMEOUT, SMB_HST_CNTL1);
1208c2ecf20Sopenharmony_ci	}
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	if (data & HST_STS_FAIL) {
1238c2ecf20Sopenharmony_ci		dev_err(&a->dev, "Cleaning fail after KILL!\n");
1248c2ecf20Sopenharmony_ci		outb_p(0x0, SMB_HST_CNTL2);
1258c2ecf20Sopenharmony_ci	}
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	return status;
1288c2ecf20Sopenharmony_ci}
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_cistatic int ali1563_block_start(struct i2c_adapter *a)
1318c2ecf20Sopenharmony_ci{
1328c2ecf20Sopenharmony_ci	u32 data;
1338c2ecf20Sopenharmony_ci	int timeout;
1348c2ecf20Sopenharmony_ci	int status = -EIO;
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	dev_dbg(&a->dev, "Block (pre): STS=%02x, CNTL1=%02x, "
1378c2ecf20Sopenharmony_ci		"CNTL2=%02x, CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n",
1388c2ecf20Sopenharmony_ci		inb_p(SMB_HST_STS), inb_p(SMB_HST_CNTL1), inb_p(SMB_HST_CNTL2),
1398c2ecf20Sopenharmony_ci		inb_p(SMB_HST_CMD), inb_p(SMB_HST_ADD), inb_p(SMB_HST_DAT0),
1408c2ecf20Sopenharmony_ci		inb_p(SMB_HST_DAT1));
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	data = inb_p(SMB_HST_STS);
1438c2ecf20Sopenharmony_ci	if (data & HST_STS_BAD) {
1448c2ecf20Sopenharmony_ci		dev_warn(&a->dev, "ali1563: Trying to reset busy device\n");
1458c2ecf20Sopenharmony_ci		outb_p(data | HST_STS_BAD, SMB_HST_STS);
1468c2ecf20Sopenharmony_ci		data = inb_p(SMB_HST_STS);
1478c2ecf20Sopenharmony_ci		if (data & HST_STS_BAD)
1488c2ecf20Sopenharmony_ci			return -EBUSY;
1498c2ecf20Sopenharmony_ci	}
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	/* Clear byte-ready bit */
1528c2ecf20Sopenharmony_ci	outb_p(data | HST_STS_DONE, SMB_HST_STS);
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	/* Start transaction and wait for byte-ready bit to be set */
1558c2ecf20Sopenharmony_ci	outb_p(inb_p(SMB_HST_CNTL2) | HST_CNTL2_START, SMB_HST_CNTL2);
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	timeout = ALI1563_MAX_TIMEOUT;
1588c2ecf20Sopenharmony_ci	do {
1598c2ecf20Sopenharmony_ci		msleep(1);
1608c2ecf20Sopenharmony_ci	} while (!((data = inb_p(SMB_HST_STS)) & HST_STS_DONE) && --timeout);
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	dev_dbg(&a->dev, "Block (post): STS=%02x, CNTL1=%02x, "
1638c2ecf20Sopenharmony_ci		"CNTL2=%02x, CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n",
1648c2ecf20Sopenharmony_ci		inb_p(SMB_HST_STS), inb_p(SMB_HST_CNTL1), inb_p(SMB_HST_CNTL2),
1658c2ecf20Sopenharmony_ci		inb_p(SMB_HST_CMD), inb_p(SMB_HST_ADD), inb_p(SMB_HST_DAT0),
1668c2ecf20Sopenharmony_ci		inb_p(SMB_HST_DAT1));
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	if (timeout && !(data & HST_STS_BAD))
1698c2ecf20Sopenharmony_ci		return 0;
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	if (timeout == 0)
1728c2ecf20Sopenharmony_ci		status = -ETIMEDOUT;
1738c2ecf20Sopenharmony_ci
1748c2ecf20Sopenharmony_ci	if (data & HST_STS_DEVERR)
1758c2ecf20Sopenharmony_ci		status = -ENXIO;
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	dev_err(&a->dev, "SMBus Error: %s%s%s%s%s\n",
1788c2ecf20Sopenharmony_ci		timeout ? "" : "Timeout ",
1798c2ecf20Sopenharmony_ci		data & HST_STS_FAIL ? "Transaction Failed " : "",
1808c2ecf20Sopenharmony_ci		data & HST_STS_BUSERR ? "No response or Bus Collision " : "",
1818c2ecf20Sopenharmony_ci		data & HST_STS_DEVERR ? "Device Error " : "",
1828c2ecf20Sopenharmony_ci		!(data & HST_STS_DONE) ? "Transaction Never Finished " : "");
1838c2ecf20Sopenharmony_ci	return status;
1848c2ecf20Sopenharmony_ci}
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_cistatic int ali1563_block(struct i2c_adapter *a,
1878c2ecf20Sopenharmony_ci			 union i2c_smbus_data *data, u8 rw)
1888c2ecf20Sopenharmony_ci{
1898c2ecf20Sopenharmony_ci	int i, len;
1908c2ecf20Sopenharmony_ci	int error = 0;
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	/* Do we need this? */
1938c2ecf20Sopenharmony_ci	outb_p(HST_CNTL1_LAST, SMB_HST_CNTL1);
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	if (rw == I2C_SMBUS_WRITE) {
1968c2ecf20Sopenharmony_ci		len = data->block[0];
1978c2ecf20Sopenharmony_ci		if (len < 1)
1988c2ecf20Sopenharmony_ci			len = 1;
1998c2ecf20Sopenharmony_ci		else if (len > 32)
2008c2ecf20Sopenharmony_ci			len = 32;
2018c2ecf20Sopenharmony_ci		outb_p(len, SMB_HST_DAT0);
2028c2ecf20Sopenharmony_ci		outb_p(data->block[1], SMB_BLK_DAT);
2038c2ecf20Sopenharmony_ci	} else
2048c2ecf20Sopenharmony_ci		len = 32;
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci	outb_p(inb_p(SMB_HST_CNTL2) | HST_CNTL2_BLOCK, SMB_HST_CNTL2);
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	for (i = 0; i < len; i++) {
2098c2ecf20Sopenharmony_ci		if (rw == I2C_SMBUS_WRITE) {
2108c2ecf20Sopenharmony_ci			outb_p(data->block[i + 1], SMB_BLK_DAT);
2118c2ecf20Sopenharmony_ci			error = ali1563_block_start(a);
2128c2ecf20Sopenharmony_ci			if (error)
2138c2ecf20Sopenharmony_ci				break;
2148c2ecf20Sopenharmony_ci		} else {
2158c2ecf20Sopenharmony_ci			error = ali1563_block_start(a);
2168c2ecf20Sopenharmony_ci			if (error)
2178c2ecf20Sopenharmony_ci				break;
2188c2ecf20Sopenharmony_ci			if (i == 0) {
2198c2ecf20Sopenharmony_ci				len = inb_p(SMB_HST_DAT0);
2208c2ecf20Sopenharmony_ci				if (len < 1)
2218c2ecf20Sopenharmony_ci					len = 1;
2228c2ecf20Sopenharmony_ci				else if (len > 32)
2238c2ecf20Sopenharmony_ci					len = 32;
2248c2ecf20Sopenharmony_ci			}
2258c2ecf20Sopenharmony_ci			data->block[i+1] = inb_p(SMB_BLK_DAT);
2268c2ecf20Sopenharmony_ci		}
2278c2ecf20Sopenharmony_ci	}
2288c2ecf20Sopenharmony_ci	/* Do we need this? */
2298c2ecf20Sopenharmony_ci	outb_p(HST_CNTL1_LAST, SMB_HST_CNTL1);
2308c2ecf20Sopenharmony_ci	return error;
2318c2ecf20Sopenharmony_ci}
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_cistatic s32 ali1563_access(struct i2c_adapter *a, u16 addr,
2348c2ecf20Sopenharmony_ci			  unsigned short flags, char rw, u8 cmd,
2358c2ecf20Sopenharmony_ci			  int size, union i2c_smbus_data *data)
2368c2ecf20Sopenharmony_ci{
2378c2ecf20Sopenharmony_ci	int error = 0;
2388c2ecf20Sopenharmony_ci	int timeout;
2398c2ecf20Sopenharmony_ci	u32 reg;
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	for (timeout = ALI1563_MAX_TIMEOUT; timeout; timeout--) {
2428c2ecf20Sopenharmony_ci		reg = inb_p(SMB_HST_STS);
2438c2ecf20Sopenharmony_ci		if (!(reg & HST_STS_BUSY))
2448c2ecf20Sopenharmony_ci			break;
2458c2ecf20Sopenharmony_ci	}
2468c2ecf20Sopenharmony_ci	if (!timeout)
2478c2ecf20Sopenharmony_ci		dev_warn(&a->dev, "SMBus not idle. HST_STS = %02x\n", reg);
2488c2ecf20Sopenharmony_ci	outb_p(0xff, SMB_HST_STS);
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	/* Map the size to what the chip understands */
2518c2ecf20Sopenharmony_ci	switch (size) {
2528c2ecf20Sopenharmony_ci	case I2C_SMBUS_QUICK:
2538c2ecf20Sopenharmony_ci		size = HST_CNTL2_QUICK;
2548c2ecf20Sopenharmony_ci		break;
2558c2ecf20Sopenharmony_ci	case I2C_SMBUS_BYTE:
2568c2ecf20Sopenharmony_ci		size = HST_CNTL2_BYTE;
2578c2ecf20Sopenharmony_ci		break;
2588c2ecf20Sopenharmony_ci	case I2C_SMBUS_BYTE_DATA:
2598c2ecf20Sopenharmony_ci		size = HST_CNTL2_BYTE_DATA;
2608c2ecf20Sopenharmony_ci		break;
2618c2ecf20Sopenharmony_ci	case I2C_SMBUS_WORD_DATA:
2628c2ecf20Sopenharmony_ci		size = HST_CNTL2_WORD_DATA;
2638c2ecf20Sopenharmony_ci		break;
2648c2ecf20Sopenharmony_ci	case I2C_SMBUS_BLOCK_DATA:
2658c2ecf20Sopenharmony_ci		size = HST_CNTL2_BLOCK;
2668c2ecf20Sopenharmony_ci		break;
2678c2ecf20Sopenharmony_ci	default:
2688c2ecf20Sopenharmony_ci		dev_warn(&a->dev, "Unsupported transaction %d\n", size);
2698c2ecf20Sopenharmony_ci		error = -EOPNOTSUPP;
2708c2ecf20Sopenharmony_ci		goto Done;
2718c2ecf20Sopenharmony_ci	}
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	outb_p(((addr & 0x7f) << 1) | (rw & 0x01), SMB_HST_ADD);
2748c2ecf20Sopenharmony_ci	outb_p((inb_p(SMB_HST_CNTL2) & ~HST_CNTL2_SIZEMASK) |
2758c2ecf20Sopenharmony_ci	       (size << 3), SMB_HST_CNTL2);
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	/* Write the command register */
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	switch (size) {
2808c2ecf20Sopenharmony_ci	case HST_CNTL2_BYTE:
2818c2ecf20Sopenharmony_ci		if (rw == I2C_SMBUS_WRITE)
2828c2ecf20Sopenharmony_ci			/* Beware it uses DAT0 register and not CMD! */
2838c2ecf20Sopenharmony_ci			outb_p(cmd, SMB_HST_DAT0);
2848c2ecf20Sopenharmony_ci		break;
2858c2ecf20Sopenharmony_ci	case HST_CNTL2_BYTE_DATA:
2868c2ecf20Sopenharmony_ci		outb_p(cmd, SMB_HST_CMD);
2878c2ecf20Sopenharmony_ci		if (rw == I2C_SMBUS_WRITE)
2888c2ecf20Sopenharmony_ci			outb_p(data->byte, SMB_HST_DAT0);
2898c2ecf20Sopenharmony_ci		break;
2908c2ecf20Sopenharmony_ci	case HST_CNTL2_WORD_DATA:
2918c2ecf20Sopenharmony_ci		outb_p(cmd, SMB_HST_CMD);
2928c2ecf20Sopenharmony_ci		if (rw == I2C_SMBUS_WRITE) {
2938c2ecf20Sopenharmony_ci			outb_p(data->word & 0xff, SMB_HST_DAT0);
2948c2ecf20Sopenharmony_ci			outb_p((data->word & 0xff00) >> 8, SMB_HST_DAT1);
2958c2ecf20Sopenharmony_ci		}
2968c2ecf20Sopenharmony_ci		break;
2978c2ecf20Sopenharmony_ci	case HST_CNTL2_BLOCK:
2988c2ecf20Sopenharmony_ci		outb_p(cmd, SMB_HST_CMD);
2998c2ecf20Sopenharmony_ci		error = ali1563_block(a, data, rw);
3008c2ecf20Sopenharmony_ci		goto Done;
3018c2ecf20Sopenharmony_ci	}
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci	error = ali1563_transaction(a, size);
3048c2ecf20Sopenharmony_ci	if (error)
3058c2ecf20Sopenharmony_ci		goto Done;
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	if ((rw == I2C_SMBUS_WRITE) || (size == HST_CNTL2_QUICK))
3088c2ecf20Sopenharmony_ci		goto Done;
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	switch (size) {
3118c2ecf20Sopenharmony_ci	case HST_CNTL2_BYTE:	/* Result put in SMBHSTDAT0 */
3128c2ecf20Sopenharmony_ci		data->byte = inb_p(SMB_HST_DAT0);
3138c2ecf20Sopenharmony_ci		break;
3148c2ecf20Sopenharmony_ci	case HST_CNTL2_BYTE_DATA:
3158c2ecf20Sopenharmony_ci		data->byte = inb_p(SMB_HST_DAT0);
3168c2ecf20Sopenharmony_ci		break;
3178c2ecf20Sopenharmony_ci	case HST_CNTL2_WORD_DATA:
3188c2ecf20Sopenharmony_ci		data->word = inb_p(SMB_HST_DAT0) + (inb_p(SMB_HST_DAT1) << 8);
3198c2ecf20Sopenharmony_ci		break;
3208c2ecf20Sopenharmony_ci	}
3218c2ecf20Sopenharmony_ciDone:
3228c2ecf20Sopenharmony_ci	return error;
3238c2ecf20Sopenharmony_ci}
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_cistatic u32 ali1563_func(struct i2c_adapter *a)
3268c2ecf20Sopenharmony_ci{
3278c2ecf20Sopenharmony_ci	return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
3288c2ecf20Sopenharmony_ci	    I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
3298c2ecf20Sopenharmony_ci	    I2C_FUNC_SMBUS_BLOCK_DATA;
3308c2ecf20Sopenharmony_ci}
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_cistatic int ali1563_setup(struct pci_dev *dev)
3348c2ecf20Sopenharmony_ci{
3358c2ecf20Sopenharmony_ci	u16 ctrl;
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	pci_read_config_word(dev, ALI1563_SMBBA, &ctrl);
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_ci	/* SMB I/O Base in high 12 bits and must be aligned with the
3408c2ecf20Sopenharmony_ci	 * size of the I/O space. */
3418c2ecf20Sopenharmony_ci	ali1563_smba = ctrl & ~(ALI1563_SMB_IOSIZE - 1);
3428c2ecf20Sopenharmony_ci	if (!ali1563_smba) {
3438c2ecf20Sopenharmony_ci		dev_warn(&dev->dev, "ali1563_smba Uninitialized\n");
3448c2ecf20Sopenharmony_ci		goto Err;
3458c2ecf20Sopenharmony_ci	}
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci	/* Check if device is enabled */
3488c2ecf20Sopenharmony_ci	if (!(ctrl & ALI1563_SMB_HOSTEN)) {
3498c2ecf20Sopenharmony_ci		dev_warn(&dev->dev, "Host Controller not enabled\n");
3508c2ecf20Sopenharmony_ci		goto Err;
3518c2ecf20Sopenharmony_ci	}
3528c2ecf20Sopenharmony_ci	if (!(ctrl & ALI1563_SMB_IOEN)) {
3538c2ecf20Sopenharmony_ci		dev_warn(&dev->dev, "I/O space not enabled, trying manually\n");
3548c2ecf20Sopenharmony_ci		pci_write_config_word(dev, ALI1563_SMBBA,
3558c2ecf20Sopenharmony_ci				      ctrl | ALI1563_SMB_IOEN);
3568c2ecf20Sopenharmony_ci		pci_read_config_word(dev, ALI1563_SMBBA, &ctrl);
3578c2ecf20Sopenharmony_ci		if (!(ctrl & ALI1563_SMB_IOEN)) {
3588c2ecf20Sopenharmony_ci			dev_err(&dev->dev,
3598c2ecf20Sopenharmony_ci				"I/O space still not enabled, giving up\n");
3608c2ecf20Sopenharmony_ci			goto Err;
3618c2ecf20Sopenharmony_ci		}
3628c2ecf20Sopenharmony_ci	}
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci	if (acpi_check_region(ali1563_smba, ALI1563_SMB_IOSIZE,
3658c2ecf20Sopenharmony_ci			      ali1563_pci_driver.name))
3668c2ecf20Sopenharmony_ci		goto Err;
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci	if (!request_region(ali1563_smba, ALI1563_SMB_IOSIZE,
3698c2ecf20Sopenharmony_ci			    ali1563_pci_driver.name)) {
3708c2ecf20Sopenharmony_ci		dev_err(&dev->dev, "Could not allocate I/O space at 0x%04x\n",
3718c2ecf20Sopenharmony_ci			ali1563_smba);
3728c2ecf20Sopenharmony_ci		goto Err;
3738c2ecf20Sopenharmony_ci	}
3748c2ecf20Sopenharmony_ci	dev_info(&dev->dev, "Found ALi1563 SMBus at 0x%04x\n", ali1563_smba);
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci	return 0;
3778c2ecf20Sopenharmony_ciErr:
3788c2ecf20Sopenharmony_ci	return -ENODEV;
3798c2ecf20Sopenharmony_ci}
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_cistatic void ali1563_shutdown(struct pci_dev *dev)
3828c2ecf20Sopenharmony_ci{
3838c2ecf20Sopenharmony_ci	release_region(ali1563_smba, ALI1563_SMB_IOSIZE);
3848c2ecf20Sopenharmony_ci}
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_cistatic const struct i2c_algorithm ali1563_algorithm = {
3878c2ecf20Sopenharmony_ci	.smbus_xfer	= ali1563_access,
3888c2ecf20Sopenharmony_ci	.functionality	= ali1563_func,
3898c2ecf20Sopenharmony_ci};
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_cistatic struct i2c_adapter ali1563_adapter = {
3928c2ecf20Sopenharmony_ci	.owner	= THIS_MODULE,
3938c2ecf20Sopenharmony_ci	.class	= I2C_CLASS_HWMON | I2C_CLASS_SPD,
3948c2ecf20Sopenharmony_ci	.algo	= &ali1563_algorithm,
3958c2ecf20Sopenharmony_ci};
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_cistatic int ali1563_probe(struct pci_dev *dev,
3988c2ecf20Sopenharmony_ci			 const struct pci_device_id *id_table)
3998c2ecf20Sopenharmony_ci{
4008c2ecf20Sopenharmony_ci	int error;
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	error = ali1563_setup(dev);
4038c2ecf20Sopenharmony_ci	if (error)
4048c2ecf20Sopenharmony_ci		goto exit;
4058c2ecf20Sopenharmony_ci	ali1563_adapter.dev.parent = &dev->dev;
4068c2ecf20Sopenharmony_ci	snprintf(ali1563_adapter.name, sizeof(ali1563_adapter.name),
4078c2ecf20Sopenharmony_ci		 "SMBus ALi 1563 Adapter @ %04x", ali1563_smba);
4088c2ecf20Sopenharmony_ci	error = i2c_add_adapter(&ali1563_adapter);
4098c2ecf20Sopenharmony_ci	if (error)
4108c2ecf20Sopenharmony_ci		goto exit_shutdown;
4118c2ecf20Sopenharmony_ci	return 0;
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ciexit_shutdown:
4148c2ecf20Sopenharmony_ci	ali1563_shutdown(dev);
4158c2ecf20Sopenharmony_ciexit:
4168c2ecf20Sopenharmony_ci	dev_warn(&dev->dev, "ALi1563 SMBus probe failed (%d)\n", error);
4178c2ecf20Sopenharmony_ci	return error;
4188c2ecf20Sopenharmony_ci}
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_cistatic void ali1563_remove(struct pci_dev *dev)
4218c2ecf20Sopenharmony_ci{
4228c2ecf20Sopenharmony_ci	i2c_del_adapter(&ali1563_adapter);
4238c2ecf20Sopenharmony_ci	ali1563_shutdown(dev);
4248c2ecf20Sopenharmony_ci}
4258c2ecf20Sopenharmony_ci
4268c2ecf20Sopenharmony_cistatic const struct pci_device_id ali1563_id_table[] = {
4278c2ecf20Sopenharmony_ci	{ PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M1563) },
4288c2ecf20Sopenharmony_ci	{},
4298c2ecf20Sopenharmony_ci};
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(pci, ali1563_id_table);
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_cistatic struct pci_driver ali1563_pci_driver = {
4348c2ecf20Sopenharmony_ci	.name		= "ali1563_smbus",
4358c2ecf20Sopenharmony_ci	.id_table	= ali1563_id_table,
4368c2ecf20Sopenharmony_ci	.probe		= ali1563_probe,
4378c2ecf20Sopenharmony_ci	.remove		= ali1563_remove,
4388c2ecf20Sopenharmony_ci};
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_cimodule_pci_driver(ali1563_pci_driver);
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
443