Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | Copyright (c) 2002,2003 Alexander Malysh <amalysh@web.de> |
| 4 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | Status: beta |
| 9 | |
| 10 | Supports: |
| 11 | SIS 630 |
| 12 | SIS 730 |
Amaury Decrême | 974d6a3 | 2013-01-28 22:21:05 +0100 | [diff] [blame] | 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) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
| 28 | Note: we assume there can only be one device, with one SMBus interface. |
| 29 | */ |
| 30 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <linux/i2c.h> |
Jean Delvare | 54fb4a05 | 2008-07-14 22:38:33 +0200 | [diff] [blame] | 37 | #include <linux/acpi.h> |
H Hartley Sweeten | 2178218 | 2010-05-21 18:41:01 +0200 | [diff] [blame] | 38 | #include <linux/io.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Amaury Decrême | 974d6a3 | 2013-01-28 22:21:05 +0100 | [diff] [blame] | 40 | /* SIS964 id is defined here as we are the only file using it */ |
| 41 | #define PCI_DEVICE_ID_SI_964 0x0964 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
Amaury Decrême | 974d6a3 | 2013-01-28 22:21:05 +0100 | [diff] [blame] | 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 | |
Amaury Decrême | a6e7d0ef | 2013-01-28 22:21:08 +0100 | [diff] [blame] | 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 | |
Amaury Decrême | 974d6a3 | 2013-01-28 22:21:05 +0100 | [diff] [blame] | 67 | /* register count for request_region |
| 68 | * As we don't use SMB_PCOUNT, 20 is ok for SiS630 and SiS964 |
| 69 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Jean Delvare | d6072f8 | 2005-09-25 16:37:04 +0200 | [diff] [blame] | 89 | static struct pci_driver sis630_driver; |
| 90 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | /* insmod parameters */ |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 92 | static bool high_clock; |
| 93 | static bool force; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | module_param(high_clock, bool, 0); |
Amaury Decrême | 974d6a3 | 2013-01-28 22:21:05 +0100 | [diff] [blame] | 95 | MODULE_PARM_DESC(high_clock, |
| 96 | "Set Host Master Clock to 56KHz (default 14KHz) (SIS630/730 only)."); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | module_param(force, bool, 0); |
| 98 | MODULE_PARM_DESC(force, "Forcibly enable the SIS630. DANGEROUS!"); |
| 99 | |
Amaury Decrême | 974d6a3 | 2013-01-28 22:21:05 +0100 | [diff] [blame] | 100 | /* SMBus base adress */ |
| 101 | static unsigned short smbus_base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
| 103 | /* supported chips */ |
| 104 | static int supported[] = { |
| 105 | PCI_DEVICE_ID_SI_630, |
| 106 | PCI_DEVICE_ID_SI_730, |
Amaury Decrême | 974d6a3 | 2013-01-28 22:21:05 +0100 | [diff] [blame] | 107 | PCI_DEVICE_ID_SI_760, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | 0 /* terminates the list */ |
| 109 | }; |
| 110 | |
| 111 | static inline u8 sis630_read(u8 reg) |
| 112 | { |
Amaury Decrême | 974d6a3 | 2013-01-28 22:21:05 +0100 | [diff] [blame] | 113 | return inb(smbus_base + reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | static inline void sis630_write(u8 reg, u8 data) |
| 117 | { |
Amaury Decrême | 974d6a3 | 2013-01-28 22:21:05 +0100 | [diff] [blame] | 118 | outb(data, smbus_base + reg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | } |
| 120 | |
Amaury Decrême | 91991f3 | 2013-01-29 21:22:26 +0100 | [diff] [blame] | 121 | static int sis630_transaction_start(struct i2c_adapter *adap, int size, |
| 122 | u8 *oldclock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | { |
Amaury Decrême | 91991f3 | 2013-01-29 21:22:26 +0100 | [diff] [blame] | 124 | int temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | |
| 126 | /* Make sure the SMBus host is ready to start transmitting. */ |
Amaury Decrême | a6e7d0ef | 2013-01-28 22:21:08 +0100 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | /* kill smbus transaction */ |
Amaury Decrême | a6e7d0ef | 2013-01-28 22:21:08 +0100 | [diff] [blame] | 131 | sis630_write(SMBHOST_CNT, SMB_KILL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
Amaury Decrême | a6e7d0ef | 2013-01-28 22:21:08 +0100 | [diff] [blame] | 133 | temp = sis630_read(SMB_CNT); |
| 134 | if (temp & (SMB_PROBE | SMB_HOSTBUSY)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | dev_dbg(&adap->dev, "Failed! (%02x)\n", temp); |
David Brownell | 9714034 | 2008-07-14 22:38:25 +0200 | [diff] [blame] | 136 | return -EBUSY; |
Amaury Decrême | 91991f3 | 2013-01-29 21:22:26 +0100 | [diff] [blame] | 137 | } else { |
Jean Delvare | c5d21b7 | 2008-04-29 23:11:37 +0200 | [diff] [blame] | 138 | dev_dbg(&adap->dev, "Successful!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | } |
Amaury Decrême | 91991f3 | 2013-01-29 21:22:26 +0100 | [diff] [blame] | 140 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Amaury Decrême | 91991f3 | 2013-01-29 21:22:26 +0100 | [diff] [blame] | 147 | /* disable timeout interrupt, |
| 148 | * set Host Master Clock to 56KHz if requested */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | if (high_clock) |
Amaury Decrême | a6e7d0ef | 2013-01-28 22:21:08 +0100 | [diff] [blame] | 150 | sis630_write(SMB_CNT, SMBCLK_SEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | else |
Amaury Decrême | a6e7d0ef | 2013-01-28 22:21:08 +0100 | [diff] [blame] | 152 | sis630_write(SMB_CNT, (*oldclock & ~MSTO_EN)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ |
Amaury Decrême | a6e7d0ef | 2013-01-28 22:21:08 +0100 | [diff] [blame] | 159 | sis630_write(SMBHOST_CNT, SMB_START | (size & 0x07)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | static 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 */ |
Amaury Decrême | a6e7d0ef | 2013-01-28 22:21:08 +0100 | [diff] [blame] | 173 | if (size == SIS630_BLOCK_DATA && (temp & BYTE_DONE_STS)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | break; |
| 175 | } while (!(temp & 0x0e) && (timeout++ < MAX_TIMEOUT)); |
| 176 | |
| 177 | /* If the SMBus is still busy, we give up */ |
Roel Kluin | 4ccc28f | 2009-05-05 08:39:24 +0200 | [diff] [blame] | 178 | if (timeout > MAX_TIMEOUT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | dev_dbg(&adap->dev, "SMBus Timeout!\n"); |
David Brownell | 9714034 | 2008-07-14 22:38:25 +0200 | [diff] [blame] | 180 | result = -ETIMEDOUT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | } |
| 182 | |
Amaury Decrême | a6e7d0ef | 2013-01-28 22:21:08 +0100 | [diff] [blame] | 183 | if (temp & SMBERR_STS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | dev_dbg(&adap->dev, "Error: Failed bus transaction\n"); |
David Brownell | 9714034 | 2008-07-14 22:38:25 +0200 | [diff] [blame] | 185 | result = -ENXIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Amaury Decrême | a6e7d0ef | 2013-01-28 22:21:08 +0100 | [diff] [blame] | 188 | if (temp & SMBCOL_STS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | dev_err(&adap->dev, "Bus collision!\n"); |
Amaury Decrême | 499b919 | 2013-01-28 22:21:07 +0100 | [diff] [blame] | 190 | result = -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | return result; |
| 194 | } |
| 195 | |
| 196 | static void sis630_transaction_end(struct i2c_adapter *adap, u8 oldclock) |
| 197 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | /* clear all status "sticky" bits */ |
Amaury Decrême | aa9e7a3 | 2013-01-28 22:21:06 +0100 | [diff] [blame] | 199 | sis630_write(SMB_STS, 0xFF); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
Amaury Decrême | 91991f3 | 2013-01-29 21:22:26 +0100 | [diff] [blame] | 201 | dev_dbg(&adap->dev, |
| 202 | "SMB_CNT before clock restore 0x%02x\n", sis630_read(SMB_CNT)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | |
| 204 | /* |
| 205 | * restore old Host Master Clock if high_clock is set |
| 206 | * and oldclock was not 56KHz |
| 207 | */ |
Amaury Decrême | a6e7d0ef | 2013-01-28 22:21:08 +0100 | [diff] [blame] | 208 | if (high_clock && !(oldclock & SMBCLK_SEL)) |
| 209 | sis630_write(SMB_CNT, sis630_read(SMB_CNT) & ~SMBCLK_SEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | |
Amaury Decrême | 91991f3 | 2013-01-29 21:22:26 +0100 | [diff] [blame] | 211 | dev_dbg(&adap->dev, |
| 212 | "SMB_CNT after clock restore 0x%02x\n", sis630_read(SMB_CNT)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | static 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 | |
Amaury Decrême | 91991f3 | 2013-01-29 21:22:26 +0100 | [diff] [blame] | 229 | static int sis630_block_data(struct i2c_adapter *adap, |
| 230 | union i2c_smbus_data *data, int read_write) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Amaury Decrême | 91991f3 | 2013-01-29 21:22:26 +0100 | [diff] [blame] | 242 | for (i = 1; i <= len; i++) { |
| 243 | dev_dbg(&adap->dev, |
| 244 | "set data 0x%02x\n", data->block[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | /* set data */ |
Amaury Decrême | 91991f3 | 2013-01-29 21:22:26 +0100 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | /* first transaction */ |
David Brownell | 9714034 | 2008-07-14 22:38:25 +0200 | [diff] [blame] | 251 | rc = sis630_transaction_start(adap, |
| 252 | SIS630_BLOCK_DATA, &oldclock); |
| 253 | if (rc) |
| 254 | return rc; |
Amaury Decrême | 91991f3 | 2013-01-29 21:22:26 +0100 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | /* |
| 263 | If this is not first transaction, |
| 264 | we must clear sticky bit. |
| 265 | clear SMBARY_STS |
| 266 | */ |
Amaury Decrême | a6e7d0ef | 2013-01-28 22:21:08 +0100 | [diff] [blame] | 267 | sis630_write(SMB_STS, BYTE_DONE_STS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | } |
David Brownell | 9714034 | 2008-07-14 22:38:25 +0200 | [diff] [blame] | 269 | rc = sis630_transaction_wait(adap, |
| 270 | SIS630_BLOCK_DATA); |
| 271 | if (rc) { |
Amaury Decrême | 91991f3 | 2013-01-29 21:22:26 +0100 | [diff] [blame] | 272 | dev_dbg(&adap->dev, |
| 273 | "trans_wait failed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | break; |
| 275 | } |
| 276 | } |
| 277 | } |
Amaury Decrême | 91991f3 | 2013-01-29 21:22:26 +0100 | [diff] [blame] | 278 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | /* read request */ |
| 280 | data->block[0] = len = 0; |
David Brownell | 9714034 | 2008-07-14 22:38:25 +0200 | [diff] [blame] | 281 | rc = sis630_transaction_start(adap, |
| 282 | SIS630_BLOCK_DATA, &oldclock); |
| 283 | if (rc) |
| 284 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | do { |
David Brownell | 9714034 | 2008-07-14 22:38:25 +0200 | [diff] [blame] | 286 | rc = sis630_transaction_wait(adap, SIS630_BLOCK_DATA); |
| 287 | if (rc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | dev_dbg(&adap->dev, "trans_wait failed\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Amaury Decrême | 91991f3 | 2013-01-29 21:22:26 +0100 | [diff] [blame] | 299 | dev_dbg(&adap->dev, |
| 300 | "block data read len=0x%x\n", data->block[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | |
Amaury Decrême | 91991f3 | 2013-01-29 21:22:26 +0100 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Amaury Decrême | 91991f3 | 2013-01-29 21:22:26 +0100 | [diff] [blame] | 309 | dev_dbg(&adap->dev, |
| 310 | "clear smbary_sts len=%d i=%d\n", len, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | |
| 312 | /* clear SMBARY_STS */ |
Amaury Decrême | a6e7d0ef | 2013-01-28 22:21:08 +0100 | [diff] [blame] | 313 | sis630_write(SMB_STS, BYTE_DONE_STS); |
Amaury Decrême | 91991f3 | 2013-01-29 21:22:26 +0100 | [diff] [blame] | 314 | } while (len < data->block[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | sis630_transaction_end(adap, oldclock); |
| 318 | |
| 319 | return rc; |
| 320 | } |
| 321 | |
David Brownell | 9714034 | 2008-07-14 22:38:25 +0200 | [diff] [blame] | 322 | /* Return negative errno on error. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | static 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 | { |
David Brownell | 9714034 | 2008-07-14 22:38:25 +0200 | [diff] [blame] | 327 | int status; |
| 328 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | switch (size) { |
Amaury Decrême | 91991f3 | 2013-01-29 21:22:26 +0100 | [diff] [blame] | 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) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | sis630_write(SMB_CMD, command); |
Amaury Decrême | 91991f3 | 2013-01-29 21:22:26 +0100 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | } |
| 372 | |
David Brownell | 9714034 | 2008-07-14 22:38:25 +0200 | [diff] [blame] | 373 | status = sis630_transaction(adap, size); |
| 374 | if (status) |
| 375 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | |
| 377 | if ((size != SIS630_PCALL) && |
| 378 | ((read_write == I2C_SMBUS_WRITE) || (size == SIS630_QUICK))) { |
| 379 | return 0; |
| 380 | } |
| 381 | |
Amaury Decrême | 91991f3 | 2013-01-29 21:22:26 +0100 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | static u32 sis630_func(struct i2c_adapter *adapter) |
| 398 | { |
Amaury Decrême | 91991f3 | 2013-01-29 21:22:26 +0100 | [diff] [blame] | 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; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | } |
| 403 | |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 404 | static int sis630_setup(struct pci_dev *sis630_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | { |
| 406 | unsigned char b; |
| 407 | struct pci_dev *dummy = NULL; |
Jean Delvare | 7c1f59c | 2012-01-12 20:32:03 +0100 | [diff] [blame] | 408 | int retval, i; |
Amaury Decrême | 974d6a3 | 2013-01-28 22:21:05 +0100 | [diff] [blame] | 409 | /* acpi base address */ |
| 410 | unsigned short acpi_base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | |
| 412 | /* check for supported SiS devices */ |
Amaury Decrême | 91991f3 | 2013-01-29 21:22:26 +0100 | [diff] [blame] | 413 | for (i = 0; supported[i] > 0; i++) { |
| 414 | dummy = pci_get_device(PCI_VENDOR_ID_SI, supported[i], dummy); |
| 415 | if (dummy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | break; /* found */ |
| 417 | } |
| 418 | |
| 419 | if (dummy) { |
| 420 | pci_dev_put(dummy); |
Amaury Decrême | 91991f3 | 2013-01-29 21:22:26 +0100 | [diff] [blame] | 421 | } else if (force) { |
| 422 | dev_err(&sis630_dev->dev, |
| 423 | "WARNING: Can't detect SIS630 compatible device, but " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | "loading because of force option enabled\n"); |
Amaury Decrême | 91991f3 | 2013-01-29 21:22:26 +0100 | [diff] [blame] | 425 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Amaury Decrême | 91991f3 | 2013-01-29 21:22:26 +0100 | [diff] [blame] | 433 | if (pci_read_config_byte(sis630_dev, SIS630_BIOS_CTL_REG, &b)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | dev_err(&sis630_dev->dev, "Error: Can't read bios ctl reg\n"); |
Jean Delvare | 7c1f59c | 2012-01-12 20:32:03 +0100 | [diff] [blame] | 435 | retval = -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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"); |
Jean Delvare | 7c1f59c | 2012-01-12 20:32:03 +0100 | [diff] [blame] | 442 | retval = -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | goto exit; |
| 444 | } |
| 445 | |
| 446 | /* Determine the ACPI base address */ |
Amaury Decrême | 91991f3 | 2013-01-29 21:22:26 +0100 | [diff] [blame] | 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"); |
Jean Delvare | 7c1f59c | 2012-01-12 20:32:03 +0100 | [diff] [blame] | 451 | retval = -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | goto exit; |
| 453 | } |
| 454 | |
Amaury Decrême | 97da42d | 2013-01-28 22:21:09 +0100 | [diff] [blame] | 455 | dev_dbg(&sis630_dev->dev, "ACPI base at 0x%04hx\n", acpi_base); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | |
Amaury Decrême | 974d6a3 | 2013-01-28 22:21:05 +0100 | [diff] [blame] | 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, |
Jean Delvare | 54fb4a05 | 2008-07-14 22:38:33 +0200 | [diff] [blame] | 465 | sis630_driver.name); |
| 466 | if (retval) |
| 467 | goto exit; |
| 468 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | /* Everything is happy, let's grab the memory and set things up. */ |
Amaury Decrême | 974d6a3 | 2013-01-28 22:21:05 +0100 | [diff] [blame] | 470 | if (!request_region(smbus_base + SMB_STS, SIS630_SMB_IOREGION, |
Jean Delvare | d6072f8 | 2005-09-25 16:37:04 +0200 | [diff] [blame] | 471 | sis630_driver.name)) { |
Amaury Decrême | 974d6a3 | 2013-01-28 22:21:05 +0100 | [diff] [blame] | 472 | dev_err(&sis630_dev->dev, |
Louis Taylor | 60f7691 | 2019-03-02 14:18:36 +0000 | [diff] [blame] | 473 | "I/O Region 0x%04x-0x%04x for SMBus already in use.\n", |
Amaury Decrême | 974d6a3 | 2013-01-28 22:21:05 +0100 | [diff] [blame] | 474 | smbus_base + SMB_STS, |
| 475 | smbus_base + SMB_STS + SIS630_SMB_IOREGION - 1); |
Jean Delvare | 7c1f59c | 2012-01-12 20:32:03 +0100 | [diff] [blame] | 476 | retval = -EBUSY; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | goto exit; |
| 478 | } |
| 479 | |
| 480 | retval = 0; |
| 481 | |
| 482 | exit: |
| 483 | if (retval) |
Amaury Decrême | 974d6a3 | 2013-01-28 22:21:05 +0100 | [diff] [blame] | 484 | smbus_base = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | return retval; |
| 486 | } |
| 487 | |
| 488 | |
Jean Delvare | 8f9082c | 2006-09-03 22:39:46 +0200 | [diff] [blame] | 489 | static const struct i2c_algorithm smbus_algorithm = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | .smbus_xfer = sis630_access, |
| 491 | .functionality = sis630_func, |
| 492 | }; |
| 493 | |
| 494 | static struct i2c_adapter sis630_adapter = { |
| 495 | .owner = THIS_MODULE, |
Jean Delvare | 3401b2f | 2008-07-14 22:38:29 +0200 | [diff] [blame] | 496 | .class = I2C_CLASS_HWMON | I2C_CLASS_SPD, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | .algo = &smbus_algorithm, |
Amaury Decrême | 974d6a3 | 2013-01-28 22:21:05 +0100 | [diff] [blame] | 498 | .retries = 3 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | }; |
| 500 | |
Jingoo Han | 392debf | 2013-12-03 08:11:20 +0900 | [diff] [blame] | 501 | static const struct pci_device_id sis630_ids[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | { PCI_DEVICE(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_503) }, |
| 503 | { PCI_DEVICE(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_LPC) }, |
Amaury Decrême | 974d6a3 | 2013-01-28 22:21:05 +0100 | [diff] [blame] | 504 | { PCI_DEVICE(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_964) }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | { 0, } |
| 506 | }; |
| 507 | |
Amaury Decrême | 91991f3 | 2013-01-29 21:22:26 +0100 | [diff] [blame] | 508 | MODULE_DEVICE_TABLE(pci, sis630_ids); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 510 | static int sis630_probe(struct pci_dev *dev, const struct pci_device_id *id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | { |
| 512 | if (sis630_setup(dev)) { |
Amaury Decrême | 91991f3 | 2013-01-29 21:22:26 +0100 | [diff] [blame] | 513 | dev_err(&dev->dev, |
| 514 | "SIS630 compatible bus not detected, " |
| 515 | "module not inserted.\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | return -ENODEV; |
| 517 | } |
| 518 | |
Robert P. J. Day | 405ae7d | 2007-02-17 19:13:42 +0100 | [diff] [blame] | 519 | /* set up the sysfs linkage to our parent device */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | sis630_adapter.dev.parent = &dev->dev; |
| 521 | |
Jean Delvare | 66c7acf | 2009-01-07 14:29:18 +0100 | [diff] [blame] | 522 | snprintf(sis630_adapter.name, sizeof(sis630_adapter.name), |
Louis Taylor | 60f7691 | 2019-03-02 14:18:36 +0000 | [diff] [blame] | 523 | "SMBus SIS630 adapter at %04x", smbus_base + SMB_STS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | |
| 525 | return i2c_add_adapter(&sis630_adapter); |
| 526 | } |
| 527 | |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 528 | static void sis630_remove(struct pci_dev *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | { |
Amaury Decrême | 974d6a3 | 2013-01-28 22:21:05 +0100 | [diff] [blame] | 530 | if (smbus_base) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | i2c_del_adapter(&sis630_adapter); |
Amaury Decrême | 974d6a3 | 2013-01-28 22:21:05 +0100 | [diff] [blame] | 532 | release_region(smbus_base + SMB_STS, SIS630_SMB_IOREGION); |
| 533 | smbus_base = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | } |
| 535 | } |
| 536 | |
| 537 | |
| 538 | static struct pci_driver sis630_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | .name = "sis630_smbus", |
| 540 | .id_table = sis630_ids, |
| 541 | .probe = sis630_probe, |
Bill Pemberton | 0b255e9 | 2012-11-27 15:59:38 -0500 | [diff] [blame] | 542 | .remove = sis630_remove, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | }; |
| 544 | |
Axel Lin | 56f2178 | 2012-07-24 14:13:56 +0200 | [diff] [blame] | 545 | module_pci_driver(sis630_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | |
| 547 | MODULE_LICENSE("GPL"); |
| 548 | MODULE_AUTHOR("Alexander Malysh <amalysh@web.de>"); |
| 549 | MODULE_DESCRIPTION("SIS630 SMBus driver"); |