xref: /illumos-gate/usr/src/uts/common/io/i2c/ctrl/pchsmbus/pchsmbus.c (revision 9fe9c13c87b112555793ee3ed5d7de3ef769b461)
1 /*
2  * This file and its contents are supplied under the terms of the
3  * Common Development and Distribution License ("CDDL"), version 1.0.
4  * You may only use this file in accordance with the terms of version
5  * 1.0 of the CDDL.
6  *
7  * A full copy of the text of the CDDL should have accompanied this
8  * source.  A copy of the CDDL is also available via the Internet at
9  * http://www.illumos.org/license/CDDL.
10  */
11 
12 /*
13  * Copyright 2026 Oxide Computer Company
14  */
15 
16 /*
17  * Intel PCH (ICH) SMBus Controller
18  *
19  * This driver supports a wide variety of controllers, having been found in
20  * various Intel chipsets since the late 1990s. The hardware has evolved a
21  * little bit, but it is a controller that only supports SMBus 2.0 and a little
22  * bit of I2C emulation to support EEPROMs that fit in a single address byte. It
23  * cannot run arbitrary I2C commands.
24  *
25  * As a result, the hardware interface is structured around issuing specific
26  * SMBus commands and operations. For non-block based commands this is
27  * straightforward. Unfortunately, for block commands it is less simple. In the
28  * hardware's evolution, support for a block buffer was added. Prior to this one
29  * has to read and write a single byte at a time. With this, one can instead use
30  * the 32-byte buffer for transactions. Notably, 32 bytes comes from the SMBus
31  * 2.0 block limit.
32  *
33  * While this 32-byte buffer is a great simplifying thing, it actually doesn't
34  * work with I2C emulation and therefore we have to do per-byte I/Os in the
35  * device. Because I2C block I/Os are much more common than SMBus, this means
36  * that the block buffer flag, like the I2C flag, are enabled on a per-request
37  * basis.
38  *
39  * When operating in the byte mode, we basically track how many bytes there are
40  * to transmit and receive and will issue all transmit bytes before any repeated
41  * start that requires reading.
42  */
43 
44 #include <sys/modctl.h>
45 #include <sys/conf.h>
46 #include <sys/devops.h>
47 #include <sys/ddi.h>
48 #include <sys/sunddi.h>
49 #include <sys/pci.h>
50 #include <sys/sysmacros.h>
51 
52 #include <sys/i2c/controller.h>
53 #include "pchsmbus.h"
54 
55 /*
56  * The controller only has a single BAR which contains what we need. This may be
57  * in I/O space or MMIO space, but which it is doesn't make a difference to the
58  * driver itself.
59  */
60 #define	PCHSMBUS_REGNO	1
61 
62 typedef enum {
63 	PCHSMBUS_INIT_PCI		= 1 << 0,
64 	PCHSMBUS_INIT_REGS		= 1 << 1,
65 	PCHSMBUS_INIT_INTR_ALLOC	= 1 << 2,
66 	PCHSMBUS_INIT_INTR_HDL		= 1 << 3,
67 	PCHSMBUS_INIT_SYNC		= 1 << 4,
68 	PCHSMBUS_INIT_CTRL		= 1 << 5,
69 	PCHSMBUS_INIT_INTR_EN		= 1 << 6,
70 	PCHSMBUS_INIT_I2C		= 1 << 7,
71 	/*
72 	 * The following are used at run time.
73 	 */
74 	PCHSMBUS_RUN_BUF_EN		= 1 << 8,
75 	PCHSMBUS_RUN_I2C_EN		= 1 << 9
76 } pchsmbus_init_t;
77 
78 typedef struct {
79 	dev_info_t *ps_dip;
80 	ddi_acc_handle_t ps_cfg;
81 	pchsmbus_init_t ps_init;
82 	pch_smbus_feat_t ps_feats;
83 	/*
84 	 * Register related data
85 	 */
86 	caddr_t ps_base;
87 	off_t ps_regsize;
88 	ddi_acc_handle_t ps_regs;
89 	uint32_t ps_init_hcfg;
90 	uint8_t ps_init_hctl;
91 	uint8_t ps_init_scmd;
92 	/*
93 	 * Interrupt data
94 	 */
95 	int ps_nintrs;
96 	ddi_intr_handle_t ps_intr_hdl;
97 	uint_t ps_intr_pri;
98 	/*
99 	 * Request and framework synchronization
100 	 */
101 	kmutex_t ps_mutex;
102 	kcondvar_t ps_cv;
103 	i2c_ctrl_hdl_t *ps_hdl;
104 	smbus_req_t *ps_req;
105 	uint16_t ps_req_off;
106 	uint8_t ps_req_hctl;
107 	bool ps_req_done;
108 	i2c_ctrl_error_t ps_kill_err;
109 } pchsmbus_t;
110 
111 typedef struct {
112 	uint16_t pcm_did;
113 	pch_smbus_feat_t pcm_feat;
114 } pchsmbus_hw_map_t;
115 
116 static const pchsmbus_hw_map_t pchsmbus_feats[] = {
117 	{ PCH_SMBUS_ICH0_82801AA, 0 },
118 	{ PCH_SMBUS_ICH0_82901AB, 0 },
119 	{ PCH_SMBUS_ICH2_82801BA, PCH_SMBUS_FEAT_TARG },
120 	{ PCH_SMBUS_ICH3_82801CA, PCH_SMBUS_FEAT_ALL_ICH3 },
121 	{ PCH_SMBUS_ICH4_82801DB, PCH_SMBUS_FEAT_ALL_ICH4 },
122 	{ PCH_SMBUS_ICH5_82801Ex, PCH_SMBUS_FEAT_ALL_ICH5 },
123 	{ PCH_SMBUS_6300ESB, PCH_SMBUS_FEAT_ALL_ICH5 },
124 	{ PCH_SMBUS_ICH6, PCH_SMBUS_FEAT_ALL_ICH5 },
125 	{ PCH_SMBUS_631xESB, PCH_SMBUS_FEAT_ALL_ICH5 },
126 	{ PCH_SMBUS_ICH7, PCH_SMBUS_FEAT_ALL_ICH5 },
127 	{ PCH_SMBUS_ICH8, PCH_SMBUS_FEAT_ALL_ICH8 },
128 	{ PCH_SMBUS_ICH9, PCH_SMBUS_FEAT_ALL_ICH8 },
129 	{ PCH_SMBUS_ICH10_CORP, PCH_SMBUS_FEAT_ALL_ICH8 },
130 	{ PCH_SMBUS_ICH10_USER, PCH_SMBUS_FEAT_ALL_ICH8 },
131 	{ PCH_SMBUS_PCH5, PCH_SMBUS_FEAT_ALL_ICH8 },
132 	{ PCH_SMBUS_C600, PCH_SMBUS_FEAT_ALL_ICH8 },
133 	{ PCH_SMBUS_C600_SMB0, PCH_SMBUS_FEAT_ALL_ICH8 },
134 	{ PCH_SMBUS_C600_SMB1, PCH_SMBUS_FEAT_ALL_ICH8 },
135 	{ PCH_SMBUS_C600_SMB2, PCH_SMBUS_FEAT_ALL_ICH8 },
136 	{ PCH_SMBUS_DH89xxCC, PCH_SMBUS_FEAT_ALL_ICH8 },
137 	{ PCH_SMBUS_DH89xxCL, PCH_SMBUS_FEAT_ALL_ICH8 },
138 	{ PCH_SMBUS_PCH6, PCH_SMBUS_FEAT_ALL_ICH8 },
139 	{ PCH_SMBUS_PCH7, PCH_SMBUS_FEAT_ALL_ICH8 },
140 	{ PCH_SMBUS_PCH8, PCH_SMBUS_FEAT_ALL_ICH8 },
141 	{ PCH_SMBUS_PCH8_LP, PCH_SMBUS_FEAT_ALL_ICH8 },
142 	{ PCH_SMBUS_C610, PCH_SMBUS_FEAT_ALL_ICH8 },
143 	{ PCH_SMBUS_C610_MS0, PCH_SMBUS_FEAT_ALL_ICH8 },
144 	{ PCH_SMBUS_C610_MS1, PCH_SMBUS_FEAT_ALL_ICH8 },
145 	{ PCH_SMBUS_C610_MS2, PCH_SMBUS_FEAT_ALL_ICH8 },
146 	{ PCH_SMBUS_PCH9, PCH_SMBUS_FEAT_ALL_ICH8 },
147 	{ PCH_SMBUS_PCH9_LP, PCH_SMBUS_FEAT_ALL_ICH8 },
148 	{ PCH_SMBUS_BAYTRAIL, PCH_SMBUS_FEAT_ALL_ICH8 },
149 	{ PCH_SMBUS_100, PCH_SMBUS_FEAT_ALL_ICH8 },
150 	{ PCH_SMBUS_DENVERTON, PCH_SMBUS_FEAT_ALL_ICH8 },
151 	{ PCH_SMBUS_C740, PCH_SMBUS_FEAT_ALL_ICH8 },
152 	{ PCH_SMBUS_APOLLO, PCH_SMBUS_FEAT_ALL_ICH8 },
153 	{ PCH_SMBUS_200, PCH_SMBUS_FEAT_ALL_ICH8 },
154 	{ PCH_SMBUS_GEMINI, PCH_SMBUS_FEAT_ALL_ICH8 },
155 	{ PCH_SMBUS_C620, PCH_SMBUS_FEAT_ALL_ICH8 },
156 	{ PCH_SMBUS_C620_SUPER, PCH_SMBUS_FEAT_ALL_ICH8 },
157 	{ PCH_SMBUS_300, PCH_SMBUS_FEAT_ALL_ICH8 },
158 	{ PCH_SMBUS_ICE_LAKE_D, PCH_SMBUS_FEAT_ALL_ICH8 },
159 	{ PCH_SMBUS_495_PKG, PCH_SMBUS_FEAT_ALL_ICH8 },
160 	{ PCH_SMBUS_400, PCH_SMBUS_FEAT_ALL_ICH8 },
161 	{ PCH_SMBUS_400_PKG, PCH_SMBUS_FEAT_ALL_ICH8 },
162 	{ PCH_SMBUS_ELKHART, PCH_SMBUS_FEAT_ALL_ICH8 },
163 	{ PCH_SMBUS_500, PCH_SMBUS_FEAT_ALL_ICH8 },
164 	{ PCH_SMBUS_500_PKG, PCH_SMBUS_FEAT_ALL_ICH8 },
165 	{ PCH_SMBUS_JASPER, PCH_SMBUS_FEAT_ALL_ICH8 },
166 	{ PCH_SMBUS_600, PCH_SMBUS_FEAT_ALL_ICH8 },
167 	{ PCH_SMBUS_600_PKG, PCH_SMBUS_FEAT_ALL_ICH8 },
168 	{ PCH_SMBUS_ALDER_N, PCH_SMBUS_FEAT_ALL_ICH8 },
169 	{ PCH_SMBUS_700, PCH_SMBUS_FEAT_ALL_ICH8 },
170 	{ PCH_SMBUS_800, PCH_SMBUS_FEAT_ALL_ICH8 },
171 	{ PCH_SMBUS_METEOR_PS, PCH_SMBUS_FEAT_ALL_ICH8 },
172 	{ PCH_SMBUS_PANTHER_H, PCH_SMBUS_FEAT_ALL_ICH8 },
173 	{ PCH_SMBUS_PANTHER_P, PCH_SMBUS_FEAT_ALL_ICH8 },
174 	{ PCH_SMBUS_900, PCH_SMBUS_FEAT_ALL_ICH8 }
175 };
176 
177 static uint8_t
pchsmbus_read8(pchsmbus_t * pch,uint8_t regno)178 pchsmbus_read8(pchsmbus_t *pch, uint8_t regno)
179 {
180 	ASSERT3U(regno, <, pch->ps_regsize);
181 
182 	return (ddi_get8(pch->ps_regs, (uint8_t *)(pch->ps_base + regno)));
183 }
184 
185 static void
pchsmbus_write8(pchsmbus_t * pch,uint8_t regno,uint8_t val)186 pchsmbus_write8(pchsmbus_t *pch, uint8_t regno, uint8_t val)
187 {
188 	ASSERT3U(regno, <, pch->ps_regsize);
189 
190 	ddi_put8(pch->ps_regs, (uint8_t *)(pch->ps_base + regno), val);
191 }
192 
193 static i2c_errno_t
pchsmbus_prop_info(void * arg,i2c_prop_t prop,i2c_prop_info_t * info)194 pchsmbus_prop_info(void *arg, i2c_prop_t prop, i2c_prop_info_t *info)
195 {
196 	switch (prop) {
197 	case I2C_PROP_BUS_SPEED:
198 		i2c_prop_info_set_def_u32(info, I2C_SPEED_STD);
199 		i2c_prop_info_set_pos_bit32(info, I2C_SPEED_STD);
200 		break;
201 	case SMBUS_PROP_SUP_OPS:
202 	case SMBUS_PROP_MAX_BLOCK:
203 		break;
204 	default:
205 		return (I2C_PROP_E_UNSUP);
206 	}
207 
208 	/*
209 	 * We can't set any timing properties or the speed really, so we
210 	 * indicate that all properties are read-only.
211 	 */
212 	i2c_prop_info_set_perm(info, I2C_PROP_PERM_RO);
213 
214 	return (I2C_CORE_E_OK);
215 }
216 
217 static i2c_errno_t
pchsmbus_prop_get(void * arg,i2c_prop_t prop,void * buf,size_t buflen)218 pchsmbus_prop_get(void *arg, i2c_prop_t prop, void *buf, size_t buflen)
219 {
220 	uint32_t val;
221 
222 	switch (prop) {
223 	case I2C_PROP_BUS_SPEED:
224 		val = I2C_SPEED_STD;
225 		break;
226 	case SMBUS_PROP_SUP_OPS:
227 		val = SMBUS_PROP_OP_QUICK_COMMAND | SMBUS_PROP_OP_SEND_BYTE |
228 		    SMBUS_PROP_OP_RECV_BYTE | SMBUS_PROP_OP_WRITE_BYTE |
229 		    SMBUS_PROP_OP_READ_BYTE | SMBUS_PROP_OP_WRITE_WORD |
230 		    SMBUS_PROP_OP_READ_WORD | SMBUS_PROP_OP_PROCESS_CALL |
231 		    SMBUS_PROP_OP_WRITE_BLOCK | SMBUS_PROP_OP_READ_BLOCK |
232 		    SMBUS_PROP_OP_BLOCK_PROCESS_CALL |
233 		    SMBUS_PROP_OP_I2C_WRITE_BLOCK |
234 		    SMBUS_PROP_OP_I2C_READ_BLOCK;
235 		break;
236 	case SMBUS_PROP_MAX_BLOCK:
237 		val = SMBUS_V2_MAX_BLOCK;
238 		break;
239 	default:
240 		return (I2C_PROP_E_UNSUP);
241 	}
242 
243 	VERIFY3U(buflen, >=, sizeof (val));
244 	bcopy(&val, buf, sizeof (val));
245 	return (I2C_CORE_E_OK);
246 }
247 
248 static bool
pchsmbus_bus_avail(pchsmbus_t * pch)249 pchsmbus_bus_avail(pchsmbus_t *pch)
250 {
251 	const uint32_t count = i2c_ctrl_timeout_count(pch->ps_hdl,
252 	    I2C_CTRL_TO_BUS_ACT);
253 	const uint32_t wait = i2c_ctrl_timeout_delay_us(pch->ps_hdl,
254 	    I2C_CTRL_TO_BUS_ACT);
255 
256 	for (uint32_t i = 0; i < count; i++) {
257 		uint8_t hsts = pchsmbus_read8(pch, PCH_R_BAR_HSTS);
258 		if ((hsts & PCH_HSTS_BUSY) == 0) {
259 			return (true);
260 		}
261 
262 		delay(drv_usectohz(wait));
263 	}
264 
265 	dev_err(pch->ps_dip, CE_WARN, "controller timed out waiting for "
266 	    "bus activity to cease");
267 	return (false);
268 }
269 
270 static void
pchsmbus_set_addr(pchsmbus_t * pch,const smbus_req_t * req,bool write)271 pchsmbus_set_addr(pchsmbus_t *pch, const smbus_req_t *req, bool write)
272 {
273 	uint8_t addr = 0;
274 	uint8_t wbit = write ? PCH_R_TSA_RW_WRITE : PCH_R_TSA_RW_READ;
275 
276 
277 	ASSERT3U(req->smbr_addr.ia_type, ==, I2C_ADDR_7BIT);
278 	addr = PCH_R_TSA_SET_ADDR(addr, req->smbr_addr.ia_addr);
279 	addr = PCH_R_TSA_SET_RW(addr, wbit);
280 	pchsmbus_write8(pch, PCH_R_BAR_TSA, addr);
281 }
282 
283 static pch_smbus_cmd_t
pchsmbus_req_to_cmd(const smbus_req_t * req)284 pchsmbus_req_to_cmd(const smbus_req_t *req)
285 {
286 	switch (req->smbr_op) {
287 	case SMBUS_OP_QUICK_COMMAND:
288 		return (PCH_SMBUS_CMD_QUICK);
289 	case SMBUS_OP_SEND_BYTE:
290 	case SMBUS_OP_RECV_BYTE:
291 		return (PCH_SMBUS_CMD_BYTE);
292 	case SMBUS_OP_WRITE_BYTE:
293 	case SMBUS_OP_READ_BYTE:
294 		return (PCH_SMBUS_CMD_BYTE_DATA);
295 	case SMBUS_OP_WRITE_WORD:
296 	case SMBUS_OP_READ_WORD:
297 		return (PCH_SMBUS_CMD_WORD_DATA);
298 	case SMBUS_OP_WRITE_BLOCK:
299 	case SMBUS_OP_READ_BLOCK:
300 	case SMBUS_OP_I2C_WRITE_BLOCK:
301 		return (PCH_SMBUS_CMD_BLOCK);
302 	case SMBUS_OP_PROCESS_CALL:
303 		return (PCH_SMBUS_CMD_PROC_CALL);
304 	case SMBUS_OP_BLOCK_PROCESS_CALL:
305 		return (PCH_SMBUS_CMD_BLOCK_PROC);
306 	case SMBUS_OP_I2C_READ_BLOCK:
307 		return (PCH_SMBUS_CMD_I2C_READ);
308 	default:
309 		panic("asked to translate unexpected request type: 0x%x",
310 		    req->smbr_op);
311 	}
312 }
313 
314 /*
315  * Initialize a block request. These are the most complicated requests to deal
316  * with because of the different variations. There are two modes of operation: a
317  * 32 byte buffer that we can use to just take care of the operation in one shot
318  * or a single byte at a time operation. Most hardware supports the 32 byte
319  * buffer; however, when performing an I2C read or write, it must operate in
320  * byte at a time mode.
321  *
322  * We also need to update the controller registers. This means specifically:
323  *
324  *  - Enabling I2C mode specifically for I2C block writes. This is not required
325  *    for I2C block reads which have their own dedicated command in the
326  *    controller.
327  *  - Enabling the 32-byte buffer for block reads when it's supported in
328  *    hardware. This cannot be done for I2C operations and must be done for
329  *    block procedure calls. We will not advertise support for block procedure
330  *    calls to the framework if they are not supported in hardware.
331  *
332  * Note, regardless of whether this is the 'i2c' form or not, we are going to
333  * end up issuing the 'command' register. When doing an i2c block read, the
334  * controller will issue a repeated start and do the transition to a read.
335  */
336 static void
pchsmbus_io_init_block(pchsmbus_t * pch,const smbus_req_t * req)337 pchsmbus_io_init_block(pchsmbus_t *pch, const smbus_req_t *req)
338 {
339 	bool write, want_buf = false, want_i2c = false;
340 
341 	switch (req->smbr_op) {
342 	case SMBUS_OP_WRITE_BLOCK:
343 	case SMBUS_OP_BLOCK_PROCESS_CALL:
344 		want_buf = true;
345 		write = true;
346 		break;
347 	case SMBUS_OP_READ_BLOCK:
348 		want_buf = true;
349 		write = false;
350 		break;
351 	case SMBUS_OP_I2C_WRITE_BLOCK:
352 		write = true;
353 		want_buf = false;
354 		/*
355 		 * This is the only operation that requires an explicit I2C
356 		 * enable. This causes us to skip sending the byte count. This
357 		 * isn't required for the I2C Read Block operation because it's
358 		 * just part of the controller's semantics.
359 		 */
360 		want_i2c = true;
361 		break;
362 	case SMBUS_OP_I2C_READ_BLOCK:
363 		/*
364 		 * Yes, this seems on the face an oxymoron. The reason for this
365 		 * is buried in the datasheets (though some are inconsistent).
366 		 * When issuing an I2C block read we first are going to do a
367 		 * write with a byte and then issue a repeated start with a
368 		 * read. The first thing we do will be a write, hence we set
369 		 * this.
370 		 *
371 		 * However, this gets more nuanced. There exists the SPD write
372 		 * disable bit which was added in the PCH7 generation. When this
373 		 * is set, this needs to be false. This is likely why some
374 		 * chipsets after this generation say this should always be
375 		 * treated as a read (i.e. Ice Lake-D); however, this is also
376 		 * contradicted by other devices between PCH7 and Ice Lake such
377 		 * as the 100/200-series chipsets. A right mess, isn't it?
378 		 */
379 		write = PCH_R_HCFG_GET_SPDWD(pch->ps_init_hcfg) == 0;
380 		break;
381 	default:
382 		panic("programmer error: not a block type: 0x%x\n",
383 		    req->smbr_op);
384 	}
385 
386 	if ((pch->ps_feats & PCH_SMBUS_FEAT_32B_BUF) == 0)
387 		want_buf = false;
388 
389 	VERIFY(!(want_i2c && want_buf));
390 	if (want_i2c) {
391 		uint32_t val = pci_config_get32(pch->ps_cfg, PCH_R_PCIE_HCFG);
392 		val = PCH_R_HCFG_SET_I2CEN(val, 1);
393 		pci_config_put32(pch->ps_cfg, PCH_R_PCIE_HCFG, val);
394 		pch->ps_init |= PCHSMBUS_RUN_I2C_EN;
395 	}
396 
397 	if (want_buf) {
398 		uint8_t val = pchsmbus_read8(pch, PCH_R_BAR_AUXC);
399 		val = PCH_R_AUXC_SET_E32B(val, 1);
400 		pchsmbus_write8(pch, PCH_R_BAR_AUXC, val);
401 		pch->ps_init |= PCHSMBUS_RUN_BUF_EN;
402 	}
403 
404 	/*
405 	 * All operations get the address and the command register set. Though
406 	 * of course the I2C Block read actually doesn't use the command
407 	 * register and instead uses the data 1 register for it.
408 	 */
409 	pchsmbus_set_addr(pch, req, write);
410 	if (req->smbr_op == SMBUS_OP_I2C_READ_BLOCK) {
411 		pchsmbus_write8(pch, PCH_R_BAR_HD1, req->smbr_cmd);
412 	} else {
413 		pchsmbus_write8(pch, PCH_R_BAR_HCMD, req->smbr_cmd);
414 	}
415 
416 	/*
417 	 * If this is a read command, there is nothing else to do. For the
418 	 * various write types we must actually write the data in question.
419 	 */
420 	if (req->smbr_op == SMBUS_OP_I2C_READ_BLOCK || SMBUS_OP_READ_BLOCK) {
421 		return;
422 	}
423 
424 	/*
425 	 * For all writes, regardless of length, indicate how many bytes are in
426 	 * the transaction.
427 	 */
428 	pchsmbus_write8(pch, PCH_R_BAR_HD0, req->smbr_wlen);
429 	uint16_t wlen = req->smbr_wlen;
430 	if (!want_buf) {
431 		wlen = 1;
432 	}
433 
434 	/*
435 	 * Explicitly reset the index into the buffer. This is a nop if we're
436 	 * not using the buffer.
437 	 */
438 	(void) pchsmbus_read8(pch, PCH_R_BAR_HCTL);
439 	for (uint16_t i = 0; i < wlen; i++, pch->ps_req_off++) {
440 		pchsmbus_write8(pch, PCH_R_BAR_HBD, req->smbr_wdata[i]);
441 	}
442 }
443 
444 /*
445  * We have one of three different general classes of errors that we need to
446  * prioritize and synthesize into useful errors upstack. We treat them in the
447  * following order:
448  *
449  * 1) The FAIL error takes priority as this is set due to a request by us to
450  *    abort the error. The driver sets the appropriate error to use in our
451  *    device structure before issuing this.
452  * 2) The Bus Error indicates that something went wrong on the bus itself. The
453  *    datasheet says it's a general transaction collision or a bus arbitration
454  *    loss. We always translate that into I2C_CTRL_E_ARB_LOST.
455  * 3) The device error is a combination of different possibilities. The most
456  *    common case is getting no acknowledgement. However, this can also happen
457  *    because the driver requests an illegal command, a PEC error occurs, or we
458  *    exceed the 25 ms SMBus timeout. This is definitely unfortunate, but we
459  *    basically just stick to the unknown I2C_CTRL_E_NACK.
460  */
461 static void
pchsmbus_io_error(pchsmbus_t * pch,pch_smbus_sts_t status)462 pchsmbus_io_error(pchsmbus_t *pch, pch_smbus_sts_t status)
463 {
464 	i2c_ctrl_error_t err;
465 
466 	if ((status & PCH_HSTS_FAIL) != 0) {
467 		ASSERT3U(pch->ps_kill_err, !=, I2C_CTRL_E_OK);
468 		err = pch->ps_kill_err;
469 	} else if ((status & PCH_HSTS_BUS_ERR)) {
470 		err = I2C_CTRL_E_ARB_LOST;
471 	} else {
472 		err = I2C_CTRL_E_NACK;
473 	}
474 
475 	i2c_ctrl_io_error(&pch->ps_req->smbr_error, I2C_CORE_E_CONTROLLER,
476 	    err);
477 	pch->ps_req_done = true;
478 }
479 
480 /*
481  * We have received a byte done callback. This means that we're either
482  * performing a read or write. The controller does not support performing both
483  * without the buffer enabled.
484  *
485  * If we are writing, we need to write the next byte into the buffer. If there
486  * is any more.
487  *
488  * If we are reading, we need to read the next byte out of the buffer. It the
489  * subsequent byte (the one after we just read) would be the last one, then we
490  * need to indicate to the controller that this it will be the last byte. When
491  * executing an SMBus block read, the data length is not known in advance.
492  *
493  * In both cases, reading or writing all bytes is not indicating of completing
494  * the command. The controller explicitly sets the INTR status bit for that.
495  */
496 static void
pchsmbus_io_byte_done(pchsmbus_t * pch)497 pchsmbus_io_byte_done(pchsmbus_t *pch)
498 {
499 	ASSERT(MUTEX_HELD(&pch->ps_mutex));
500 	ASSERT3U(pch->ps_init & PCHSMBUS_RUN_BUF_EN, ==, 0);
501 	ASSERT3P(pch->ps_req, !=, NULL);
502 
503 	if (pch->ps_req->smbr_op == SMBUS_OP_WRITE_BLOCK ||
504 	    pch->ps_req->smbr_op == SMBUS_OP_I2C_WRITE_BLOCK) {
505 		if (pch->ps_req_off < pch->ps_req->smbr_wlen) {
506 			pchsmbus_write8(pch, PCH_R_BAR_HBD,
507 			    pch->ps_req->smbr_wdata[pch->ps_req_off]);
508 			pch->ps_req_off++;
509 		}
510 		return;
511 	}
512 
513 	/*
514 	 * I2C block reads already know the size that they care about. However,
515 	 * normal SMBus block reads have it in their first byte, which will be
516 	 * in the HD0 register, not the HDB register like normal data.
517 	 */
518 	if (pch->ps_req->smbr_rlen == 0) {
519 		ASSERT3U(pch->ps_req->smbr_op, ==, SMBUS_OP_READ_BLOCK);
520 
521 		uint8_t len = pchsmbus_read8(pch, PCH_R_BAR_HD0);
522 		if (len == 0 || len > SMBUS_V2_MAX_BLOCK) {
523 			pch->ps_kill_err = I2C_CTRL_E_BAD_SMBUS_RLEN;
524 			uint8_t val = PCH_R_HCTL_SET_KILL(0, 1);
525 			pchsmbus_write8(pch, PCH_R_BAR_HCTL, val);
526 			return;
527 		}
528 		pch->ps_req->smbr_rlen = len;
529 		return;
530 	}
531 
532 	pch->ps_req->smbr_rdata[pch->ps_req_off] = pchsmbus_read8(pch,
533 	    PCH_R_BAR_HBD);
534 	pch->ps_req_off++;
535 	if (pch->ps_req_off + 1 == pch->ps_req->smbr_rlen) {
536 		uint8_t hctl = PCH_R_HCTL_SET_LAST(pch->ps_req_hctl, 1);
537 		pchsmbus_write8(pch, PCH_R_BAR_HCTL, hctl);
538 	}
539 }
540 
541 /*
542  * We've been told that the request completed successfully. The action that we
543  * must take will vary based upon the type of request. Here is where we read out
544  * result data. For writes, we're simply done. Note, for block requests, we will
545  * have already processed it if we're not operating in block mode.
546  */
547 static void
pchsmbus_io_req_done(pchsmbus_t * pch)548 pchsmbus_io_req_done(pchsmbus_t *pch)
549 {
550 	uint8_t len;
551 
552 	pch->ps_req_done = true;
553 	switch (pch->ps_req->smbr_op) {
554 	case SMBUS_OP_QUICK_COMMAND:
555 	case SMBUS_OP_SEND_BYTE:
556 	case SMBUS_OP_WRITE_BYTE:
557 	case SMBUS_OP_WRITE_WORD:
558 	case SMBUS_OP_WRITE_BLOCK:
559 	case SMBUS_OP_I2C_WRITE_BLOCK:
560 		/*
561 		 * There is nothing to do for all write requests.
562 		 */
563 		break;
564 	case SMBUS_OP_RECV_BYTE:
565 	case SMBUS_OP_READ_BYTE:
566 		pch->ps_req->smbr_rdata[0] = pchsmbus_read8(pch, PCH_R_BAR_HD0);
567 		break;
568 	case SMBUS_OP_READ_WORD:
569 	case SMBUS_OP_PROCESS_CALL:
570 		pch->ps_req->smbr_rdata[0] = pchsmbus_read8(pch, PCH_R_BAR_HD0);
571 		pch->ps_req->smbr_rdata[1] = pchsmbus_read8(pch, PCH_R_BAR_HD1);
572 		break;
573 	case SMBUS_OP_READ_BLOCK:
574 	case SMBUS_OP_BLOCK_PROCESS_CALL:
575 	case SMBUS_OP_I2C_READ_BLOCK:
576 		/*
577 		 * Byte mode already has all of its data.
578 		 */
579 		if ((pch->ps_init & PCHSMBUS_RUN_BUF_EN) == 0) {
580 			break;
581 		}
582 
583 		len = pchsmbus_read8(pch, PCH_R_BAR_HD0);
584 		if (len == 0 || len > SMBUS_V2_MAX_BLOCK) {
585 			i2c_ctrl_io_error(&pch->ps_req->smbr_error,
586 			    I2C_CORE_E_CONTROLLER,
587 			    I2C_CTRL_E_BAD_SMBUS_RLEN);
588 			return;
589 		}
590 
591 		pch->ps_req->smbr_rlen = len;
592 		/* Explicitly reset the buffer index */
593 		(void) pchsmbus_read8(pch, PCH_R_BAR_HCTL);
594 		for (uint16_t i = 0; i < pch->ps_req->smbr_rlen; i++) {
595 			pch->ps_req->smbr_rdata[i] = pchsmbus_read8(pch,
596 			    PCH_R_BAR_HBD);
597 		}
598 		break;
599 	case SMBUS_OP_HOST_NOTIFY:
600 	case SMBUS_OP_WRITE_U32:
601 	case SMBUS_OP_READ_U32:
602 	case SMBUS_OP_WRITE_U64:
603 	case SMBUS_OP_READ_U64:
604 	default:
605 		panic("programmer error: unsupported request type 0x%x should "
606 		    "not have been completed", pch->ps_req->smbr_op);
607 	}
608 
609 	i2c_ctrl_io_success(&pch->ps_req->smbr_error);
610 }
611 
612 /*
613  * We have been given a status register read from the driver, whether by polling
614  * or by an interrupt. We must look at the bits present, clear anything that
615  * needs to be, and then take action to advance the state machine. Here's how we
616  * have to react to each bit:
617  *
618  *  - Byte Done: This indicates a byte has been transferred when we're not in
619  *    the 32 byte buffer mode. At this time, we either write the next byte or
620  *    read the next byte out of the buffer.
621  *  - Alert: This shouldn't be generated, so we generally ignore it, but clear
622  *    it just for completeness.
623  *  - Fail, Bus Error, Device Error: The transaction is over. We need to guess
624  *    the best error that we can with the unfortunately limited information that
625  *    we get.
626  *  - Interrupt: This indicates that the entire command was completed and is the
627  *    only thing that we should use to signal successful completion.
628  */
629 static bool
pchsmbus_io(pchsmbus_t * pch,pch_smbus_sts_t status)630 pchsmbus_io(pchsmbus_t *pch, pch_smbus_sts_t status)
631 {
632 	ASSERT(MUTEX_HELD(&pch->ps_mutex));
633 
634 	/*
635 	 * Is there actually activity for us to process or not. If not, then
636 	 * we're done. Mask off bits like In Use and related. Clear them now and
637 	 * proceed to process them all in turn.
638 	 */
639 	status &= PCH_HSTS_CLEAR_PRE;
640 	if (status == 0) {
641 		return (false);
642 	}
643 
644 	if ((status & PCH_HSTS_ERRORS) != 0) {
645 		pchsmbus_io_error(pch, status);
646 		goto done;
647 	}
648 
649 	if ((status & PCH_HSTS_BYTE_DONE) != 0) {
650 		pchsmbus_io_byte_done(pch);
651 	}
652 
653 	if ((status & PCH_HSTS_INTR) != 0) {
654 		pchsmbus_io_req_done(pch);
655 	}
656 
657 done:
658 	/*
659 	 * We clear the status codes last as when operating in byte at a time
660 	 * mode, the data must be read and written prior to clearing this status
661 	 * to indicate that we are done.
662 	 */
663 	pchsmbus_write8(pch, PCH_R_BAR_HSTS, status);
664 	return (true);
665 }
666 
667 static uint_t
pchsmbus_intr(caddr_t arg1,caddr_t arg2)668 pchsmbus_intr(caddr_t arg1, caddr_t arg2)
669 {
670 	pchsmbus_t *pch = (pchsmbus_t *)arg1;
671 	pch_smbus_sts_t sts;
672 
673 	mutex_enter(&pch->ps_mutex);
674 	sts = pchsmbus_read8(pch, PCH_R_BAR_HSTS);
675 	if (!pchsmbus_io(pch, sts)) {
676 		mutex_exit(&pch->ps_mutex);
677 		return (DDI_INTR_UNCLAIMED);
678 	}
679 
680 	if (pch->ps_req_done) {
681 		cv_signal(&pch->ps_cv);
682 	}
683 	mutex_exit(&pch->ps_mutex);
684 	return (DDI_INTR_CLAIMED);
685 }
686 
687 static void
pchsmbus_wait(pchsmbus_t * pch,bool poll)688 pchsmbus_wait(pchsmbus_t *pch, bool poll)
689 {
690 	uint32_t to, spin;
691 
692 	VERIFY(MUTEX_HELD(&pch->ps_mutex));
693 	VERIFY3P(pch->ps_req, !=, NULL);
694 
695 	to = i2c_ctrl_timeout_delay_us(pch->ps_hdl, I2C_CTRL_TO_IO);
696 	spin = i2c_ctrl_timeout_delay_us(pch->ps_hdl, I2C_CTRL_TO_POLL_CTRL);
697 
698 	if (!poll) {
699 		clock_t abs = ddi_get_lbolt() + drv_usectohz(to);
700 		while (!pch->ps_req_done) {
701 			clock_t ret = cv_timedwait(&pch->ps_cv, &pch->ps_mutex,
702 			    abs);
703 			if (ret == -1) {
704 				break;
705 			}
706 		}
707 	} else {
708 		hrtime_t abs = gethrtime() + USEC2NSEC(to);
709 
710 		while (!pch->ps_req_done && gethrtime() < abs) {
711 			drv_usecwait(spin);
712 			uint8_t status = pchsmbus_read8(pch, PCH_R_BAR_HSTS);
713 			(void) pchsmbus_io(pch, status);
714 		}
715 	}
716 
717 	/*
718 	 * If this is not done, we're going to set the kill bit. The next user
719 	 * will be the one that waits for the kill to actually complete with the
720 	 * normal call to pchsmbus_bus_avail(). The FAIL status in the HSTS
721 	 * register will get cleared before the next transaction begins and the
722 	 * HCTL KILL bit will be cleared when we issue the next command.
723 	 */
724 	if (!pch->ps_req_done) {
725 		uint8_t val = PCH_R_HCTL_SET_KILL(0, 1);
726 		pchsmbus_write8(pch, PCH_R_BAR_HCTL, val);
727 		i2c_ctrl_io_error(&pch->ps_req->smbr_error,
728 		    I2C_CORE_E_CONTROLLER, I2C_CTRL_E_REQ_TO);
729 		pch->ps_req_done = true;
730 	}
731 }
732 
733 static void
pchsmbus_io_smbus(void * arg,uint32_t port,smbus_req_t * req)734 pchsmbus_io_smbus(void *arg, uint32_t port, smbus_req_t *req)
735 {
736 	bool poll;
737 	pchsmbus_t *pch = arg;
738 
739 	ASSERT3U(port, ==, 0);
740 
741 	mutex_enter(&pch->ps_mutex);
742 	if (!pchsmbus_bus_avail(pch)) {
743 		mutex_exit(&pch->ps_mutex);
744 		i2c_ctrl_io_error(&req->smbr_error, I2C_CORE_E_CONTROLLER,
745 		    I2C_CTRL_E_BUS_BUSY);
746 		return;
747 	}
748 
749 	ASSERT3P(pch->ps_req, ==, NULL);
750 	pch->ps_req = req;
751 	pch->ps_req_off = 0;
752 	pch->ps_req_done = false;
753 
754 	/*
755 	 * Determine whether or not we should use interrupts or poll for
756 	 * completion. We may have been asked to poll explicitly. We may also
757 	 * not have interrupt support.
758 	 */
759 	poll = (req->smbr_flags & I2C_IO_REQ_F_POLL) != 0;
760 	if (pch->ps_nintrs == 0)
761 		poll = true;
762 
763 	switch (req->smbr_op) {
764 	case SMBUS_OP_QUICK_COMMAND:
765 		pchsmbus_set_addr(pch, req, (req->smbr_flags &
766 		    I2C_IO_REQ_F_QUICK_WRITE) != 0);
767 		break;
768 	case SMBUS_OP_SEND_BYTE:
769 		pchsmbus_set_addr(pch, req, true);
770 		pchsmbus_write8(pch, PCH_R_BAR_HCMD, req->smbr_wdata[0]);
771 		break;
772 	case SMBUS_OP_WRITE_BYTE:
773 		pchsmbus_set_addr(pch, req, true);
774 		pchsmbus_write8(pch, PCH_R_BAR_HCMD, req->smbr_cmd);
775 		pchsmbus_write8(pch, PCH_R_BAR_HD0, req->smbr_wdata[0]);
776 		break;
777 	case SMBUS_OP_WRITE_WORD:
778 	case SMBUS_OP_PROCESS_CALL:
779 		pchsmbus_set_addr(pch, req, true);
780 		pchsmbus_write8(pch, PCH_R_BAR_HCMD, req->smbr_cmd);
781 		pchsmbus_write8(pch, PCH_R_BAR_HD0, req->smbr_wdata[0]);
782 		pchsmbus_write8(pch, PCH_R_BAR_HD1, req->smbr_wdata[1]);
783 		break;
784 	case SMBUS_OP_RECV_BYTE:
785 		pchsmbus_set_addr(pch, req, false);
786 		break;
787 	case SMBUS_OP_READ_BYTE:
788 	case SMBUS_OP_READ_WORD:
789 		pchsmbus_set_addr(pch, req, false);
790 		pchsmbus_write8(pch, PCH_R_BAR_HCMD, req->smbr_cmd);
791 		break;
792 	case SMBUS_OP_WRITE_BLOCK:
793 	case SMBUS_OP_BLOCK_PROCESS_CALL:
794 	case SMBUS_OP_READ_BLOCK:
795 	case SMBUS_OP_I2C_WRITE_BLOCK:
796 	case SMBUS_OP_I2C_READ_BLOCK:
797 		pchsmbus_io_init_block(pch, req);
798 		break;
799 	case SMBUS_OP_HOST_NOTIFY:
800 	case SMBUS_OP_WRITE_U32:
801 	case SMBUS_OP_READ_U32:
802 	case SMBUS_OP_WRITE_U64:
803 	case SMBUS_OP_READ_U64:
804 	default:
805 		dev_err(pch->ps_dip, CE_WARN, "!framework passed unsupported "
806 		    "SMBus command 0x%x", req->smbr_op);
807 		i2c_ctrl_io_error(&req->smbr_error, I2C_CORE_E_CONTROLLER,
808 		    I2C_CTRL_E_UNSUP_CMD);
809 		goto done;
810 	}
811 
812 	/*
813 	 * Prepare to issue the command. We do this in a few different steps:
814 	 *
815 	 * 1) We set up command-specific parameters such as I2C enable. If the
816 	 *    block enable is present, then it will have been already enabled.
817 	 * 2) Clear all interrupts.
818 	 * 3) Actually begin the transaction, indicating whether or not
819 	 *    interrupts should occur.
820 	 * 4) Poll or wait for completion.
821 	 */
822 	pchsmbus_write8(pch, PCH_R_BAR_HSTS, PCH_HSTS_CLEAR_PRE);
823 	pch_smbus_cmd_t cmd = pchsmbus_req_to_cmd(req);
824 	uint8_t ctl = PCH_R_HCTL_SET_CMD(0, cmd);
825 	ctl = PCH_R_HCTL_SET_START(ctl, 1);
826 	ctl = PCH_R_HCTL_SET_INT_EN(ctl, !poll);
827 	pchsmbus_write8(pch, PCH_R_BAR_HCTL, ctl);
828 	pch->ps_req_hctl = ctl;
829 
830 	pchsmbus_wait(pch, poll);
831 
832 done:
833 	/*
834 	 * Now that this operation has completed, whether successful or not,
835 	 * restore the host configuration and block enable to our defaults.
836 	 */
837 	if ((pch->ps_init & PCHSMBUS_RUN_I2C_EN) != 0) {
838 		uint32_t val = pci_config_get32(pch->ps_cfg, PCH_R_PCIE_HCFG);
839 		val = PCH_R_HCFG_SET_I2CEN(val, 0);
840 		pci_config_put32(pch->ps_cfg, PCH_R_PCIE_HCFG, val);
841 		pch->ps_init &= ~PCHSMBUS_RUN_I2C_EN;
842 	}
843 
844 	if ((pch->ps_init & PCHSMBUS_RUN_BUF_EN) != 0) {
845 		uint8_t val = pchsmbus_read8(pch, PCH_R_BAR_AUXC);
846 		val = PCH_R_AUXC_SET_E32B(val, 0);
847 		pchsmbus_write8(pch, PCH_R_BAR_AUXC, val);
848 		pch->ps_init &= ~PCHSMBUS_RUN_BUF_EN;
849 	}
850 
851 	pch->ps_req = NULL;
852 	pch->ps_req_off = 0;
853 	pch->ps_req_hctl = 0;
854 	pch->ps_req_done = false;
855 	pch->ps_kill_err = I2C_CTRL_E_OK;
856 	mutex_exit(&pch->ps_mutex);
857 }
858 
859 static const i2c_ctrl_ops_t pchsmbus_ctrl_ops = {
860 	.i2c_port_name_f = i2c_ctrl_port_name_portno,
861 	.i2c_io_smbus_f = pchsmbus_io_smbus,
862 	.i2c_prop_info_f = pchsmbus_prop_info,
863 	.i2c_prop_get_f = pchsmbus_prop_get
864 };
865 
866 static bool
pchsmbus_supported(pchsmbus_t * pch)867 pchsmbus_supported(pchsmbus_t *pch)
868 {
869 	uint16_t id = pci_config_get16(pch->ps_cfg, PCI_CONF_VENID);
870 
871 	if (id != PCH_SMBUS_VID_INTEL) {
872 		dev_err(pch->ps_dip, CE_WARN, "found unsupported non-Intel "
873 		    "vendor ID: 0x%x", id);
874 		return (false);
875 	}
876 
877 	id = pci_config_get16(pch->ps_cfg, PCI_CONF_DEVID);
878 	for (size_t i = 0; i < ARRAY_SIZE(pchsmbus_feats); i++) {
879 		if (id != pchsmbus_feats[i].pcm_did)
880 			continue;
881 
882 		pch->ps_feats = pchsmbus_feats[i].pcm_feat;
883 		return (true);
884 	}
885 
886 	dev_err(pch->ps_dip, CE_WARN, "found unsupported device ID: 0x%x", id);
887 	return (false);
888 }
889 
890 static bool
pchsmbus_setup_regs(pchsmbus_t * pch)891 pchsmbus_setup_regs(pchsmbus_t *pch)
892 {
893 	int ret;
894 	ddi_device_acc_attr_t attr;
895 
896 	bzero(&attr, sizeof (attr));
897 	attr.devacc_attr_version = DDI_DEVICE_ATTR_V1;
898 	attr.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC;
899 	attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
900 	attr.devacc_attr_access = DDI_DEFAULT_ACC;
901 
902 	if (ddi_dev_regsize(pch->ps_dip, PCHSMBUS_REGNO, &pch->ps_regsize) !=
903 	    DDI_SUCCESS) {
904 		dev_err(pch->ps_dip, CE_WARN, "failed to get regs[%u] size",
905 		    PCHSMBUS_REGNO);
906 		return (false);
907 	}
908 
909 	ret = ddi_regs_map_setup(pch->ps_dip, PCHSMBUS_REGNO, &pch->ps_base,
910 	    0, pch->ps_regsize, &attr, &pch->ps_regs);
911 	if (ret != DDI_SUCCESS) {
912 		dev_err(pch->ps_dip, CE_WARN, "failed to map regs[%u]: %u",
913 		    PCHSMBUS_REGNO, ret);
914 		return (false);
915 	}
916 
917 	pch->ps_init |= PCHSMBUS_INIT_REGS;
918 	return (true);
919 }
920 
921 /*
922  * Go ahead and set up interrupts. It is possible that we don't get access to
923  * interrupts because firmware has enabled it to be delivered via a #SMI. If
924  * that's the case, we will just always rely on polling. Because that is the
925  * nature of this hardware, we treat the failure to detect interrupts as
926  * non-fatal, but if we find one and cannot actually set it up, we will fail
927  * then.
928  */
929 static bool
pchsmbus_setup_intr(pchsmbus_t * pch)930 pchsmbus_setup_intr(pchsmbus_t *pch)
931 {
932 	int types, ret;
933 
934 	pch->ps_nintrs = 0;
935 	pch->ps_intr_pri = 0;
936 
937 	/*
938 	 * If the SMI enable flag is set, that means that firmware is on the
939 	 * scene and we will just need to poll for completion rather than
940 	 * assuming we get interrupts. That's fine. Why it means that they won't
941 	 * fight us for the controller despite that is a deeper mystery.
942 	 */
943 	if (PCH_R_HCFG_GET_SMI_EN(pch->ps_init_hcfg) != 0) {
944 		dev_err(pch->ps_dip, CE_WARN, "!firmware has taken our "
945 		    "interrupt for itself via #SMI; limiting to polling");
946 		return (true);
947 	}
948 
949 	ret = ddi_intr_get_supported_types(pch->ps_dip, &types);
950 	if (ret != DDI_SUCCESS) {
951 		dev_err(pch->ps_dip, CE_WARN, "failed to get supported "
952 		    "interrupt types: 0x%x; limiting to polling", ret);
953 		return (true);
954 	}
955 
956 	/*
957 	 * Hardware only supports INTx style fixed interrupts. That hasn't
958 	 * changed in 25 years of hardware. If we don't find a fixed interrupt
959 	 * that's that.
960 	 */
961 	if ((types & DDI_INTR_TYPE_FIXED) == 0) {
962 		dev_err(pch->ps_dip, CE_WARN, "missing support for fixed "
963 		    "interrupts: found 0x%x; limiting to polling", types);
964 		return (true);
965 	}
966 
967 	ret = ddi_intr_alloc(pch->ps_dip, &pch->ps_intr_hdl,
968 	    DDI_INTR_TYPE_FIXED, 0, 1, &pch->ps_nintrs, DDI_INTR_ALLOC_STRICT);
969 	if (ret != DDI_SUCCESS) {
970 		dev_err(pch->ps_dip, CE_WARN, "failed to allocate "
971 		    "interrupts: 0x%x", ret);
972 		return (false);
973 	}
974 	pch->ps_init |= PCHSMBUS_INIT_INTR_ALLOC;
975 
976 	ret = ddi_intr_add_handler(pch->ps_intr_hdl, pchsmbus_intr, pch, NULL);
977 	if (ret != DDI_SUCCESS) {
978 		dev_err(pch->ps_dip, CE_WARN, "failed to add interrupt "
979 		    "handler: 0x%x", ret);
980 		return (false);
981 	}
982 	pch->ps_init |= PCHSMBUS_INIT_INTR_HDL;
983 
984 	ret = ddi_intr_get_pri(pch->ps_intr_hdl, &pch->ps_intr_pri);
985 	if (ret != DDI_SUCCESS) {
986 		dev_err(pch->ps_dip, CE_WARN, "failed to get interrupt "
987 		    "priority");
988 		return (false);
989 	}
990 
991 	return (true);
992 }
993 
994 /*
995  * Go through and set up the controller for general use. In particular, there
996  * are a few things that we go through and make sure are set in a way that makes
997  * sense for us:
998  *
999  *  - We always disable automatic PEC. The Auxiliary 32 byte buffer control will
1000  *    be enabled when it can be used.
1001  *  - We disable any events that can be generated by the target.
1002  *  - We make sure that SMBus timing is enabled by default.
1003  *  - Ensure that interrupts are disabled and that the PEC feature is not set.
1004  *    Interrupts will be enabled when we actually enable commands.
1005  *  - We actually enable the controller.
1006  */
1007 static void
pchsmbus_ctrl_init(pchsmbus_t * pch)1008 pchsmbus_ctrl_init(pchsmbus_t *pch)
1009 {
1010 	if ((pch->ps_feats & PCH_SMBUS_FEAT_HW_PEC) != 0) {
1011 		uint8_t val = pchsmbus_read8(pch, PCH_R_BAR_AUXC);
1012 		val = PCH_R_AUXC_SET_AAC(val, 0);
1013 		pchsmbus_write8(pch, PCH_R_BAR_AUXC, val);
1014 	}
1015 
1016 	if ((pch->ps_feats & PCH_SMBUS_FEAT_TARG_NOTIFY) != 0) {
1017 		pch->ps_init_scmd = pchsmbus_read8(pch, PCH_R_BAR_SCMD);
1018 
1019 		uint8_t val = PCH_R_SCMD_SET_SMB_D(pch->ps_init_scmd, 1);
1020 		val = PCH_R_SCMD_SET_HNI(val, 0);
1021 		pchsmbus_write8(pch, PCH_R_BAR_SCMD, 0);
1022 	}
1023 
1024 	/*
1025 	 * Save the initial control register to restore later. However, don't
1026 	 * save the kill bit which stops transactions. At this point, make sure
1027 	 * interrupts and related activity are all disabled.
1028 	 */
1029 	pch->ps_init_hctl = pchsmbus_read8(pch, PCH_R_BAR_HCTL);
1030 	pch->ps_init_hctl = PCH_R_HCTL_SET_KILL(pch->ps_init_hctl, 0);
1031 	pchsmbus_write8(pch, PCH_R_BAR_HCTL, 0);
1032 
1033 	uint32_t val = pch->ps_init_hcfg;
1034 	val = PCH_R_HCFG_SET_EN(val, 1);
1035 	val = PCH_R_HCFG_SET_I2CEN(val, PCH_R_HCFG_I2CEN_SMBUS);
1036 	pci_config_put32(pch->ps_cfg, PCH_R_PCIE_HCFG, val);
1037 
1038 	pch->ps_init |= PCHSMBUS_INIT_CTRL;
1039 }
1040 
1041 static bool
pchsmbus_enable_intr(pchsmbus_t * pch)1042 pchsmbus_enable_intr(pchsmbus_t *pch)
1043 {
1044 	int ret = ddi_intr_enable(pch->ps_intr_hdl);
1045 	if (ret != DDI_SUCCESS) {
1046 		dev_err(pch->ps_dip, CE_WARN, "failed to enable interrupt "
1047 		    "handler: %d", ret);
1048 		return (false);
1049 	}
1050 
1051 	pch->ps_init |= PCHSMBUS_INIT_INTR_EN;
1052 	return (true);
1053 }
1054 
1055 static bool
pchsmbus_register(pchsmbus_t * pch)1056 pchsmbus_register(pchsmbus_t *pch)
1057 {
1058 	i2c_ctrl_reg_error_t ret;
1059 	i2c_ctrl_register_t *reg;
1060 
1061 	ret = i2c_ctrl_register_alloc(I2C_CTRL_PROVIDER, &reg);
1062 	if (ret != 0) {
1063 		dev_err(pch->ps_dip, CE_WARN, "failed to allocate i2c "
1064 		    "controller registration structure: 0x%x", ret);
1065 		return (false);
1066 	}
1067 
1068 	reg->ic_type = I2C_CTRL_TYPE_SMBUS;
1069 	reg->ic_nports = 1;
1070 	reg->ic_dip = pch->ps_dip;
1071 	reg->ic_drv = pch;
1072 	reg->ic_ops = &pchsmbus_ctrl_ops;
1073 
1074 	ret = i2c_ctrl_register(reg, &pch->ps_hdl);
1075 	i2c_ctrl_register_free(reg);
1076 	if (ret != 0) {
1077 		dev_err(pch->ps_dip, CE_WARN, "failed to register with i2c "
1078 		    "framework: 0x%x", ret);
1079 		return (false);
1080 	}
1081 
1082 	pch->ps_init |= PCHSMBUS_INIT_I2C;
1083 	return (true);
1084 }
1085 
1086 static void
pchsmbus_cleanup(pchsmbus_t * pch)1087 pchsmbus_cleanup(pchsmbus_t *pch)
1088 {
1089 	if ((pch->ps_init & PCHSMBUS_INIT_INTR_EN) != 0) {
1090 		/*
1091 		 * If this fails while tearing down, there isn't much we can do.
1092 		 */
1093 		int ret = ddi_intr_disable(pch->ps_intr_hdl);
1094 		if (ret != DDI_SUCCESS) {
1095 			dev_err(pch->ps_dip, CE_WARN, "failed to disable "
1096 			    "interrupt handler: %d", ret);
1097 		}
1098 		pch->ps_init &= ~PCHSMBUS_INIT_INTR_EN;
1099 	}
1100 
1101 	/*
1102 	 * We restore several of the controllers original values as the BIOS may
1103 	 * use this device and can rely on it.
1104 	 */
1105 	if ((pch->ps_init & PCHSMBUS_INIT_CTRL) != 0) {
1106 		if ((pch->ps_feats & PCH_SMBUS_FEAT_TARG_NOTIFY) != 0) {
1107 			pchsmbus_write8(pch, PCH_R_BAR_SCMD, pch->ps_init_scmd);
1108 		}
1109 
1110 		pchsmbus_write8(pch, PCH_R_BAR_HCTL, pch->ps_init_hctl);
1111 		pci_config_put32(pch->ps_cfg, PCH_R_PCIE_HCFG,
1112 		    pch->ps_init_hcfg);
1113 		pch->ps_init &= ~PCHSMBUS_INIT_CTRL;
1114 	}
1115 
1116 	if ((pch->ps_init & PCHSMBUS_INIT_SYNC) != 0) {
1117 		cv_destroy(&pch->ps_cv);
1118 		mutex_destroy(&pch->ps_mutex);
1119 		pch->ps_init &= ~PCHSMBUS_INIT_SYNC;
1120 	}
1121 
1122 	if ((pch->ps_init & PCHSMBUS_INIT_INTR_HDL) != 0) {
1123 		int ret = ddi_intr_remove_handler(pch->ps_intr_hdl);
1124 		if (ret != 0) {
1125 			dev_err(pch->ps_dip, CE_WARN, "failed to remove "
1126 			    "interrupt handler: 0x%x", ret);
1127 		}
1128 		pch->ps_init &= ~PCHSMBUS_INIT_INTR_HDL;
1129 	}
1130 
1131 	if ((pch->ps_init & PCHSMBUS_INIT_INTR_ALLOC) != 0) {
1132 		int ret = ddi_intr_free(pch->ps_intr_hdl);
1133 		if (ret != DDI_SUCCESS) {
1134 			dev_err(pch->ps_dip, CE_WARN, "failed to free "
1135 			    "device interrupt: 0x%x", ret);
1136 		}
1137 		pch->ps_init &= ~PCHSMBUS_INIT_INTR_ALLOC;
1138 	}
1139 
1140 	if ((pch->ps_init & PCHSMBUS_INIT_REGS) != 0) {
1141 		ddi_regs_map_free(&pch->ps_regs);
1142 		pch->ps_base = NULL;
1143 		pch->ps_regsize = 0;
1144 		pch->ps_init &= ~PCHSMBUS_INIT_REGS;
1145 	}
1146 
1147 	if ((pch->ps_init & PCHSMBUS_INIT_PCI) != 0) {
1148 		pci_config_teardown(&pch->ps_cfg);
1149 		pch->ps_cfg = NULL;
1150 		pch->ps_init &= ~PCHSMBUS_INIT_PCI;
1151 	}
1152 
1153 	ASSERT0(pch->ps_init);
1154 	ddi_set_driver_private(pch->ps_dip, NULL);
1155 	kmem_free(pch, sizeof (pchsmbus_t));
1156 }
1157 
1158 int
pchsmbus_attach(dev_info_t * dip,ddi_attach_cmd_t cmd)1159 pchsmbus_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
1160 {
1161 	pchsmbus_t *pch;
1162 
1163 	switch (cmd) {
1164 	case DDI_ATTACH:
1165 		break;
1166 	case DDI_RESUME:
1167 	default:
1168 		return (DDI_FAILURE);
1169 	}
1170 
1171 	pch = kmem_zalloc(sizeof (pchsmbus_t), KM_SLEEP);
1172 	pch->ps_dip = dip;
1173 	ddi_set_driver_private(dip, pch);
1174 
1175 	if (pci_config_setup(dip, &pch->ps_cfg) != DDI_SUCCESS) {
1176 		dev_err(dip, CE_WARN, "failed to set up config space");
1177 		goto cleanup;
1178 	}
1179 	pch->ps_init |= PCHSMBUS_INIT_PCI;
1180 
1181 	if (!pchsmbus_supported(pch))
1182 		goto cleanup;
1183 
1184 	if (!pchsmbus_setup_regs(pch))
1185 		goto cleanup;
1186 
1187 	/*
1188 	 * Snapshot the original value of the host configuration register. This
1189 	 * is something that some systems will restore on detach as sometimes
1190 	 * firmware uses this controller. In addition, we need this to determine
1191 	 * if we have interrupts available to us.
1192 	 */
1193 	pch->ps_init_hcfg = pci_config_get32(pch->ps_cfg, PCH_R_PCIE_HCFG);
1194 
1195 	if (!pchsmbus_setup_intr(pch))
1196 		goto cleanup;
1197 
1198 	/*
1199 	 * Now that we (potentially) have our interrupt. Go ahead and get our
1200 	 * intrrupt and CV. If we don't have an interrupt this'll turn into a
1201 	 * NULL.
1202 	 */
1203 	mutex_init(&pch->ps_mutex, NULL, MUTEX_DRIVER,
1204 	    DDI_INTR_PRI(pch->ps_intr_pri));
1205 	cv_init(&pch->ps_cv, NULL, CV_DRIVER, NULL);
1206 	pch->ps_init |= PCHSMBUS_INIT_SYNC;
1207 
1208 	pchsmbus_ctrl_init(pch);
1209 
1210 	if (!pchsmbus_enable_intr(pch))
1211 		goto cleanup;
1212 
1213 	if (!pchsmbus_register(pch))
1214 		goto cleanup;
1215 
1216 	return (DDI_SUCCESS);
1217 
1218 cleanup:
1219 	pchsmbus_cleanup(pch);
1220 	return (DDI_FAILURE);
1221 }
1222 
1223 int
pchsmbus_detach(dev_info_t * dip,ddi_detach_cmd_t cmd)1224 pchsmbus_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
1225 {
1226 	pchsmbus_t *pch;
1227 
1228 	switch (cmd) {
1229 	case DDI_DETACH:
1230 		break;
1231 	case DDI_SUSPEND:
1232 	default:
1233 		return (DDI_FAILURE);
1234 	}
1235 
1236 	pch = ddi_get_driver_private(dip);
1237 	if (pch == NULL) {
1238 		dev_err(dip, CE_WARN, "asked to detach, but missing private "
1239 		    "data");
1240 		return (DDI_FAILURE);
1241 	}
1242 
1243 	VERIFY3P(pch->ps_dip, ==, dip);
1244 	i2c_ctrl_reg_error_t ret = i2c_ctrl_unregister(pch->ps_hdl);
1245 	if (ret != 0) {
1246 		dev_err(dip, CE_WARN, "failed to unregister from i2c "
1247 		    "framework 0x%x", ret);
1248 		return (DDI_FAILURE);
1249 	}
1250 	pch->ps_init &= ~PCHSMBUS_INIT_I2C;
1251 	pchsmbus_cleanup(pch);
1252 
1253 	return (DDI_SUCCESS);
1254 }
1255 
1256 static struct dev_ops pchsmbus_dev_ops = {
1257 	.devo_rev = DEVO_REV,
1258 	.devo_refcnt = 0,
1259 	.devo_identify = nulldev,
1260 	.devo_probe = nulldev,
1261 	.devo_attach = pchsmbus_attach,
1262 	.devo_detach = pchsmbus_detach,
1263 	.devo_reset = nodev,
1264 	.devo_quiesce = ddi_quiesce_not_supported,
1265 };
1266 
1267 static struct modldrv pchsmbus_modldrv = {
1268 	.drv_modops = &mod_driverops,
1269 	.drv_linkinfo = "Intel ICH/PCH SMBus Controller",
1270 	.drv_dev_ops = &pchsmbus_dev_ops
1271 };
1272 
1273 static struct modlinkage pchsmbus_modlinkage = {
1274 	.ml_rev = MODREV_1,
1275 	.ml_linkage = { &pchsmbus_modldrv, NULL }
1276 };
1277 
1278 int
_init(void)1279 _init(void)
1280 {
1281 	int ret;
1282 
1283 	i2c_ctrl_mod_init(&pchsmbus_dev_ops);
1284 	if ((ret = mod_install(&pchsmbus_modlinkage)) != 0) {
1285 		i2c_ctrl_mod_fini(&pchsmbus_dev_ops);
1286 	}
1287 
1288 	return (ret);
1289 }
1290 
1291 int
_info(struct modinfo * modinfop)1292 _info(struct modinfo *modinfop)
1293 {
1294 	return (mod_info(&pchsmbus_modlinkage, modinfop));
1295 }
1296 
1297 int
_fini(void)1298 _fini(void)
1299 {
1300 	int ret;
1301 
1302 	if ((ret = mod_remove(&pchsmbus_modlinkage)) == 0) {
1303 		i2c_ctrl_mod_fini(&pchsmbus_dev_ops);
1304 	}
1305 
1306 	return (ret);
1307 }
1308