Linux Audio

Check our new training course

Loading...
v6.13.7
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * Copyright (c) 2000  Frodo Looijaard <frodol@dds.nl>,
  4 *                      Philip Edelbrock <phil@netroedge.com>,
  5 *                      Mark D. Studebaker <mdsxyz123@yahoo.com>,
  6 *                      Dan Eaton <dan.eaton@rocketlogix.com> and
  7 *                      Stephen Rousset <stephen.rousset@rocketlogix.com>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
  8*/
  9
 10/*
 11    This is the driver for the SMB Host controller on
 12    Acer Labs Inc. (ALI) M1535 South Bridge.
 13
 14    The M1535 is a South bridge for portable systems.
 15    It is very similar to the M15x3 South bridges also produced
 16    by Acer Labs Inc.  Some of the registers within the part
 17    have moved and some have been redefined slightly. Additionally,
 18    the sequencing of the SMBus transactions has been modified
 19    to be more consistent with the sequencing recommended by
 20    the manufacturer and observed through testing.  These
 21    changes are reflected in this driver and can be identified
 22    by comparing this driver to the i2c-ali15x3 driver.
 23    For an overview of these chips see http://www.acerlabs.com
 24
 25    The SMB controller is part of the 7101 device, which is an
 26    ACPI-compliant Power Management Unit (PMU).
 27
 28    The whole 7101 device has to be enabled for the SMB to work.
 29    You can't just enable the SMB alone.
 30    The SMB and the ACPI have separate I/O spaces.
 31    We make sure that the SMB is enabled. We leave the ACPI alone.
 32
 33    This driver controls the SMB Host only.
 34
 35    This driver does not use interrupts.
 36*/
 37
 38
 39/* Note: we assume there can only be one ALI1535, with one SMBus interface */
 40
 41#include <linux/module.h>
 42#include <linux/pci.h>
 43#include <linux/kernel.h>
 44#include <linux/stddef.h>
 45#include <linux/delay.h>
 46#include <linux/ioport.h>
 47#include <linux/i2c.h>
 48#include <linux/acpi.h>
 49#include <linux/io.h>
 50
 51
 52/* ALI1535 SMBus address offsets */
 53#define SMBHSTSTS	(0 + ali1535_smba)
 54#define SMBHSTTYP	(1 + ali1535_smba)
 55#define SMBHSTPORT	(2 + ali1535_smba)
 56#define SMBHSTCMD	(7 + ali1535_smba)
 57#define SMBHSTADD	(3 + ali1535_smba)
 58#define SMBHSTDAT0	(4 + ali1535_smba)
 59#define SMBHSTDAT1	(5 + ali1535_smba)
 60#define SMBBLKDAT	(6 + ali1535_smba)
 61
 62/* PCI Address Constants */
 63#define SMBCOM		0x004
 64#define SMBREV		0x008
 65#define SMBCFG		0x0D1
 66#define SMBBA		0x0E2
 67#define SMBHSTCFG	0x0F0
 68#define SMBCLK		0x0F2
 69
 70/* Other settings */
 71#define MAX_TIMEOUT		500	/* times 1/100 sec */
 72#define ALI1535_SMB_IOSIZE	32
 73
 74#define ALI1535_SMB_DEFAULTBASE	0x8040
 75
 76/* ALI1535 address lock bits */
 77#define ALI1535_LOCK		0x06	/* dwe */
 78
 79/* ALI1535 command constants */
 80#define ALI1535_QUICK		0x00
 81#define ALI1535_BYTE		0x10
 82#define ALI1535_BYTE_DATA	0x20
 83#define ALI1535_WORD_DATA	0x30
 84#define ALI1535_BLOCK_DATA	0x40
 85#define ALI1535_I2C_READ	0x60
 86
 87#define	ALI1535_DEV10B_EN	0x80	/* Enable 10-bit addressing in	*/
 88					/*  I2C read			*/
 89#define	ALI1535_T_OUT		0x08	/* Time-out Command (write)	*/
 90#define	ALI1535_A_HIGH_BIT9	0x08	/* Bit 9 of 10-bit address in	*/
 91					/* Alert-Response-Address	*/
 92					/* (read)			*/
 93#define	ALI1535_KILL		0x04	/* Kill Command (write)		*/
 94#define	ALI1535_A_HIGH_BIT8	0x04	/* Bit 8 of 10-bit address in	*/
 95					/*  Alert-Response-Address	*/
 96					/*  (read)			*/
 97
 98#define	ALI1535_D_HI_MASK	0x03	/* Mask for isolating bits 9-8	*/
 99					/*  of 10-bit address in I2C	*/
