1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
6 * SMBus block read/write support added by Stefan Roese:
7 * Copyright (C) 2016 Stefan Roese <sr@denx.de>
16 /* PCI Configuration Space (D31:F3): SMBus */
19 #define HST_EN (1 << 0)
20 #define SMB_RCV_SLVA 0x09
23 #define SMBHSTSTAT 0x0
26 #define SMBXMITADD 0x4
27 #define SMBHSTDAT0 0x5
28 #define SMBHSTDAT1 0x6
30 #define SMBTRNSADD 0x9
31 #define SMBSLVDATA 0xa
33 #define SMLINK_PIN_CTL 0xe
34 #define SMBUS_PIN_CTL 0xf
36 /* I801 Hosts Status register bits */
37 #define SMBHSTSTS_BYTE_DONE 0x80
38 #define SMBHSTSTS_INUSE_STS 0x40
39 #define SMBHSTSTS_SMBALERT_STS 0x20
40 #define SMBHSTSTS_FAILED 0x10
41 #define SMBHSTSTS_BUS_ERR 0x08
42 #define SMBHSTSTS_DEV_ERR 0x04
43 #define SMBHSTSTS_INTR 0x02
44 #define SMBHSTSTS_HOST_BUSY 0x01
46 /* I801 Host Control register bits */
47 #define SMBHSTCNT_INTREN 0x01
48 #define SMBHSTCNT_KILL 0x02
49 #define SMBHSTCNT_LAST_BYTE 0x20
50 #define SMBHSTCNT_START 0x40
51 #define SMBHSTCNT_PEC_EN 0x80 /* ICH3 and later */
53 /* Auxiliary control register bits, ICH4+ only */
54 #define SMBAUXCTL_CRC 1
55 #define SMBAUXCTL_E32B 2
57 #define SMBUS_TIMEOUT 100 /* 100 ms */
64 static int smbus_wait_until_ready(u32 base)
71 byte = inb(base + SMBHSTSTAT);
74 } while (get_timer(ts) < SMBUS_TIMEOUT);
79 static int smbus_wait_until_done(u32 base)
86 byte = inb(base + SMBHSTSTAT);
87 if (!((byte & 1) || (byte & ~((1 << 6) | (1 << 0))) == 0))
89 } while (get_timer(ts) < SMBUS_TIMEOUT);
94 static int smbus_block_read(u32 base, u8 dev, u8 *buffer,
101 debug("%s (%d): dev=0x%x offs=0x%x len=0x%x\n",
102 __func__, __LINE__, dev, offset, len);
103 if (smbus_wait_until_ready(base) < 0)
106 /* Setup transaction */
108 /* Reset the data buffer index */
109 inb(base + SMBHSTCTL);
111 /* Set the device I'm talking too */
112 outb(((dev & 0x7f) << 1) | 1, base + SMBXMITADD);
113 /* Set the command/address... */
114 outb(offset & 0xff, base + SMBHSTCMD);
115 /* Set up for a block read */
116 outb((inb(base + SMBHSTCTL) & (~(0x7) << 2)) | (0x5 << 2),
118 /* Clear any lingering errors, so the transaction will run */
119 outb(inb(base + SMBHSTSTAT), base + SMBHSTSTAT);
121 /* Start the command */
122 outb((inb(base + SMBHSTCTL) | SMBHSTCNT_START), base + SMBHSTCTL);
124 /* Poll for transaction completion */
125 if (smbus_wait_until_done(base) < 0) {
126 printf("SMBUS read transaction timeout (dev=0x%x)\n", dev);
130 count = inb(base + SMBHSTDAT0);
131 debug("%s (%d): count=%d (len=%d)\n", __func__, __LINE__, count, len);
133 debug("ERROR: len=0 on read\n");
138 debug("ERROR: too few bytes read\n");
143 debug("ERROR: count=%d too high\n", count);
147 /* Read all available bytes from buffer */
148 for (i = 0; i < count; i++)
149 buf_temp[i] = inb(base + SMBBLKDAT);
151 memcpy(buffer, buf_temp, len);
153 /* Return results of transaction */
154 if (!(inb(base + SMBHSTSTAT) & SMBHSTSTS_INTR))
160 static int smbus_block_write(u32 base, u8 dev, u8 *buffer,
165 debug("%s (%d): dev=0x%x offs=0x%x len=0x%x\n",
166 __func__, __LINE__, dev, offset, len);
167 if (smbus_wait_until_ready(base) < 0)
170 /* Setup transaction */
171 /* Set the device I'm talking too */
172 outb(((dev & 0x7f) << 1) & ~0x01, base + SMBXMITADD);
173 /* Set the command/address... */
174 outb(offset, base + SMBHSTCMD);
175 /* Set up for a block write */
176 outb((inb(base + SMBHSTCTL) & (~(0x7) << 2)) | (0x5 << 2),
178 /* Clear any lingering errors, so the transaction will run */
179 outb(inb(base + SMBHSTSTAT), base + SMBHSTSTAT);
181 /* Write count in DAT0 register */
182 outb(len, base + SMBHSTDAT0);
184 /* Write data bytes... */
185 for (i = 0; i < len; i++)
186 outb(*buffer++, base + SMBBLKDAT);
188 /* Start the command */
189 outb((inb(base + SMBHSTCTL) | SMBHSTCNT_START), base + SMBHSTCTL);
191 /* Poll for transaction completion */
192 if (smbus_wait_until_done(base) < 0) {
193 printf("SMBUS write transaction timeout (dev=0x%x)\n", dev);
197 /* Return results of transaction */
198 if (!(inb(base + SMBHSTSTAT) & SMBHSTSTS_INTR))
204 static int intel_i2c_xfer(struct udevice *bus, struct i2c_msg *msg, int nmsgs)
206 struct intel_i2c *i2c = dev_get_priv(bus);
207 struct i2c_msg *dmsg, *omsg, dummy;
209 debug("i2c_xfer: %d messages\n", nmsgs);
211 memset(&dummy, 0, sizeof(struct i2c_msg));
214 * We expect either two messages (one with an offset and one with the
215 * actucal data) or one message (just data)
217 if (nmsgs > 2 || nmsgs == 0) {
218 debug("%s: Only one or two messages are supported", __func__);
222 omsg = nmsgs == 1 ? &dummy : msg;
223 dmsg = nmsgs == 1 ? msg : msg + 1;
225 if (dmsg->flags & I2C_M_RD)
226 return smbus_block_read(i2c->base, dmsg->addr, &dmsg->buf[0],
227 omsg->buf[0], dmsg->len);
229 return smbus_block_write(i2c->base, dmsg->addr, &dmsg->buf[1],
230 dmsg->buf[0], dmsg->len - 1);
233 static int intel_i2c_probe_chip(struct udevice *bus, uint chip_addr,
236 struct intel_i2c *i2c = dev_get_priv(bus);
239 return smbus_block_read(i2c->base, chip_addr, buf, 0, 1);
242 static int intel_i2c_set_bus_speed(struct udevice *bus, unsigned int speed)
247 static int intel_i2c_probe(struct udevice *dev)
249 struct intel_i2c *priv = dev_get_priv(dev);
252 /* Save base address from PCI BAR */
253 priv->base = (ulong)dm_pci_map_bar(dev, PCI_BASE_ADDRESS_4,
257 /* Set SMBus enable. */
258 dm_pci_write_config8(dev, HOSTC, HST_EN);
260 /* Disable interrupts */
261 outb(inb(base + SMBHSTCTL) & ~SMBHSTCNT_INTREN, base + SMBHSTCTL);
263 /* Set 32-byte data buffer mode */
264 outb(inb(base + SMBAUXCTL) | SMBAUXCTL_E32B, base + SMBAUXCTL);
269 static int intel_i2c_bind(struct udevice *dev)
271 static int num_cards __attribute__ ((section(".data")));
274 /* Create a unique device name for PCI type devices */
275 if (device_is_on_pci_bus(dev)) {
278 * Setting req_seq in the driver is probably not recommended.
279 * But without a DT alias the number is not configured. And
280 * using this driver is impossible for PCIe I2C devices.
281 * This can be removed, once a better (correct) way for this
282 * is found and implemented.
284 dev->req_seq = num_cards;
285 sprintf(name, "intel_i2c#%u", num_cards++);
286 device_set_name(dev, name);
292 static const struct dm_i2c_ops intel_i2c_ops = {
293 .xfer = intel_i2c_xfer,
294 .probe_chip = intel_i2c_probe_chip,
295 .set_bus_speed = intel_i2c_set_bus_speed,
298 static const struct udevice_id intel_i2c_ids[] = {
299 { .compatible = "intel,ich-i2c" },
303 U_BOOT_DRIVER(intel_i2c) = {
306 .of_match = intel_i2c_ids,
307 .ops = &intel_i2c_ops,
308 .priv_auto_alloc_size = sizeof(struct intel_i2c),
309 .bind = intel_i2c_bind,
310 .probe = intel_i2c_probe,
313 static struct pci_device_id intel_smbus_pci_supported[] = {
314 /* Intel BayTrail SMBus on the PCI bus */
315 { PCI_VDEVICE(INTEL, 0x0f12) },
316 /* Intel IvyBridge (Panther Point PCH) SMBus on the PCI bus */
317 { PCI_VDEVICE(INTEL, 0x1e22) },
321 U_BOOT_PCI_DEVICE(intel_i2c, intel_smbus_pci_supported);