1 // SPDX-License-Identifier: GPL-2.0-only
3 i2c-isch.c - Linux kernel driver for Intel SCH chipset SMBus
5 Copyright (c) 1998 - 2002 Frodo Looijaard <frodol@dds.nl> and
6 Philip Edelbrock <phil@netroedge.com>
8 Copyright (c) 2007 - 2008 Jacob Jun Pan <jacob.jun.pan@intel.com>
14 Intel SCH chipsets (AF82US15W, AF82US15L, AF82UL11L)
15 Note: we assume there can only be one device, with one SMBus interface.
18 #include <linux/module.h>
19 #include <linux/platform_device.h>
20 #include <linux/kernel.h>
21 #include <linux/delay.h>
22 #include <linux/stddef.h>
23 #include <linux/ioport.h>
24 #include <linux/i2c.h>
27 /* SCH SMBus address offsets */
28 #define SMBHSTCNT (0 + sch_smba)
29 #define SMBHSTSTS (1 + sch_smba)
30 #define SMBHSTCLK (2 + sch_smba)
31 #define SMBHSTADD (4 + sch_smba) /* TSA */
32 #define SMBHSTCMD (5 + sch_smba)
33 #define SMBHSTDAT0 (6 + sch_smba)
34 #define SMBHSTDAT1 (7 + sch_smba)
35 #define SMBBLKDAT (0x20 + sch_smba)
38 #define MAX_RETRIES 5000
41 #define SCH_QUICK 0x00
43 #define SCH_BYTE_DATA 0x02
44 #define SCH_WORD_DATA 0x03
45 #define SCH_BLOCK_DATA 0x05
47 static unsigned short sch_smba
;
48 static struct i2c_adapter sch_adapter
;
49 static int backbone_speed
= 33000; /* backbone speed in kHz */
50 module_param(backbone_speed
, int, S_IRUSR
| S_IWUSR
);
51 MODULE_PARM_DESC(backbone_speed
, "Backbone speed in kHz, (default = 33000)");
54 * Start the i2c transaction -- the i2c_access will prepare the transaction
55 * and this function will execute it.
56 * return 0 for success and others for failure.
58 static int sch_transaction(void)
64 dev_dbg(&sch_adapter
.dev
, "Transaction (pre): CNT=%02x, CMD=%02x, "
65 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb(SMBHSTCNT
),
66 inb(SMBHSTCMD
), inb(SMBHSTADD
), inb(SMBHSTDAT0
),
69 /* Make sure the SMBus host is ready to start transmitting */
70 temp
= inb(SMBHSTSTS
) & 0x0f;
72 /* Can not be busy since we checked it in sch_access */
74 dev_dbg(&sch_adapter
.dev
, "Completion (%02x). "
78 dev_dbg(&sch_adapter
.dev
, "SMBus error (%02x). "
79 "Resetting...\n", temp
);
81 outb(temp
, SMBHSTSTS
);
82 temp
= inb(SMBHSTSTS
) & 0x0f;
84 dev_err(&sch_adapter
.dev
,
85 "SMBus is not ready: (%02x)\n", temp
);
90 /* start the transaction by setting bit 4 */
91 outb(inb(SMBHSTCNT
) | 0x10, SMBHSTCNT
);
94 usleep_range(100, 200);
95 temp
= inb(SMBHSTSTS
) & 0x0f;
96 } while ((temp
& 0x08) && (retries
++ < MAX_RETRIES
));
98 /* If the SMBus is still busy, we give up */
99 if (retries
> MAX_RETRIES
) {
100 dev_err(&sch_adapter
.dev
, "SMBus Timeout!\n");
105 dev_dbg(&sch_adapter
.dev
, "Bus collision! SMBus may be "
106 "locked until next hard reset. (sorry!)\n");
107 /* Clock stops and slave is stuck in mid-transmission */
108 } else if (temp
& 0x02) {
110 dev_err(&sch_adapter
.dev
, "Error: no response!\n");
111 } else if (temp
& 0x01) {
112 dev_dbg(&sch_adapter
.dev
, "Post complete!\n");
113 outb(temp
, SMBHSTSTS
);
114 temp
= inb(SMBHSTSTS
) & 0x07;
116 /* Completion clear failed */
117 dev_dbg(&sch_adapter
.dev
, "Failed reset at end of "
118 "transaction (%02x), Bus error!\n", temp
);
122 dev_dbg(&sch_adapter
.dev
, "No such address.\n");
124 dev_dbg(&sch_adapter
.dev
, "Transaction (post): CNT=%02x, CMD=%02x, "
125 "ADD=%02x, DAT0=%02x, DAT1=%02x\n", inb(SMBHSTCNT
),
126 inb(SMBHSTCMD
), inb(SMBHSTADD
), inb(SMBHSTDAT0
),
132 * This is the main access entry for i2c-sch access
133 * adap is i2c_adapter pointer, addr is the i2c device bus address, read_write
134 * (0 for read and 1 for write), size is i2c transaction type and data is the
135 * union of transaction for data to be transferred or data read from bus.
136 * return 0 for success and others for failure.
138 static s32
sch_access(struct i2c_adapter
*adap
, u16 addr
,
139 unsigned short flags
, char read_write
,
140 u8 command
, int size
, union i2c_smbus_data
*data
)
142 int i
, len
, temp
, rc
;
144 /* Make sure the SMBus host is not busy */
145 temp
= inb(SMBHSTSTS
) & 0x0f;
147 dev_dbg(&sch_adapter
.dev
, "SMBus busy (%02x)\n", temp
);
150 temp
= inw(SMBHSTCLK
);
153 * We can't determine if we have 33 or 25 MHz clock for
154 * SMBus, so expect 33 MHz and calculate a bus clock of
155 * 100 kHz. If we actually run at 25 MHz the bus will be
156 * run ~75 kHz instead which should do no harm.
158 dev_notice(&sch_adapter
.dev
,
159 "Clock divider uninitialized. Setting defaults\n");
160 outw(backbone_speed
/ (4 * 100), SMBHSTCLK
);
163 dev_dbg(&sch_adapter
.dev
, "access size: %d %s\n", size
,
164 (read_write
)?"READ":"WRITE");
166 case I2C_SMBUS_QUICK
:
167 outb((addr
<< 1) | read_write
, SMBHSTADD
);
171 outb((addr
<< 1) | read_write
, SMBHSTADD
);
172 if (read_write
== I2C_SMBUS_WRITE
)
173 outb(command
, SMBHSTCMD
);
176 case I2C_SMBUS_BYTE_DATA
:
177 outb((addr
<< 1) | read_write
, SMBHSTADD
);
178 outb(command
, SMBHSTCMD
);
179 if (read_write
== I2C_SMBUS_WRITE
)
180 outb(data
->byte
, SMBHSTDAT0
);
181 size
= SCH_BYTE_DATA
;
183 case I2C_SMBUS_WORD_DATA
:
184 outb((addr
<< 1) | read_write
, SMBHSTADD
);
185 outb(command
, SMBHSTCMD
);
186 if (read_write
== I2C_SMBUS_WRITE
) {
187 outb(data
->word
& 0xff, SMBHSTDAT0
);
188 outb((data
->word
& 0xff00) >> 8, SMBHSTDAT1
);
190 size
= SCH_WORD_DATA
;
192 case I2C_SMBUS_BLOCK_DATA
:
193 outb((addr
<< 1) | read_write
, SMBHSTADD
);
194 outb(command
, SMBHSTCMD
);
195 if (read_write
== I2C_SMBUS_WRITE
) {
196 len
= data
->block
[0];
197 if (len
== 0 || len
> I2C_SMBUS_BLOCK_MAX
)
199 outb(len
, SMBHSTDAT0
);
200 for (i
= 1; i
<= len
; i
++)
201 outb(data
->block
[i
], SMBBLKDAT
+i
-1);
203 size
= SCH_BLOCK_DATA
;
206 dev_warn(&adap
->dev
, "Unsupported transaction %d\n", size
);
209 dev_dbg(&sch_adapter
.dev
, "write size %d to 0x%04x\n", size
, SMBHSTCNT
);
210 outb((inb(SMBHSTCNT
) & 0xb0) | (size
& 0x7), SMBHSTCNT
);
212 rc
= sch_transaction();
213 if (rc
) /* Error in transaction */
216 if ((read_write
== I2C_SMBUS_WRITE
) || (size
== SCH_QUICK
))
222 data
->byte
= inb(SMBHSTDAT0
);
225 data
->word
= inb(SMBHSTDAT0
) + (inb(SMBHSTDAT1
) << 8);
228 data
->block
[0] = inb(SMBHSTDAT0
);
229 if (data
->block
[0] == 0 || data
->block
[0] > I2C_SMBUS_BLOCK_MAX
)
231 for (i
= 1; i
<= data
->block
[0]; i
++)
232 data
->block
[i
] = inb(SMBBLKDAT
+i
-1);
238 static u32
sch_func(struct i2c_adapter
*adapter
)
240 return I2C_FUNC_SMBUS_QUICK
| I2C_FUNC_SMBUS_BYTE
|
241 I2C_FUNC_SMBUS_BYTE_DATA
| I2C_FUNC_SMBUS_WORD_DATA
|
242 I2C_FUNC_SMBUS_BLOCK_DATA
;
245 static const struct i2c_algorithm smbus_algorithm
= {
246 .smbus_xfer
= sch_access
,
247 .functionality
= sch_func
,
250 static struct i2c_adapter sch_adapter
= {
251 .owner
= THIS_MODULE
,
252 .class = I2C_CLASS_HWMON
| I2C_CLASS_SPD
,
253 .algo
= &smbus_algorithm
,
256 static int smbus_sch_probe(struct platform_device
*dev
)
258 struct resource
*res
;
261 res
= platform_get_resource(dev
, IORESOURCE_IO
, 0);
265 if (!devm_request_region(&dev
->dev
, res
->start
, resource_size(res
),
267 dev_err(&dev
->dev
, "SMBus region 0x%x already in use!\n",
272 sch_smba
= res
->start
;
274 dev_dbg(&dev
->dev
, "SMBA = 0x%X\n", sch_smba
);
276 /* set up the sysfs linkage to our parent device */
277 sch_adapter
.dev
.parent
= &dev
->dev
;
279 snprintf(sch_adapter
.name
, sizeof(sch_adapter
.name
),
280 "SMBus SCH adapter at %04x", sch_smba
);
282 retval
= i2c_add_adapter(&sch_adapter
);
289 static int smbus_sch_remove(struct platform_device
*pdev
)
292 i2c_del_adapter(&sch_adapter
);
299 static struct platform_driver smbus_sch_driver
= {
301 .name
= "isch_smbus",
303 .probe
= smbus_sch_probe
,
304 .remove
= smbus_sch_remove
,
307 module_platform_driver(smbus_sch_driver
);
309 MODULE_AUTHOR("Jacob Pan <jacob.jun.pan@intel.com>");
310 MODULE_DESCRIPTION("Intel SCH SMBus driver");
311 MODULE_LICENSE("GPL");
312 MODULE_ALIAS("platform:isch_smbus");