100					/*  Read Command		*/
101
102/* ALI1535 status register bits */
103#define ALI1535_STS_IDLE	0x04
104#define ALI1535_STS_BUSY	0x08	/* host busy */
105#define ALI1535_STS_DONE	0x10	/* transaction complete */
106#define ALI1535_STS_DEV		0x20	/* device error */
107#define ALI1535_STS_BUSERR	0x40	/* bus error    */
108#define ALI1535_STS_FAIL	0x80	/* failed bus transaction */
109#define ALI1535_STS_ERR		0xE0	/* all the bad error bits */
110
111#define ALI1535_BLOCK_CLR	0x04	/* reset block data index */
112
113/* ALI1535 device address register bits */
114#define	ALI1535_RD_ADDR		0x01	/* Read/Write Bit in Device	*/
115					/*  Address field		*/
116					/*  -> Write = 0		*/
117					/*  -> Read  = 1		*/
118#define	ALI1535_SMBIO_EN	0x04	/* SMB I/O Space enable		*/
119
120static struct pci_driver ali1535_driver;
121static unsigned long ali1535_smba;
122static unsigned short ali1535_offset;
123
124/* Detect whether a ALI1535 can be found, and initialize it, where necessary.
125   Note the differences between kernels with the old PCI BIOS interface and
126   newer kernels with the real PCI interface. In compat.h some things are
127   defined to make the transition easier. */
128static int ali1535_setup(struct pci_dev *dev)
129{
130	int retval;
131	unsigned char temp;
132
133	/* Check the following things:
134		- SMB I/O address is initialized
135		- Device is enabled
136		- We can use the addresses
137	*/
138
139	retval = pci_enable_device(dev);
140	if (retval) {
141		dev_err(&dev->dev, "ALI1535_smb can't enable device\n");
142		goto exit;
143	}
144
145	/* Determine the address of the SMBus area */
146	pci_read_config_word(dev, SMBBA, &ali1535_offset);
147	dev_dbg(&dev->dev, "ALI1535_smb is at offset 0x%04x\n", ali1535_offset);
148	ali1535_offset &= (0xffff & ~(ALI1535_SMB_IOSIZE - 1));
149	if (ali1535_offset == 0) {
150		dev_warn(&dev->dev,
151			"ALI1535_smb region uninitialized - upgrade BIOS?\n");
152		retval = -ENODEV;
153		goto exit;
154	}
155
156	if (pci_resource_flags(dev, 0) & IORESOURCE_IO)
157		ali1535_smba = pci_resource_start(dev, 0) + ali1535_offset;
158	else
159		ali1535_smba = ali1535_offset;
160
161	retval = acpi_check_region(ali1535_smba, ALI1535_SMB_IOSIZE,
162				   ali1535_driver.name);
163	if (retval)
164		goto exit;
165
166	if (!request_region(ali1535_smba, ALI1535_SMB_IOSIZE,
167			    ali1535_driver.name)) {
168		dev_err(&dev->dev, "ALI1535_smb region 0x%lx already in use!\n",
169			ali1535_smba);
170		retval = -EBUSY;
171		goto exit;
172	}
173
174	/* check if whole device is enabled */
175	pci_read_config_byte(dev, SMBCFG, &temp);
176	if ((temp & ALI1535_SMBIO_EN) == 0) {
177		dev_err(&dev->dev, "SMB device not enabled - upgrade BIOS?\n");
178		retval = -ENODEV;
179		goto exit_free;
180	}
181
182	/* Is SMB Host controller enabled? */
183	pci_read_config_byte(dev, SMBHSTCFG, &temp);
184	if ((temp & 1) == 0) {
185		dev_err(&dev->dev, "SMBus controller not enabled - upgrade BIOS?\n");
186		retval = -ENODEV;
187		goto exit_free;
188	}
189
190	/* set SMB clock to 74KHz as recommended in data sheet */
191	pci_write_config_byte(dev, SMBCLK, 0x20);
192
193	/*
194	  The interrupt routing for SMB is set up in register 0x77 in the
195	  1533 ISA Bridge device, NOT in the 7101 device.
196	  Don't bother with finding the 1533 device and reading the register.
197	if ((....... & 0x0F) == 1)
198		dev_dbg(&dev->dev, "ALI1535 using Interrupt 9 for SMBus.\n");
199	*/
200	pci_read_config_byte(dev, SMBREV, &temp);
201	dev_dbg(&dev->dev, "SMBREV = 0x%X\n", temp);
202	dev_dbg(&dev->dev, "ALI1535_smba = 0x%lx\n", ali1535_smba);
203
204	return 0;
205
206exit_free:
207	release_region(ali1535_smba, ALI1535_SMB_IOSIZE);
208exit:
209	return retval;
210}
211
212static int ali1535_transaction(struct i2c_adapter *adap)
213{
214	int temp;
215	int result = 0;
216	int timeout = 0;
217
218	dev_dbg(&adap->dev, "Transaction (pre): STS=%02x, TYP=%02x, "
219		"CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n",
220		inb_p(SMBHSTSTS), inb_p(SMBHSTTYP), inb_p(SMBHSTCMD),
221		inb_p(SMBHSTADD), inb_p(SMBHSTDAT0), inb_p(SMBHSTDAT1));
222
223	/* get status */
224	temp = inb_p(SMBHSTSTS);
225
226	/* Make sure the SMBus host is ready to start transmitting */
227	/* Check the busy bit first */
228	if (temp & ALI1535_STS_BUSY) {
229		/* If the host controller is still busy, it may have timed out
230		 * in the previous transaction, resulting in a "SMBus Timeout"
231		 * printk.  I've tried the following to reset a stuck busy bit.
232		 *   1. Reset the controller with an KILL command. (this
233		 *      doesn't seem to clear the controller if an external
234		 *      device is hung)
235		 *   2. Reset the controller and the other SMBus devices with a
236		 *      T_OUT command. (this clears the host busy bit if an
237		 *      external device is hung, but it comes back upon a new
238		 *      access to a device)
239		 *   3. Disable and reenable the controller in SMBHSTCFG. Worst
240		 *      case, nothing seems to work except power reset.
241		 */
242
243		/* Try resetting entire SMB bus, including other devices - This
244		 * may not work either - it clears the BUSY bit but then the
245		 * BUSY bit may come back on when you try and use the chip
246		 * again.  If that's the case you are stuck.
247		 */
248		dev_info(&adap->dev,
249			"Resetting entire SMB Bus to clear busy condition (%02x)\n",
250			temp);
251		outb_p(ALI1535_T_OUT, SMBHSTTYP);
252		temp = inb_p(SMBHSTSTS);
253	}
254
255	/* now check the error bits and the busy bit */
256	if (temp & (ALI1535_STS_ERR | ALI1535_STS_BUSY)) {
257		/* do a clear-on-write */
258		outb_p(0xFF, SMBHSTSTS);
259		temp = inb_p(SMBHSTSTS);
260		if (temp & (ALI1535_STS_ERR | ALI1535_STS_BUSY)) {
261			/* This is probably going to be correctable only by a
262			 * power reset as one of the bits now appears to be
263			 * stuck */
264			/* This may be a bus or device with electrical problems. */
265			dev_err(&adap->dev,
266				"SMBus reset failed! (0x%02x) - controller or "
267				"device on bus is probably hung\n", temp);
268			return -EBUSY;
269		}
270	} else {
271		/* check and clear done bit */
272		if (temp & ALI1535_STS_DONE)
273			outb_p(temp, SMBHSTSTS);
274	}
275
276	/* start the transaction by writing anything to the start register */
277	outb_p(0xFF, SMBHSTPORT);
278
279	/* We will always wait for a fraction of a second! */
280	timeout = 0;
281	do {
282		usleep_range(1000, 2000);
283		temp = inb_p(SMBHSTSTS);
284	} while (((temp & ALI1535_STS_BUSY) && !(temp & ALI1535_STS_IDLE))
285		 && (timeout++ < MAX_TIMEOUT));
286
287	/* If the SMBus is still busy, we give up */
288	if (timeout > MAX_TIMEOUT)
289		result = -ETIMEDOUT;
 
 
290
291	if (temp & ALI1535_STS_FAIL) {
292		result = -EIO;
293		dev_dbg(&adap->dev, "Error: Failed bus transaction\n");
294	}
295
296	/* Unfortunately the ALI SMB controller maps "no response" and "bus
297	 * collision" into a single bit. No response is the usual case so don't
298	 * do a printk.  This means that bus collisions go unreported.
299	 */
300	if (temp & ALI1535_STS_BUSERR) {
301		result = -ENXIO;
302		dev_dbg(&adap->dev,
303			"Error: no response or bus collision ADD=%02x\n",
304			inb_p(SMBHSTADD));
305	}
306
307	/* haven't ever seen this */
308	if (temp & ALI1535_STS_DEV) {
309		result = -EIO;
310		dev_err(&adap->dev, "Error: device error\n");
311	}
312
313	/* check to see if the "command complete" indication is set */
314	if (!(temp & ALI1535_STS_DONE))
315		result = -ETIMEDOUT;
 
 
316
317	dev_dbg(&adap->dev, "Transaction (post): STS=%02x, TYP=%02x, "
318		"CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n",
319		inb_p(SMBHSTSTS), inb_p(SMBHSTTYP), inb_p(SMBHSTCMD),
320		inb_p(SMBHSTADD), inb_p(SMBHSTDAT0), inb_p(SMBHSTDAT1));
321
322	/* take consequent actions for error conditions */
323	if (!(temp & ALI1535_STS_DONE)) {
324		/* issue "kill" to reset host controller */
325		outb_p(ALI1535_KILL, SMBHSTTYP);
326		outb_p(0xFF, SMBHSTSTS);
327	} else if (temp & ALI1535_STS_ERR) {
328		/* issue "timeout" to reset all devices on bus */
329		outb_p(ALI1535_T_OUT, SMBHSTTYP);
330		outb_p(0xFF, SMBHSTSTS);
331	}
332
333	return result;
334}
335
336/* Return negative errno on error. */
337static s32 ali1535_access(struct i2c_adapter *adap, u16 addr,
338			  unsigned short flags, char read_write, u8 command,
339			  int size, union i2c_smbus_data *data)
340{
341	int i, len;
342	int temp;
343	int timeout;
344	s32 result = 0;
345
346	/* make sure SMBus is idle */
347	temp = inb_p(SMBHSTSTS);
348	for (timeout = 0;
349	     (timeout < MAX_TIMEOUT) && !(temp & ALI1535_STS_IDLE);
350	     timeout++) {
351		usleep_range(1000, 2000);
352		temp = inb_p(SMBHSTSTS);
353	}
354	if (timeout >= MAX_TIMEOUT)
355		dev_warn(&adap->dev, "Idle wait Timeout! STS=0x%02x\n", temp);
356
357	/* clear status register (clear-on-write) */
358	outb_p(0xFF, SMBHSTSTS);
359
360	switch (size) {
361	case I2C_SMBUS_QUICK:
362		outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
363		       SMBHSTADD);
364		size = ALI1535_QUICK;
365		outb_p(size, SMBHSTTYP);	/* output command */
366		break;
367	case I2C_SMBUS_BYTE:
368		outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
369		       SMBHSTADD);
370		size = ALI1535_BYTE;
371		outb_p(size, SMBHSTTYP);	/* output command */
372		if (read_write == I2C_SMBUS_WRITE)
373			outb_p(command, SMBHSTCMD);
374		break;
375	case I2C_SMBUS_BYTE_DATA:
376		outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
377		       SMBHSTADD);
378		size = ALI1535_BYTE_DATA;
379		outb_p(size, SMBHSTTYP);	/* output command */
380		outb_p(command, SMBHSTCMD);
381		if (read_write == I2C_SMBUS_WRITE)
382			outb_p(data->byte, SMBHSTDAT0);
383		break;
384	case I2C_SMBUS_WORD_DATA:
385		outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
386		       SMBHSTADD);
387		size = ALI1535_WORD_DATA;
388		outb_p(size, SMBHSTTYP);	/* output command */
389		outb_p(command, SMBHSTCMD);
390		if (read_write == I2C_SMBUS_WRITE) {
391			outb_p(data->word & 0xff, SMBHSTDAT0);
392			outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1);
393		}
394		break;
395	case I2C_SMBUS_BLOCK_DATA:
396		outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
397		       SMBHSTADD);
398		size = ALI1535_BLOCK_DATA;
399		outb_p(size, SMBHSTTYP);	/* output command */
400		outb_p(command, SMBHSTCMD);
401		if (read_write == I2C_SMBUS_WRITE) {
402			len = data->block[0];
403			if (len < 0) {
404				len = 0;
405				data->block[0] = len;
406			}
407			if (len > 32) {
408				len = 32;
409				data->block[0] = len;
410			}
411			outb_p(len, SMBHSTDAT0);
412			/* Reset SMBBLKDAT */
413			outb_p(inb_p(SMBHSTTYP) | ALI1535_BLOCK_CLR, SMBHSTTYP);
414			for (i = 1; i <= len; i++)
415				outb_p(data->block[i], SMBBLKDAT);
416		}
417		break;
418	default:
419		dev_warn(&adap->dev, "Unsupported transaction %d\n", size);
420		result = -EOPNOTSUPP;
421		goto EXIT;
422	}
423
424	result = ali1535_transaction(adap);
425	if (result)
426		goto EXIT;
427
428	if ((read_write == I2C_SMBUS_WRITE) || (size == ALI1535_QUICK)) {
429		result = 0;
430		goto EXIT;
431	}
432
433	switch (size) {
434	case ALI1535_BYTE:	/* Result put in SMBHSTDAT0 */
435		data->byte = inb_p(SMBHSTDAT0);
436		break;
437	case ALI1535_BYTE_DATA:
438		data->byte = inb_p(SMBHSTDAT0);
439		break;
440	case ALI1535_WORD_DATA:
441		data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8);
442		break;
443	case ALI1535_BLOCK_DATA:
444		len = inb_p(SMBHSTDAT0);
445		if (len > 32)
446			len = 32;
447		data->block[0] = len;
448		/* Reset SMBBLKDAT */
449		outb_p(inb_p(SMBHSTTYP) | ALI1535_BLOCK_CLR, SMBHSTTYP);
450		for (i = 1; i <= data->block[0]; i++) {
451			data->block[i] = inb_p(SMBBLKDAT);
452			dev_dbg(&adap->dev, "Blk: len=%d, i=%d, data=%02x\n",
453				len, i, data->block[i]);
454		}
455		break;
456	}
457EXIT:
458	return result;
459}
460
461
462static u32 ali1535_func(struct i2c_adapter *adapter)
463{
464	return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
465	    I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
466	    I2C_FUNC_SMBUS_BLOCK_DATA;
467}
468
469static const struct i2c_algorithm smbus_algorithm = {
470	.smbus_xfer	= ali1535_access,
471	.functionality	= ali1535_func,
472};
473
474static struct i2c_adapter ali1535_adapter = {
475	.owner		= THIS_MODULE,
476	.class          = I2C_CLASS_HWMON,
477	.algo		= &smbus_algorithm,
478};
479
480static const struct pci_device_id ali1535_ids[] = {
481	{ PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M7101) },
482	{ }
483};
 
