Linux Audio

Check our new training course

Loading...
v5.9
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3    Copyright (c) 2002,2003 Alexander Malysh <amalysh@web.de>
  4
 
 
 
 
 
 
 
 
 
  5*/
  6
  7/*
  8   Status: beta
  9
 10   Supports:
 11	SIS 630
 12	SIS 730
 13	SIS 964
 14
 15   Notable differences between chips:
 16	+------------------------+--------------------+-------------------+
 17	|                        |     SIS630/730     |      SIS964       |
 18	+------------------------+--------------------+-------------------+
 19	| Clock                  | 14kHz/56kHz        | 55.56kHz/27.78kHz |
 20	| SMBus registers offset | 0x80               | 0xE0              |
 21	| SMB_CNT                | Bit 1 = Slave Busy | Bit 1 = Bus probe |
 22	|         (not used yet) | Bit 3 is reserved  | Bit 3 = Last byte |
 23	| SMB_PCOUNT		 | Offset + 0x06      | Offset + 0x14     |
 24	| SMB_COUNT              | 4:0 bits           | 5:0 bits          |
 25	+------------------------+--------------------+-------------------+
 26	(Other differences don't affect the functions provided by the driver)
 27
 28   Note: we assume there can only be one device, with one SMBus interface.
 29*/
 30
 31#include <linux/kernel.h>
 32#include <linux/module.h>
 33#include <linux/delay.h>
 34#include <linux/pci.h>
 35#include <linux/ioport.h>
 36#include <linux/i2c.h>
 37#include <linux/acpi.h>
 38#include <linux/io.h>
 39
 40/* SIS964 id is defined here as we are the only file using it */
 41#define PCI_DEVICE_ID_SI_964	0x0964
 42
 43/* SIS630/730/964 SMBus registers */
 44#define SMB_STS			0x00	/* status */
 45#define SMB_CNT			0x02	/* control */
 46#define SMBHOST_CNT		0x03	/* host control */
 47#define SMB_ADDR		0x04	/* address */
 48#define SMB_CMD			0x05	/* command */
 49#define SMB_COUNT		0x07	/* byte count */
 50#define SMB_BYTE		0x08	/* ~0x8F data byte field */
 51
 52/* SMB_STS register */
 53#define BYTE_DONE_STS		0x10	/* Byte Done Status / Block Array */
 54#define SMBCOL_STS		0x04	/* Collision */
 55#define SMBERR_STS		0x02	/* Device error */
 56
 57/* SMB_CNT register */
 58#define MSTO_EN			0x40	/* Host Master Timeout Enable */
 59#define SMBCLK_SEL		0x20	/* Host master clock selection */
 60#define SMB_PROBE		0x02	/* Bus Probe/Slave busy */
 61#define SMB_HOSTBUSY		0x01	/* Host Busy */
 62
 63/* SMBHOST_CNT register */
 64#define SMB_KILL		0x20	/* Kill */
 65#define SMB_START		0x10	/* Start */
 66
 67/* register count for request_region
 68 * As we don't use SMB_PCOUNT, 20 is ok for SiS630 and SiS964
 69 */
 70#define SIS630_SMB_IOREGION	20
 71
 72/* PCI address constants */
 73/* acpi base address register  */
 74#define SIS630_ACPI_BASE_REG	0x74
 75/* bios control register */
 76#define SIS630_BIOS_CTL_REG	0x40
 77
 78/* Other settings */
 79#define MAX_TIMEOUT		500
 80
 81/* SIS630 constants */
 82#define SIS630_QUICK		0x00
 83#define SIS630_BYTE		0x01
 84#define SIS630_BYTE_DATA	0x02
 85#define SIS630_WORD_DATA	0x03
 86#define SIS630_PCALL		0x04
 87#define SIS630_BLOCK_DATA	0x05
 88
 89static struct pci_driver sis630_driver;
 90
 91/* insmod parameters */
 92static bool high_clock;
 93static bool force;
 94module_param(high_clock, bool, 0);
 95MODULE_PARM_DESC(high_clock,
 96	"Set Host Master Clock to 56KHz (default 14KHz) (SIS630/730 only).");
 97module_param(force, bool, 0);
 98MODULE_PARM_DESC(force, "Forcibly enable the SIS630. DANGEROUS!");
 99