484MODULE_DEVICE_TABLE(pci, ali1535_ids);
485
486static int ali1535_probe(struct pci_dev *dev, const struct pci_device_id *id)
487{
488	if (ali1535_setup(dev)) {
489		dev_warn(&dev->dev,
490			"ALI1535 not detected, module not inserted.\n");
491		return -ENODEV;
492	}
493
494	/* set up the sysfs linkage to our parent device */
495	ali1535_adapter.dev.parent = &dev->dev;
496
497	snprintf(ali1535_adapter.name, sizeof(ali1535_adapter.name),
498		"SMBus ALI1535 adapter at %04x", ali1535_offset);
499	return i2c_add_adapter(&ali1535_adapter);
500}
501
502static void ali1535_remove(struct pci_dev *dev)
503{
504	i2c_del_adapter(&ali1535_adapter);
505	release_region(ali1535_smba, ALI1535_SMB_IOSIZE);
506
507	/*
508	 * do not call pci_disable_device(dev) since it can cause hard hangs on
509	 * some systems during power-off
510	 */
511}
512
513static struct pci_driver ali1535_driver = {
514	.name		= "ali1535_smbus",
515	.id_table	= ali1535_ids,
516	.probe		= ali1535_probe,
517	.remove		= ali1535_remove,
518};
519
520module_pci_driver(ali1535_driver);
521
522MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>");
523MODULE_AUTHOR("Philip Edelbrock <phil@netroedge.com>");
524MODULE_AUTHOR("Mark D. Studebaker <mdsxyz123@yahoo.com>");
525MODULE_AUTHOR("Dan Eaton <dan.eaton@rocketlogix.com>");
526MODULE_DESCRIPTION("ALI1535 SMBus driver");
527MODULE_LICENSE("GPL");
v3.15
 
  1/*
  2 * Copyright (c) 2000  Frodo Looijaard <frodol@dds.nl>,
  3 *                      Philip Edelbrock <phil@netroedge.com>,
  4 *                      Mark D. Studebaker <mdsxyz123@yahoo.com>,
  5 *                      Dan Eaton <dan.eaton@rocketlogix.com> and
  6 *                      Stephen Rousset <stephen.rousset@rocketlogix.com>
  7 *
  8 *  This program is free software; you can redistribute it and/or modify
  9 *  it under the terms of the GNU General Public License as published by
 10 *  the Free Software Foundation; either version 2 of the License, or
 11 *  (at your option) any later version.
 12 *
 13 *  This program is distributed in the hope that it will be useful,
 14 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 15 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16 *  GNU General Public License for more details.
 17 *
 18 *  You should have received a copy of the GNU General Public License
 19 *  along with this program; if not, write to the Free Software
 20 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 21*/
 22
 23/*
 24    This is the driver for the SMB Host controller on
 25    Acer Labs Inc. (ALI) M1535 South Bridge.
 26
 27    The M1535 is a South bridge for portable systems.
 28    It is very similar to the M15x3 South bridges also produced
 29    by Acer Labs Inc.  Some of the registers within the part
 30    have moved and some have been redefined slightly. Additionally,
 31    the sequencing of the SMBus transactions has been modified
 32    to be more consistent with the sequencing recommended by
 33    the manufacturer and observed through testing.  These
 34    changes are reflected in this driver and can be identified
 35    by comparing this driver to the i2c-ali15x3 driver.
 36    For an overview of these chips see http://www.acerlabs.com
 37
 38    The SMB controller is part of the 7101 device, which is an
 39    ACPI-compliant Power Management Unit (PMU).
 40
 41    The whole 7101 device has to be enabled for the SMB to work.
 42    You can't just enable the SMB alone.
 43    The SMB and the ACPI have separate I/O spaces.
 44    We make sure that the SMB is enabled. We leave the ACPI alone.
 45
 46    This driver controls the SMB Host only.
 47
 48    This driver does not use interrupts.
 49*/
 50
 51
 52/* Note: we assume there can only be one ALI1535, with one SMBus interface */
 53
 54#include <linux/module.h>
 55#include <linux/pci.h>
 56#include <linux/kernel.h>
 57#include <linux/stddef.h>
 58#include <linux/delay.h>
 59#include <linux/ioport.h>
 60#include <linux/i2c.h>
 61#include <linux/acpi.h>
 62#include <linux/io.h>
 63
 64
 65/* ALI1535 SMBus address offsets */
 66#define SMBHSTSTS	(0 + ali1535_smba)
 67#define SMBHSTTYP	(1 + ali1535_smba)
 68#define SMBHSTPORT	(2 + ali1535_smba)
 69#define SMBHSTCMD	(7 + ali1535_smba)
 70#define SMBHSTADD	(3 + ali1535_smba)
 71#define SMBHSTDAT0	(4 + ali1535_smba)
 72#define SMBHSTDAT1	(5 + ali1535_smba)
 73#define SMBBLKDAT	(6 + ali1535_smba)
 74
 75/* PCI Address Constants */
 76#define SMBCOM		0x004
 77#define SMBREV		0x008
 78#define SMBCFG		0x0D1
 79#define SMBBA		0x0E2
 80#define SMBHSTCFG	0x0F0
 81#define SMBCLK		0x0F2
 82
 83/* Other settings */
 84#define MAX_TIMEOUT		500	/* times 1/100 sec */
 85#define ALI1535_SMB_IOSIZE	32
 86
 87#define ALI1535_SMB_DEFAULTBASE	0x8040
 88
 89/* ALI1535 address lock bits */
 90#define ALI1535_LOCK		0x06	/* dwe */
 91
 92/* ALI1535 command constants */
 93#define ALI1535_QUICK		0x00
 94#define ALI1535_BYTE		0x10
 95#define ALI1535_BYTE_DATA	0x20
 96#define ALI1535_WORD_DATA	0x30
 97#define ALI1535_BLOCK_DATA	0x40
 98#define ALI1535_I2C_READ	0x60
 99