100/* SMBus base adress */
101static unsigned short smbus_base;
102
103/* supported chips */
104static int supported[] = {
105	PCI_DEVICE_ID_SI_630,
106	PCI_DEVICE_ID_SI_730,
107	PCI_DEVICE_ID_SI_760,
108	0 /* terminates the list */
109};
110
111static inline u8 sis630_read(u8 reg)
112{
113	return inb(smbus_base + reg);
114}
115
116static inline void sis630_write(u8 reg, u8 data)
117{
118	outb(data, smbus_base + reg);
119}
120
121static int sis630_transaction_start(struct i2c_adapter *adap, int size,
122				    u8 *oldclock)
123{
124	int temp;
125
126	/* Make sure the SMBus host is ready to start transmitting. */
127	temp = sis630_read(SMB_CNT);
128	if ((temp & (SMB_PROBE | SMB_HOSTBUSY)) != 0x00) {
129		dev_dbg(&adap->dev, "SMBus busy (%02x). Resetting...\n", temp);
130		/* kill smbus transaction */
131		sis630_write(SMBHOST_CNT, SMB_KILL);
132
133		temp = sis630_read(SMB_CNT);
134		if (temp & (SMB_PROBE | SMB_HOSTBUSY)) {
135			dev_dbg(&adap->dev, "Failed! (%02x)\n", temp);
136			return -EBUSY;
137		} else {
138			dev_dbg(&adap->dev, "Successful!\n");
139		}
140	}
141
142	/* save old clock, so we can prevent machine for hung */
143	*oldclock = sis630_read(SMB_CNT);
144
145	dev_dbg(&adap->dev, "saved clock 0x%02x\n", *oldclock);
146
147	/* disable timeout interrupt,
148	 * set Host Master Clock to 56KHz if requested */
149	if (high_clock)
150		sis630_write(SMB_CNT, SMBCLK_SEL);
151	else
152		sis630_write(SMB_CNT, (*oldclock & ~MSTO_EN));
153
154	/* clear all sticky bits */
155	temp = sis630_read(SMB_STS);
156	sis630_write(SMB_STS, temp & 0x1e);
157
158	/* start the transaction by setting bit 4 and size */
159	sis630_write(SMBHOST_CNT, SMB_START | (size & 0x07));
160
161	return 0;
162}
163
164static int sis630_transaction_wait(struct i2c_adapter *adap, int size)
165{
166	int temp, result = 0, timeout = 0;
167
168	/* We will always wait for a fraction of a second! */
169	do {
170		msleep(1);
171		temp = sis630_read(SMB_STS);
172		/* check if block transmitted */
173		if (size == SIS630_BLOCK_DATA && (temp & BYTE_DONE_STS))
174			break;
175	} while (!(temp & 0x0e) && (timeout++ < MAX_TIMEOUT));
176
177	/* If the SMBus is still busy, we give up */
178	if (timeout > MAX_TIMEOUT) {
179		dev_dbg(&adap->dev, "SMBus Timeout!\n");
180		result = -ETIMEDOUT;
181	}
182
183	if (temp & SMBERR_STS) {
184		dev_dbg(&adap->dev, "Error: Failed bus transaction\n");
185		result = -ENXIO;
186	}
187
188	if (temp & SMBCOL_STS) {
189		dev_err(&adap->dev, "Bus collision!\n");
190		result = -EAGAIN;
191	}
192
193	return result;
194}
195
196static void sis630_transaction_end(struct i2c_adapter *adap, u8 oldclock)
197{
198	/* clear all status "sticky" bits */
199	sis630_write(SMB_STS, 0xFF);
200
201	dev_dbg(&adap->dev,
202		"SMB_CNT before clock restore 0x%02x\n", sis630_read(SMB_CNT));
203
204	/*
205	 * restore old Host Master Clock if high_clock is set
206	 * and oldclock was not 56KHz
207	 */
208	if (high_clock && !(oldclock & SMBCLK_SEL))
209		sis630_write(SMB_CNT, sis630_read(SMB_CNT) & ~SMBCLK_SEL);
210
211	dev_dbg(&adap->dev,
212		"SMB_CNT after clock restore 0x%02x\n", sis630_read(SMB_CNT));
213}
214
215static int sis630_transaction(struct i2c_adapter *adap, int size)
216{
217	int result = 0;
218	u8 oldclock = 0;
219
220	result = sis630_transaction_start(adap, size, &oldclock);
221	if (!result) {
222		result = sis630_transaction_wait(adap, size);
223		sis630_transaction_end(adap, oldclock);
224	}
225
226	return result;
227}
228
229static int sis630_block_data(struct i2c_adapter *adap,
230			     union i2c_smbus_data *data, int read_write)
231{
232	int i, len = 0, rc = 0;
233	u8 oldclock = 0;
234
235	if (read_write == I2C_SMBUS_WRITE) {
236		len = data->block[0];
237		if (len < 0)
238			len = 0;
239		else if (len > 32)
240			len = 32;
241		sis630_write(SMB_COUNT, len);
242		for (i = 1; i <= len; i++) {
243			dev_dbg(&adap->dev,
244				"set data 0x%02x\n", data->block[i]);
245			/* set data */
246			sis630_write(SMB_BYTE + (i - 1) % 8, data->block[i]);
247			if (i == 8 || (len < 8 && i == len)) {
248				dev_dbg(&adap->dev,
249					"start trans len=%d i=%d\n", len, i);
250				/* first transaction */
251				rc = sis630_transaction_start(adap,
252						SIS630_BLOCK_DATA, &oldclock);
253				if (rc)
254					return rc;
255			} else if ((i - 1) % 8 == 7 || i == len) {
256				dev_dbg(&adap->dev,
257					"trans_wait len=%d i=%d\n", len, i);
258				if (i > 8) {
259					dev_dbg(&adap->dev,
260						"clear smbary_sts"
261						" len=%d i=%d\n", len, i);
262					/*
263					   If this is not first transaction,
264					   we must clear sticky bit.
265					   clear SMBARY_STS
266					*/
267					sis630_write(SMB_STS, BYTE_DONE_STS);
268				}
269				rc = sis630_transaction_wait(adap,
270						SIS630_BLOCK_DATA);
271				if (rc) {
272					dev_dbg(&adap->dev,
273						"trans_wait failed\n");
274					break;
275				}
276			}
277		}
278	} else {
279		/* read request */
280		data->block[0] = len = 0;
281		rc = sis630_transaction_start(adap,
282				SIS630_BLOCK_DATA, &oldclock);
283		if (rc)
284			return rc;
285		do {
286			rc = sis630_transaction_wait(adap, SIS630_BLOCK_DATA);
287			if (rc) {
288				dev_dbg(&adap->dev, "trans_wait failed\n");
289				break;
290			}
291			/* if this first transaction then read byte count */
292			if (len == 0)
293				data->block[0] = sis630_read(SMB_COUNT);
294
295			/* just to be sure */
296			if (data->block[0] > 32)
297				data->block[0] = 32;
298
299			dev_dbg(&adap->dev,
300				"block data read len=0x%x\n", data->block[0]);
301
302			for (i = 0; i < 8 && len < data->block[0]; i++, len++) {
303				dev_dbg(&adap->dev,
304					"read i=%d len=%d\n", i, len);
305				data->block[len + 1] = sis630_read(SMB_BYTE +
306								   i);
307			}
308
309			dev_dbg(&adap->dev,
310				"clear smbary_sts len=%d i=%d\n", len, i);
311
312			/* clear SMBARY_STS */
313			sis630_write(SMB_STS, BYTE_DONE_STS);
314		} while (len < data->block[0]);
315	}
316
317	sis630_transaction_end(adap, oldclock);
318
319	return rc;
320}
321
322/* Return negative errno on error. */
323static s32 sis630_access(struct i2c_adapter *adap, u16 addr,
324			 unsigned short flags, char read_write,
325			 u8 command, int size, union i2c_smbus_data *data)
326{
327	int status;
328
329	switch (size) {
330	case I2C_SMBUS_QUICK:
331		sis630_write(SMB_ADDR,
332			     ((addr & 0x7f) << 1) | (read_write & 0x01));
333		size = SIS630_QUICK;
334		break;
335	case I2C_SMBUS_BYTE:
336		sis630_write(SMB_ADDR,
337			     ((addr & 0x7f) << 1) | (read_write & 0x01));
338		if (read_write == I2C_SMBUS_WRITE)
339			sis630_write(SMB_CMD, command);
340		size = SIS630_BYTE;
341		break;
342	case I2C_SMBUS_BYTE_DATA:
343		sis630_write(SMB_ADDR,
344			     ((addr & 0x7f) << 1) | (read_write & 0x01));
345		sis630_write(SMB_CMD, command);
346		if (read_write == I2C_SMBUS_WRITE)
347			sis630_write(SMB_BYTE, data->byte);
348		size = SIS630_BYTE_DATA;
349		break;
350	case I2C_SMBUS_PROC_CALL:
351	case I2C_SMBUS_WORD_DATA:
352		sis630_write(SMB_ADDR,
353			     ((addr & 0x7f) << 1) | (read_write & 0x01));
354		sis630_write(SMB_CMD, command);
355		if (read_write == I2C_SMBUS_WRITE) {
356			sis630_write(SMB_BYTE, data->word & 0xff);
357			sis630_write(SMB_BYTE + 1, (data->word & 0xff00) >> 8);
358		}
359		size = (size == I2C_SMBUS_PROC_CALL ?
360			SIS630_PCALL : SIS630_WORD_DATA);
361		break;
362	case I2C_SMBUS_BLOCK_DATA:
363		sis630_write(SMB_ADDR,
364			     ((addr & 0x7f) << 1) | (read_write & 0x01));
365		sis630_write(SMB_CMD, command);
366		size = SIS630_BLOCK_DATA;
367		return sis630_block_data(adap, data, read_write);
368	default:
369		dev_warn(&adap->dev, "Unsupported transaction %d\n", size);
370		return -EOPNOTSUPP;
371	}
372
373	status = sis630_transaction(adap, size);
374	if (status)
375		return status;
376
377	if ((size != SIS630_PCALL) &&
378		((read_write == I2C_SMBUS_WRITE) || (size == SIS630_QUICK))) {
379		return 0;
380	}
381
382	switch (size) {
383	case SIS630_BYTE:
384	case SIS630_BYTE_DATA:
385		data->byte = sis630_read(SMB_BYTE);
386		break;
387	case SIS630_PCALL:
388	case SIS630_WORD_DATA:
389		data->word = sis630_read(SMB_BYTE) +
390			     (sis630_read(SMB_BYTE + 1) << 8);
391		break;
392	}
393
394	return 0;
395}
396
397static u32 sis630_func(struct i2c_adapter *adapter)
398{
399	return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
400		I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
401		I2C_FUNC_SMBUS_PROC_CALL | I2C_FUNC_SMBUS_BLOCK_DATA;
402}
403
404static int sis630_setup(struct pci_dev *sis630_dev)
405{
406	unsigned char b;
407	struct pci_dev *dummy = NULL;
408	int retval, i;
409	/* acpi base address */
410	unsigned short acpi_base;
411
412	/* check for supported SiS devices */
413	for (i = 0; supported[i] > 0; i++) {
414		dummy = pci_get_device(PCI_VENDOR_ID_SI, supported[i], dummy);
415		if (dummy)
416			break; /* found */
417	}
418
419	if (dummy) {
420		pci_dev_put(dummy);
421	} else if (force) {
422		dev_err(&sis630_dev->dev,
423			"WARNING: Can't detect SIS630 compatible device, but "
424			"loading because of force option enabled\n");
425	} else {
426		return -ENODEV;
427	}
428
429	/*
430	   Enable ACPI first , so we can accsess reg 74-75
431	   in acpi io space and read acpi base addr
432	*/
433	if (pci_read_config_byte(sis630_dev, SIS630_BIOS_CTL_REG, &b)) {
434		dev_err(&sis630_dev->dev, "Error: Can't read bios ctl reg\n");
435		retval = -ENODEV;
436		goto exit;
437	}
438	/* if ACPI already enabled , do nothing */
439	if (!(b & 0x80) &&
440	    pci_write_config_byte(sis630_dev, SIS630_BIOS_CTL_REG, b | 0x80)) {
441		dev_err(&sis630_dev->dev, "Error: Can't enable ACPI\n");
442		retval = -ENODEV;
443		goto exit;
444	}
445
446	/* Determine the ACPI base address */
447	if (pci_read_config_word(sis630_dev,
448				 SIS630_ACPI_BASE_REG, &acpi_base)) {
449		dev_err(&sis630_dev->dev,
450			"Error: Can't determine ACPI base address\n");
451		retval = -ENODEV;
452		goto exit;
453	}
454
455	dev_dbg(&sis630_dev->dev, "ACPI base at 0x%04hx\n", acpi_base);
456
457	if (supported[i] == PCI_DEVICE_ID_SI_760)
458		smbus_base = acpi_base + 0xE0;
459	else
460		smbus_base = acpi_base + 0x80;
461
462	dev_dbg(&sis630_dev->dev, "SMBus base at 0x%04hx\n", smbus_base);
463
464	retval = acpi_check_region(smbus_base + SMB_STS, SIS630_SMB_IOREGION,
465				   sis630_driver.name);
466	if (retval)
467		goto exit;
468
469	/* Everything is happy, let's grab the memory and set things up. */
470	if (!request_region(smbus_base + SMB_STS, SIS630_SMB_IOREGION,
471			    sis630_driver.name)) {
472		dev_err(&sis630_dev->dev,
473			"I/O Region 0x%04x-0x%04x for SMBus already in use.\n",
474			smbus_base + SMB_STS,
475			smbus_base + SMB_STS + SIS630_SMB_IOREGION - 1);
476		retval = -EBUSY;
477		goto exit;
478	}
479
480	retval = 0;
481
482exit:
483	if (retval)
484		smbus_base = 0;
485	return retval;
486}
487
488
489static const struct i2c_algorithm smbus_algorithm = {
490	.smbus_xfer	= sis630_access,
491	.functionality	= sis630_func,
492};
493
494static struct i2c_adapter sis630_adapter = {
495	.owner		= THIS_MODULE,
496	.class		= I2C_CLASS_HWMON | I2C_CLASS_SPD,
497	.algo		= &smbus_algorithm,
498	.retries	= 3
499};
500
501static const struct pci_device_id sis630_ids[] = {
502	{ PCI_DEVICE(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_503) },
503	{ PCI_DEVICE(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_LPC) },
504	{ PCI_DEVICE(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_964) },
505	{ 0, }
506};
507
508MODULE_DEVICE_TABLE(pci, sis630_ids);
509
510static int sis630_probe(struct pci_dev *dev, const struct pci_device_id *id)
511{
512	if (sis630_setup(dev)) {
513		dev_err(&dev->dev,
514			"SIS630 compatible bus not detected, "
515			"module not inserted.\n");
516		return -ENODEV;
517	}
518
519	/* set up the sysfs linkage to our parent device */
520	sis630_adapter.dev.parent = &dev->dev;
521
522	snprintf(sis630_adapter.name, sizeof(sis630_adapter.name),
523		 "SMBus SIS630 adapter at %04x", smbus_base + SMB_STS);
524
525	return i2c_add_adapter(&sis630_adapter);
526}
527
528static void sis630_remove(struct pci_dev *dev)
529{
530	if (smbus_base) {
531		i2c_del_adapter(&sis630_adapter);
532		release_region(smbus_base + SMB_STS, SIS630_SMB_IOREGION);
533		smbus_base = 0;
534	}
535}
536
537
538static struct pci_driver sis630_driver = {
539	.name		= "sis630_smbus",
540	.id_table	= sis630_ids,
541	.probe		= sis630_probe,
542	.remove		= sis630_remove,
543};
544
545module_pci_driver(sis630_driver);
546
547MODULE_LICENSE("GPL");
548MODULE_AUTHOR("Alexander Malysh <amalysh@web.de>");
549MODULE_DESCRIPTION("SIS630 SMBus driver");
v4.10.11
 
  1/*
  2    Copyright (c) 2002,2003 Alexander Malysh <amalysh@web.de>
  3
  4    This program is free software; you can redistribute it and/or modify
  5    it under the terms of the GNU General Public License as published by
  6    the Free Software Foundation; either version 2 of the License, or
  7    (at your option) any later version.
  8
  9    This program is distributed in the hope that it will be useful,
 10    but WITHOUT ANY WARRANTY; without even the implied warranty of
 11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12    GNU General Public License for more details.
 13*/
 14
 15/*
 16   Status: beta
 17
 18   Supports:
 19	SIS 630
 20	SIS 730
 21	SIS 964
 22
 23   Notable differences between chips:
 24	+------------------------+--------------------+-------------------+
 25	|                        |     SIS630/730     |      SIS964       |
 26	+------------------------+--------------------+-------------------+
 27	| Clock                  | 14kHz/56kHz        | 55.56kHz/27.78kHz |
 28	| SMBus registers offset | 0x80               | 0xE0              |
 29	| SMB_CNT                | Bit 1 = Slave Busy | Bit 1 = Bus probe |
 30	|         (not used yet) | Bit 3 is reserved  | Bit 3 = Last byte |
 31	| SMB_PCOUNT		 | Offset + 0x06      | Offset + 0x14     |
 32	| SMB_COUNT              | 4:0 bits           | 5:0 bits          |
 33	+------------------------+--------------------+-------------------+
 34	(Other differences don't affect the functions provided by the driver)
 35
 36   Note: we assume there can only be one device, with one SMBus interface.
 37*/
 38
 39#include <linux/kernel.h>
 40#include <linux/module.h>
 41#include <linux/delay.h>
 42#include <linux/pci.h>
 43#include <linux/ioport.h>
 44#include <linux/i2c.h>
 45#include <linux/acpi.h>
 46#include <linux/io.h>
 47
 48/* SIS964 id is defined here as we are the only file using it */
 49#define PCI_DEVICE_ID_SI_964	0x0964
 50
 51/* SIS630/730/964 SMBus registers */
 52#define SMB_STS			0x00	/* status */
 53#define SMB_CNT			0x02	/* control */
 54#define SMBHOST_CNT		0x03	/* host control */
 55#define SMB_ADDR		0x04	/* address */
 56#define SMB_CMD			0x05	/* command */
 57#define SMB_COUNT		0x07	/* byte count */
 58#define SMB_BYTE		0x08	/* ~0x8F data byte field */
 59
 60/* SMB_STS register */
 61#define BYTE_DONE_STS		0x10	/* Byte Done Status / Block Array */
 62#define SMBCOL_STS		0x04	/* Collision */
 63#define SMBERR_STS		0x02	/* Device error */
 64
 65/* SMB_CNT register */
 66#define MSTO_EN			0x40	/* Host Master Timeout Enable */
 67#define SMBCLK_SEL		0x20	/* Host master clock selection */
 68#define SMB_PROBE		0x02	/* Bus Probe/Slave busy */
 69#define SMB_HOSTBUSY		0x01	/* Host Busy */
 70
 71/* SMBHOST_CNT register */
 72#define SMB_KILL		0x20	/* Kill */
 73#define SMB_START		0x10	/* Start */
 74
 75/* register count for request_region
 76 * As we don't use SMB_PCOUNT, 20 is ok for SiS630 and SiS964
 77 */
 78#define SIS630_SMB_IOREGION	20
 79
 80/* PCI address constants */
 81/* acpi base address register  */
 82#define SIS630_ACPI_BASE_REG	0x74
 83/* bios control register */
 84#define SIS630_BIOS_CTL_REG	0x40
 85
 86/* Other settings */
 87#define MAX_TIMEOUT		500
 88
 89/* SIS630 constants */
 90#define SIS630_QUICK		0x00
 91#define SIS630_BYTE		0x01
 92#define SIS630_BYTE_DATA	0x02
 93#define SIS630_WORD_DATA	0x03
 94#define SIS630_PCALL		0x04
 95#define SIS630_BLOCK_DATA	0x05
 96
 97static struct pci_driver sis630_driver;
 98
 99/* insmod parameters */
100static bool high_clock;
101static bool force;
102module_param(high_clock, bool, 0);
103MODULE_PARM_DESC(high_clock,
104	"Set Host Master Clock to 56KHz (default 14KHz) (SIS630/730 only).");
105module_param(force, bool, 0);
106MODULE_PARM_DESC(force, "Forcibly enable the SIS630. DANGEROUS!");
107
108/* SMBus base adress */
109static unsigned short smbus_base;
110
111/* supported chips */
112static int supported[] = {
113	PCI_DEVICE_ID_SI_630,
114	PCI_DEVICE_ID_SI_730,
115	PCI_DEVICE_ID_SI_760,
116	0 /* terminates the list */
117};
118
119static inline u8 sis630_read(u8 reg)
120{
121	return inb(smbus_base + reg);
122}
123
124static inline void sis630_write(u8 reg, u8 data)
125{
126	outb(data, smbus_base + reg);
127}
128
129static int sis630_transaction_start(struct i2c_adapter *adap, int size,
130				    u8 *oldclock)
131{
132	int temp;
133
134	/* Make sure the SMBus host is ready to start transmitting. */
135	temp = sis630_read(SMB_CNT);
136	if ((temp & (SMB_PROBE | SMB_HOSTBUSY)) != 0x00) {
137		dev_dbg(&adap->dev, "SMBus busy (%02x). Resetting...\n", temp);
138		/* kill smbus transaction */
139		sis630_write(SMBHOST_CNT, SMB_KILL);
140
141		temp = sis630_read(SMB_CNT);
142		if (temp & (SMB_PROBE | SMB_HOSTBUSY)) {
143			dev_dbg(&adap->dev, "Failed! (%02x)\n", temp);
144			return -EBUSY;
145		} else {
146			dev_dbg(&adap->dev, "Successful!\n");
147		}
148	}
149
150	/* save old clock, so we can prevent machine for hung */
151	*oldclock = sis630_read(SMB_CNT);
152
153	dev_dbg(&adap->dev, "saved clock 0x%02x\n", *oldclock);
154
155	/* disable timeout interrupt,
156	 * set Host Master Clock to 56KHz if requested */
157	if (high_clock)
158		sis630_write(SMB_CNT, SMBCLK_SEL);
159	else
160		sis630_write(SMB_CNT, (*oldclock & ~MSTO_EN));
161
162	/* clear all sticky bits */
163	temp = sis630_read(SMB_STS);
164	sis630_write(SMB_STS, temp & 0x1e);
165
166	/* start the transaction by setting bit 4 and size */
167	sis630_write(SMBHOST_CNT, SMB_START | (size & 0x07));
168
169	return 0;
170}
171
172static int sis630_transaction_wait(struct i2c_adapter *adap, int size)
173{
174	int temp, result = 0, timeout = 0;
175
176	/* We will always wait for a fraction of a second! */
177	do {
178		msleep(1);
179		temp = sis630_read(SMB_STS);
180		/* check if block transmitted */
181		if (size == SIS630_BLOCK_DATA && (temp & BYTE_DONE_STS))
182			break;
183	} while (!(temp & 0x0e) && (timeout++ < MAX_TIMEOUT));
184
185	/* If the SMBus is still busy, we give up */
186	if (timeout > MAX_TIMEOUT) {
187		dev_dbg(&adap->dev, "SMBus Timeout!\n");
188		result = -ETIMEDOUT;
189	}
190
191	if (temp & SMBERR_STS) {
192		dev_dbg(&adap->dev, "Error: Failed bus transaction\n");
193		result = -ENXIO;
194	}
195
196	if (temp & SMBCOL_STS) {
197		dev_err(&adap->dev, "Bus collision!\n");
198		result = -EAGAIN;
199	}
200
201	return result;
202}
203
204static void sis630_transaction_end(struct i2c_adapter *adap, u8 oldclock)
205{
206	/* clear all status "sticky" bits */
207	sis630_write(SMB_STS, 0xFF);
208
209	dev_dbg(&adap->dev,
210		"SMB_CNT before clock restore 0x%02x\n", sis630_read(SMB_CNT));
211
212	/*
213	 * restore old Host Master Clock if high_clock is set
214	 * and oldclock was not 56KHz
215	 */
216	if (high_clock && !(oldclock & SMBCLK_SEL))
217		sis630_write(SMB_CNT, sis630_read(SMB_CNT) & ~SMBCLK_SEL);
218
219	dev_dbg(&adap->dev,
220		"SMB_CNT after clock restore 0x%02x\n", sis630_read(SMB_CNT));
221}
222
223static int sis630_transaction(struct i2c_adapter *adap, int size)
224{
225	int result = 0;
226	u8 oldclock = 0;
227
228	result = sis630_transaction_start(adap, size, &oldclock);
229	if (!result) {
230		result = sis630_transaction_wait(adap, size);
231		sis630_transaction_end(adap, oldclock);
232	}
233
234	return result;
235}
236
237static int sis630_block_data(struct i2c_adapter *adap,
238			     union i2c_smbus_data *data, int read_write)
239{
240	int i, len = 0, rc = 0;
241	u8 oldclock = 0;
242
243	if (read_write == I2C_SMBUS_WRITE) {
244		len = data->block[0];
245		if (len < 0)
246			len = 0;
247		else if (len > 32)
248			len = 32;
249		sis630_write(SMB_COUNT, len);
250		for (i = 1; i <= len; i++) {
251			dev_dbg(&adap->dev,
252				"set data 0x%02x\n", data->block[i]);
253			/* set data */
254			sis630_write(SMB_BYTE + (i - 1) % 8, data->block[i]);
255			if (i == 8 || (len < 8 && i == len)) {
256				dev_dbg(&adap->dev,
257					"start trans len=%d i=%d\n", len, i);
258				/* first transaction */
259				rc = sis630_transaction_start(adap,
260						SIS630_BLOCK_DATA, &oldclock);
261				if (rc)
262					return rc;
263			} else if ((i - 1) % 8 == 7 || i == len) {
264				dev_dbg(&adap->dev,
265					"trans_wait len=%d i=%d\n", len, i);
266				if (i > 8) {
267					dev_dbg(&adap->dev,
268						"clear smbary_sts"
269						" len=%d i=%d\n", len, i);
270					/*
271					   If this is not first transaction,
272					   we must clear sticky bit.
273					   clear SMBARY_STS
274					*/
275					sis630_write(SMB_STS, BYTE_DONE_STS);
276				}
277				rc = sis630_transaction_wait(adap,
278						SIS630_BLOCK_DATA);
279				if (rc) {
280					dev_dbg(&adap->dev,
281						"trans_wait failed\n");
282					break;
283				}
284			}
285		}
286	} else {
287		/* read request */
288		data->block[0] = len = 0;
289		rc = sis630_transaction_start(adap,
290				SIS630_BLOCK_DATA, &oldclock);
291		if (rc)
292			return rc;
293		do {
294			rc = sis630_transaction_wait(adap, SIS630_BLOCK_DATA);
295			if (rc) {
296				dev_dbg(&adap->dev, "trans_wait failed\n");
297				break;
298			}
299			/* if this first transaction then read byte count */
300			if (len == 0)
301				data->block[0] = sis630_read(SMB_COUNT);
302
303			/* just to be sure */
304			if (data->block[0] > 32)
305				data->block[0] = 32;
306
307			dev_dbg(&adap->dev,
308				"block data read len=0x%x\n", data->block[0]);
309
310			for (i = 0; i < 8 && len < data->block[0]; i++, len++) {
311				dev_dbg(&adap->dev,
312					"read i=%d len=%d\n", i, len);
313				data->block[len + 1] = sis630_read(SMB_BYTE +
314								   i);
315			}
316
317			dev_dbg(&adap->dev,
318				"clear smbary_sts len=%d i=%d\n", len, i);
319
320			/* clear SMBARY_STS */
321			sis630_write(SMB_STS, BYTE_DONE_STS);
322		} while (len < data->block[0]);
323	}
324
325	sis630_transaction_end(adap, oldclock);
326
327	return rc;
328}
329
330/* Return negative errno on error. */
331static s32 sis630_access(struct i2c_adapter *adap, u16 addr,
332			 unsigned short flags, char read_write,
333			 u8 command, int size, union i2c_smbus_data *data)
334{
335	int status;
336
337	switch (size) {
338	case I2C_SMBUS_QUICK:
339		sis630_write(SMB_ADDR,
340			     ((addr & 0x7f) << 1) | (read_write & 0x01));
341		size = SIS630_QUICK;
342		break;
343	case I2C_SMBUS_BYTE:
344		sis630_write(SMB_ADDR,
345			     ((addr & 0x7f) << 1) | (read_write & 0x01));
346		if (read_write == I2C_SMBUS_WRITE)
347			sis630_write(SMB_CMD, command);
348		size = SIS630_BYTE;
349		break;
350	case I2C_SMBUS_BYTE_DATA:
351		sis630_write(SMB_ADDR,
352			     ((addr & 0x7f) << 1) | (read_write & 0x01));
353		sis630_write(SMB_CMD, command);
354		if (read_write == I2C_SMBUS_WRITE)
355			sis630_write(SMB_BYTE, data->byte);
356		size = SIS630_BYTE_DATA;
357		break;
358	case I2C_SMBUS_PROC_CALL:
359	case I2C_SMBUS_WORD_DATA:
360		sis630_write(SMB_ADDR,
361			     ((addr & 0x7f) << 1) | (read_write & 0x01));
362		sis630_write(SMB_CMD, command);
363		if (read_write == I2C_SMBUS_WRITE) {
364			sis630_write(SMB_BYTE, data->word & 0xff);
365			sis630_write(SMB_BYTE + 1, (data->word & 0xff00) >> 8);
366		}
367		size = (size == I2C_SMBUS_PROC_CALL ?
368			SIS630_PCALL : SIS630_WORD_DATA);
369		break;
370	case I2C_SMBUS_BLOCK_DATA:
371		sis630_write(SMB_ADDR,
372			     ((addr & 0x7f) << 1) | (read_write & 0x01));
373		sis630_write(SMB_CMD, command);
374		size = SIS630_BLOCK_DATA;
375		return sis630_block_data(adap, data, read_write);
376	default:
377		dev_warn(&adap->dev, "Unsupported transaction %d\n", size);
378		return -EOPNOTSUPP;
379	}
380
381	status = sis630_transaction(adap, size);
382	if (status)
383		return status;
384
385	if ((size != SIS630_PCALL) &&
386		((read_write == I2C_SMBUS_WRITE) || (size == SIS630_QUICK))) {
387		return 0;
388	}
389
390	switch (size) {
391	case SIS630_BYTE:
392	case SIS630_BYTE_DATA:
393		data->byte = sis630_read(SMB_BYTE);
394		break;
395	case SIS630_PCALL:
396	case SIS630_WORD_DATA:
397		data->word = sis630_read(SMB_BYTE) +
398			     (sis630_read(SMB_BYTE + 1) << 8);
399		break;
400	}
401
402	return 0;
403}
404
405static u32 sis630_func(struct i2c_adapter *adapter)
406{
407	return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
408		I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
409		I2C_FUNC_SMBUS_PROC_CALL | I2C_FUNC_SMBUS_BLOCK_DATA;
410}
411
412static int sis630_setup(struct pci_dev *sis630_dev)
413{
414	unsigned char b;
415	struct pci_dev *dummy = NULL;
416	int retval, i;
417	/* acpi base address */
418	unsigned short acpi_base;
419
420	/* check for supported SiS devices */
421	for (i = 0; supported[i] > 0; i++) {
422		dummy = pci_get_device(PCI_VENDOR_ID_SI, supported[i], dummy);
423		if (dummy)
424			break; /* found */
425	}
426
427	if (dummy) {
428		pci_dev_put(dummy);
429	} else if (force) {
430		dev_err(&sis630_dev->dev,
431			"WARNING: Can't detect SIS630 compatible device, but "
432			"loading because of force option enabled\n");
433	} else {
434		return -ENODEV;
435	}
436
437	/*
438	   Enable ACPI first , so we can accsess reg 74-75
439	   in acpi io space and read acpi base addr
440	*/
441	if (pci_read_config_byte(sis630_dev, SIS630_BIOS_CTL_REG, &b)) {
442		dev_err(&sis630_dev->dev, "Error: Can't read bios ctl reg\n");
443		retval = -ENODEV;
444		goto exit;
445	}
446	/* if ACPI already enabled , do nothing */
447	if (!(b & 0x80) &&
448	    pci_write_config_byte(sis630_dev, SIS630_BIOS_CTL_REG, b | 0x80)) {
449		dev_err(&sis630_dev->dev, "Error: Can't enable ACPI\n");
450		retval = -ENODEV;
451		goto exit;
452	}
453
454	/* Determine the ACPI base address */
455	if (pci_read_config_word(sis630_dev,
456				 SIS630_ACPI_BASE_REG, &acpi_base)) {
457		dev_err(&sis630_dev->dev,
458			"Error: Can't determine ACPI base address\n");
459		retval = -ENODEV;
460		goto exit;
461	}
462
463	dev_dbg(&sis630_dev->dev, "ACPI base at 0x%04hx\n", acpi_base);
464
465	if (supported[i] == PCI_DEVICE_ID_SI_760)
466		smbus_base = acpi_base + 0xE0;
467	else
468		smbus_base = acpi_base + 0x80;
469
470	dev_dbg(&sis630_dev->dev, "SMBus base at 0x%04hx\n", smbus_base);
471
472	retval = acpi_check_region(smbus_base + SMB_STS, SIS630_SMB_IOREGION,
473				   sis630_driver.name);
474	if (retval)
475		goto exit;
476
477	/* Everything is happy, let's grab the memory and set things up. */
478	if (!request_region(smbus_base + SMB_STS, SIS630_SMB_IOREGION,
479			    sis630_driver.name)) {
480		dev_err(&sis630_dev->dev,
481			"I/O Region 0x%04hx-0x%04hx for SMBus already in use.\n",
482			smbus_base + SMB_STS,
483			smbus_base + SMB_STS + SIS630_SMB_IOREGION - 1);
484		retval = -EBUSY;
485		goto exit;
486	}
487
488	retval = 0;
489
490exit:
491	if (retval)
492		smbus_base = 0;
493	return retval;
494}
495
496
497static const struct i2c_algorithm smbus_algorithm = {
498	.smbus_xfer	= sis630_access,
499	.functionality	= sis630_func,
500};
501
502static struct i2c_adapter sis630_adapter = {
503	.owner		= THIS_MODULE,
504	.class		= I2C_CLASS_HWMON | I2C_CLASS_SPD,
505	.algo		= &smbus_algorithm,
506	.retries	= 3
507};
508
509static const struct pci_device_id sis630_ids[] = {
510	{ PCI_DEVICE(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_503) },
511	{ PCI_DEVICE(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_LPC) },
512	{ PCI_DEVICE(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_964) },
513	{ 0, }
514};
515
516MODULE_DEVICE_TABLE(pci, sis630_ids);
517
518static int sis630_probe(struct pci_dev *dev, const struct pci_device_id *id)
519{
520	if (sis630_setup(dev)) {
521		dev_err(&dev->dev,
522			"SIS630 compatible bus not detected, "
523			"module not inserted.\n");
524		return -ENODEV;
525	}
526
527	/* set up the sysfs linkage to our parent device */
528	sis630_adapter.dev.parent = &dev->dev;
529
530	snprintf(sis630_adapter.name, sizeof(sis630_adapter.name),
531		 "SMBus SIS630 adapter at %04hx", smbus_base + SMB_STS);
532
533	return i2c_add_adapter(&sis630_adapter);
534}
535
536static void sis630_remove(struct pci_dev *dev)
537{
538	if (smbus_base) {
539		i2c_del_adapter(&sis630_adapter);
540		release_region(smbus_base + SMB_STS, SIS630_SMB_IOREGION);
541		smbus_base = 0;
542	}
543}
544
545
546static struct pci_driver sis630_driver = {
547	.name		= "sis630_smbus",
548	.id_table	= sis630_ids,
549	.probe		= sis630_probe,
550	.remove		= sis630_remove,
551};
552
553module_pci_driver(sis630_driver);
554
555MODULE_LICENSE("GPL");
556MODULE_AUTHOR("Alexander Malysh <amalysh@web.de>");
557MODULE_DESCRIPTION("SIS630 SMBus driver");