100#define	ALI1535_DEV10B_EN	0x80	/* Enable 10-bit addressing in	*/
101					/*  I2C read			*/
102#define	ALI1535_T_OUT		0x08	/* Time-out Command (write)	*/
103#define	ALI1535_A_HIGH_BIT9	0x08	/* Bit 9 of 10-bit address in	*/
104					/* Alert-Response-Address	*/
105					/* (read)			*/
106#define	ALI1535_KILL		0x04	/* Kill Command (write)		*/
107#define	ALI1535_A_HIGH_BIT8	0x04	/* Bit 8 of 10-bit address in	*/
108					/*  Alert-Response-Address	*/
109					/*  (read)			*/
110
111#define	ALI1535_D_HI_MASK	0x03	/* Mask for isolating bits 9-8	*/
112					/*  of 10-bit address in I2C	*/
113					/*  Read Command		*/
114
115/* ALI1535 status register bits */
116#define ALI1535_STS_IDLE	0x04
117#define ALI1535_STS_BUSY	0x08	/* host busy */
118#define ALI1535_STS_DONE	0x10	/* transaction complete */
119#define ALI1535_STS_DEV		0x20	/* device error */
120#define ALI1535_STS_BUSERR	0x40	/* bus error    */
121#define ALI1535_STS_FAIL	0x80	/* failed bus transaction */
122#define ALI1535_STS_ERR		0xE0	/* all the bad error bits */
123
124#define ALI1535_BLOCK_CLR	0x04	/* reset block data index */
125
126/* ALI1535 device address register bits */
127#define	ALI1535_RD_ADDR		0x01	/* Read/Write Bit in Device	*/
128					/*  Address field		*/
129					/*  -> Write = 0		*/
130					/*  -> Read  = 1		*/
131#define	ALI1535_SMBIO_EN	0x04	/* SMB I/O Space enable		*/
132
133static struct pci_driver ali1535_driver;
134static unsigned long ali1535_smba;
135static unsigned short ali1535_offset;
136
137/* Detect whether a ALI1535 can be found, and initialize it, where necessary.
138   Note the differences between kernels with the old PCI BIOS interface and
139   newer kernels with the real PCI interface. In compat.h some things are
140   defined to make the transition easier. */
141static int ali1535_setup(struct pci_dev *dev)
142{
143	int retval;
144	unsigned char temp;
145
146	/* Check the following things:
147		- SMB I/O address is initialized
148		- Device is enabled
149		- We can use the addresses
150	*/
151
152	retval = pci_enable_device(dev);
153	if (retval) {
154		dev_err(&dev->dev, "ALI1535_smb can't enable device\n");
155		goto exit;
156	}
157
158	/* Determine the address of the SMBus area */
159	pci_read_config_word(dev, SMBBA, &ali1535_offset);
160	dev_dbg(&dev->dev, "ALI1535_smb is at offset 0x%04x\n", ali1535_offset);
161	ali1535_offset &= (0xffff & ~(ALI1535_SMB_IOSIZE - 1));
162	if (ali1535_offset == 0) {
163		dev_warn(&dev->dev,
164			"ALI1535_smb region uninitialized - upgrade BIOS?\n");
165		retval = -ENODEV;
166		goto exit;
167	}
168
169	if (pci_resource_flags(dev, 0) & IORESOURCE_IO)
170		ali1535_smba = pci_resource_start(dev, 0) + ali1535_offset;
171	else
172		ali1535_smba = ali1535_offset;
173
174	retval = acpi_check_region(ali1535_smba, ALI1535_SMB_IOSIZE,
175				   ali1535_driver.name);
176	if (retval)
177		goto exit;
178
179	if (!request_region(ali1535_smba, ALI1535_SMB_IOSIZE,
180			    ali1535_driver.name)) {
181		dev_err(&dev->dev, "ALI1535_smb region 0x%lx already in use!\n",
182			ali1535_smba);
183		retval = -EBUSY;
184		goto exit;
185	}
186
187	/* check if whole device is enabled */
188	pci_read_config_byte(dev, SMBCFG, &temp);
189	if ((temp & ALI1535_SMBIO_EN) == 0) {
190		dev_err(&dev->dev, "SMB device not enabled - upgrade BIOS?\n");
191		retval = -ENODEV;
192		goto exit_free;
193	}
194
195	/* Is SMB Host controller enabled? */
196	pci_read_config_byte(dev, SMBHSTCFG, &temp);
197	if ((temp & 1) == 0) {
198		dev_err(&dev->dev, "SMBus controller not enabled - upgrade BIOS?\n");
199		retval = -ENODEV;
200		goto exit_free;
201	}
202
203	/* set SMB clock to 74KHz as recommended in data sheet */
204	pci_write_config_byte(dev, SMBCLK, 0x20);
205
206	/*
207	  The interrupt routing for SMB is set up in register 0x77 in the
208	  1533 ISA Bridge device, NOT in the 7101 device.
209	  Don't bother with finding the 1533 device and reading the register.
210	if ((....... & 0x0F) == 1)
211		dev_dbg(&dev->dev, "ALI1535 using Interrupt 9 for SMBus.\n");
212	*/
213	pci_read_config_byte(dev, SMBREV, &temp);
214	dev_dbg(&dev->dev, "SMBREV = 0x%X\n", temp);
215	dev_dbg(&dev->dev, "ALI1535_smba = 0x%lx\n", ali1535_smba);
216
217	return 0;
218
219exit_free:
220	release_region(ali1535_smba, ALI1535_SMB_IOSIZE);
221exit:
222	return retval;
223}
224
225static int ali1535_transaction(struct i2c_adapter *adap)
226{
227	int temp;
228	int result = 0;
229	int timeout = 0;
230
231	dev_dbg(&adap->dev, "Transaction (pre): STS=%02x, TYP=%02x, "
232		"CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n",
233		inb_p(SMBHSTSTS), inb_p(SMBHSTTYP), inb_p(SMBHSTCMD),
234		inb_p(SMBHSTADD), inb_p(SMBHSTDAT0), inb_p(SMBHSTDAT1));
235
236	/* get status */
237	temp = inb_p(SMBHSTSTS);
238
239	/* Make sure the SMBus host is ready to start transmitting */
240	/* Check the busy bit first */
241	if (temp & ALI1535_STS_BUSY) {
242		/* If the host controller is still busy, it may have timed out
243		 * in the previous transaction, resulting in a "SMBus Timeout"
244		 * printk.  I've tried the following to reset a stuck busy bit.
245		 *   1. Reset the controller with an KILL command. (this
246		 *      doesn't seem to clear the controller if an external
247		 *      device is hung)
248		 *   2. Reset the controller and the other SMBus devices with a
249		 *      T_OUT command. (this clears the host busy bit if an
250		 *      external device is hung, but it comes back upon a new
251		 *      access to a device)
252		 *   3. Disable and reenable the controller in SMBHSTCFG. Worst
253		 *      case, nothing seems to work except power reset.
254		 */
255
256		/* Try resetting entire SMB bus, including other devices - This
257		 * may not work either - it clears the BUSY bit but then the
258		 * BUSY bit may come back on when you try and use the chip
259		 * again.  If that's the case you are stuck.
260		 */
261		dev_info(&adap->dev,
262			"Resetting entire SMB Bus to clear busy condition (%02x)\n",
263			temp);
264		outb_p(ALI1535_T_OUT, SMBHSTTYP);
265		temp = inb_p(SMBHSTSTS);
266	}
267
268	/* now check the error bits and the busy bit */
269	if (temp & (ALI1535_STS_ERR | ALI1535_STS_BUSY)) {
270		/* do a clear-on-write */
271		outb_p(0xFF, SMBHSTSTS);
272		temp = inb_p(SMBHSTSTS);
273		if (temp & (ALI1535_STS_ERR | ALI1535_STS_BUSY)) {
274			/* This is probably going to be correctable only by a
275			 * power reset as one of the bits now appears to be
276			 * stuck */
277			/* This may be a bus or device with electrical problems. */
278			dev_err(&adap->dev,
279				"SMBus reset failed! (0x%02x) - controller or "
280				"device on bus is probably hung\n", temp);
281			return -EBUSY;
282		}
283	} else {
284		/* check and clear done bit */
285		if (temp & ALI1535_STS_DONE)
286			outb_p(temp, SMBHSTSTS);
287	}
288
289	/* start the transaction by writing anything to the start register */
290	outb_p(0xFF, SMBHSTPORT);
291
292	/* We will always wait for a fraction of a second! */
293	timeout = 0;
294	do {
295		usleep_range(1000, 2000);
296		temp = inb_p(SMBHSTSTS);
297	} while (((temp & ALI1535_STS_BUSY) && !(temp & ALI1535_STS_IDLE))
298		 && (timeout++ < MAX_TIMEOUT));
299
300	/* If the SMBus is still busy, we give up */
301	if (timeout > MAX_TIMEOUT) {
302		result = -ETIMEDOUT;
303		dev_err(&adap->dev, "SMBus Timeout!\n");
304	}
305
306	if (temp & ALI1535_STS_FAIL) {
307		result = -EIO;
308		dev_dbg(&adap->dev, "Error: Failed bus transaction\n");
309	}
310
311	/* Unfortunately the ALI SMB controller maps "no response" and "bus
312	 * collision" into a single bit. No response is the usual case so don't
313	 * do a printk.  This means that bus collisions go unreported.
314	 */
315	if (temp & ALI1535_STS_BUSERR) {
316		result = -ENXIO;
317		dev_dbg(&adap->dev,
318			"Error: no response or bus collision ADD=%02x\n",
319			inb_p(SMBHSTADD));
320	}
321
322	/* haven't ever seen this */
323	if (temp & ALI1535_STS_DEV) {
324		result = -EIO;
325		dev_err(&adap->dev, "Error: device error\n");
326	}
327
328	/* check to see if the "command complete" indication is set */
329	if (!(temp & ALI1535_STS_DONE)) {
330		result = -ETIMEDOUT;
331		dev_err(&adap->dev, "Error: command never completed\n");
332	}
333
334	dev_dbg(&adap->dev, "Transaction (post): STS=%02x, TYP=%02x, "
335		"CMD=%02x, ADD=%02x, DAT0=%02x, DAT1=%02x\n",
336		inb_p(SMBHSTSTS), inb_p(SMBHSTTYP), inb_p(SMBHSTCMD),
337		inb_p(SMBHSTADD), inb_p(SMBHSTDAT0), inb_p(SMBHSTDAT1));
338
339	/* take consequent actions for error conditions */
340	if (!(temp & ALI1535_STS_DONE)) {
341		/* issue "kill" to reset host controller */
342		outb_p(ALI1535_KILL, SMBHSTTYP);
343		outb_p(0xFF, SMBHSTSTS);
344	} else if (temp & ALI1535_STS_ERR) {
345		/* issue "timeout" to reset all devices on bus */
346		outb_p(ALI1535_T_OUT, SMBHSTTYP);
347		outb_p(0xFF, SMBHSTSTS);
348	}
349
350	return result;
351}
352
353/* Return negative errno on error. */
354static s32 ali1535_access(struct i2c_adapter *adap, u16 addr,
355			  unsigned short flags, char read_write, u8 command,
356			  int size, union i2c_smbus_data *data)
357{
358	int i, len;
359	int temp;
360	int timeout;
361	s32 result = 0;
362
363	/* make sure SMBus is idle */
364	temp = inb_p(SMBHSTSTS);
365	for (timeout = 0;
366	     (timeout < MAX_TIMEOUT) && !(temp & ALI1535_STS_IDLE);
367	     timeout++) {
368		usleep_range(1000, 2000);
369		temp = inb_p(SMBHSTSTS);
370	}
371	if (timeout >= MAX_TIMEOUT)
372		dev_warn(&adap->dev, "Idle wait Timeout! STS=0x%02x\n", temp);
373
374	/* clear status register (clear-on-write) */
375	outb_p(0xFF, SMBHSTSTS);
376
377	switch (size) {
378	case I2C_SMBUS_QUICK:
379		outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
380		       SMBHSTADD);
381		size = ALI1535_QUICK;
382		outb_p(size, SMBHSTTYP);	/* output command */
383		break;
384	case I2C_SMBUS_BYTE:
385		outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
386		       SMBHSTADD);
387		size = ALI1535_BYTE;
388		outb_p(size, SMBHSTTYP);	/* output command */
389		if (read_write == I2C_SMBUS_WRITE)
390			outb_p(command, SMBHSTCMD);
391		break;
392	case I2C_SMBUS_BYTE_DATA:
393		outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
394		       SMBHSTADD);
395		size = ALI1535_BYTE_DATA;
396		outb_p(size, SMBHSTTYP);	/* output command */
397		outb_p(command, SMBHSTCMD);
398		if (read_write == I2C_SMBUS_WRITE)
399			outb_p(data->byte, SMBHSTDAT0);
400		break;
401	case I2C_SMBUS_WORD_DATA:
402		outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
403		       SMBHSTADD);
404		size = ALI1535_WORD_DATA;
405		outb_p(size, SMBHSTTYP);	/* output command */
406		outb_p(command, SMBHSTCMD);
407		if (read_write == I2C_SMBUS_WRITE) {
408			outb_p(data->word & 0xff, SMBHSTDAT0);
409			outb_p((data->word & 0xff00) >> 8, SMBHSTDAT1);
410		}
411		break;
412	case I2C_SMBUS_BLOCK_DATA:
413		outb_p(((addr & 0x7f) << 1) | (read_write & 0x01),
414		       SMBHSTADD);
415		size = ALI1535_BLOCK_DATA;
416		outb_p(size, SMBHSTTYP);	/* output command */
417		outb_p(command, SMBHSTCMD);
418		if (read_write == I2C_SMBUS_WRITE) {
419			len = data->block[0];
420			if (len < 0) {
421				len = 0;
422				data->block[0] = len;
423			}
424			if (len > 32) {
425				len = 32;
426				data->block[0] = len;
427			}
428			outb_p(len, SMBHSTDAT0);
429			/* Reset SMBBLKDAT */
430			outb_p(inb_p(SMBHSTTYP) | ALI1535_BLOCK_CLR, SMBHSTTYP);
431			for (i = 1; i <= len; i++)
432				outb_p(data->block[i], SMBBLKDAT);
433		}
434		break;
435	default:
436		dev_warn(&adap->dev, "Unsupported transaction %d\n", size);
437		result = -EOPNOTSUPP;
438		goto EXIT;
439	}
440
441	result = ali1535_transaction(adap);
442	if (result)
443		goto EXIT;
444
445	if ((read_write == I2C_SMBUS_WRITE) || (size == ALI1535_QUICK)) {
446		result = 0;
447		goto EXIT;
448	}
449
450	switch (size) {
451	case ALI1535_BYTE:	/* Result put in SMBHSTDAT0 */
452		data->byte = inb_p(SMBHSTDAT0);
453		break;
454	case ALI1535_BYTE_DATA:
455		data->byte = inb_p(SMBHSTDAT0);
456		break;
457	case ALI1535_WORD_DATA:
458		data->word = inb_p(SMBHSTDAT0) + (inb_p(SMBHSTDAT1) << 8);
459		break;
460	case ALI1535_BLOCK_DATA:
461		len = inb_p(SMBHSTDAT0);
462		if (len > 32)
463			len = 32;
464		data->block[0] = len;
465		/* Reset SMBBLKDAT */
466		outb_p(inb_p(SMBHSTTYP) | ALI1535_BLOCK_CLR, SMBHSTTYP);
467		for (i = 1; i <= data->block[0]; i++) {
468			data->block[i] = inb_p(SMBBLKDAT);
469			dev_dbg(&adap->dev, "Blk: len=%d, i=%d, data=%02x\n",
470				len, i, data->block[i]);
471		}
472		break;
473	}
474EXIT:
475	return result;
476}
477
478
479static u32 ali1535_func(struct i2c_adapter *adapter)
480{
481	return I2C_FUNC_SMBUS_QUICK | I2C_FUNC_SMBUS_BYTE |
482	    I2C_FUNC_SMBUS_BYTE_DATA | I2C_FUNC_SMBUS_WORD_DATA |
483	    I2C_FUNC_SMBUS_BLOCK_DATA;
484}
485
486static const struct i2c_algorithm smbus_algorithm = {
487	.smbus_xfer	= ali1535_access,
488	.functionality	= ali1535_func,
489};
490
491static struct i2c_adapter ali1535_adapter = {
492	.owner		= THIS_MODULE,
493	.class          = I2C_CLASS_HWMON | I2C_CLASS_SPD,
494	.algo		= &smbus_algorithm,
495};
496
497static const struct pci_device_id ali1535_ids[] = {
498	{ PCI_DEVICE(PCI_VENDOR_ID_AL, PCI_DEVICE_ID_AL_M7101) },
499	{ },
500};
501
502MODULE_DEVICE_TABLE(pci, ali1535_ids);
503
504static int ali1535_probe(struct pci_dev *dev, const struct pci_device_id *id)
505{
506	if (ali1535_setup(dev)) {
507		dev_warn(&dev->dev,
508			"ALI1535 not detected, module not inserted.\n");
509		return -ENODEV;
510	}
511
512	/* set up the sysfs linkage to our parent device */
513	ali1535_adapter.dev.parent = &dev->dev;
514
515	snprintf(ali1535_adapter.name, sizeof(ali1535_adapter.name),
516		"SMBus ALI1535 adapter at %04x", ali1535_offset);
517	return i2c_add_adapter(&ali1535_adapter);
518}
519
520static void ali1535_remove(struct pci_dev *dev)
521{
522	i2c_del_adapter(&ali1535_adapter);
523	release_region(ali1535_smba, ALI1535_SMB_IOSIZE);
 
 
 
 
 
524}
525
526static struct pci_driver ali1535_driver = {
527	.name		= "ali1535_smbus",
528	.id_table	= ali1535_ids,
529	.probe		= ali1535_probe,
530	.remove		= ali1535_remove,
531};
532
533module_pci_driver(ali1535_driver);
534
535MODULE_AUTHOR("Frodo Looijaard <frodol@dds.nl>, "
536	      "Philip Edelbrock <phil@netroedge.com>, "
537	      "Mark D. Studebaker <mdsxyz123@yahoo.com> "
538	      "and Dan Eaton <dan.eaton@rocketlogix.com>");
539MODULE_DESCRIPTION("ALI1535 SMBus driver");
540MODULE_LICENSE("GPL");