xref: /freebsd/sys/dev/imcsmb/imcsmb.c (revision b64c5a0ace59af62eff52bfe110a521dc73c937b)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Authors: Joe Kloss; Ravi Pokala (rpokala@freebsd.org)
5  *
6  * Copyright (c) 2017-2018 Panasas
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 /* A detailed description of this device is present in imcsmb_pci.c */
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/endian.h>
37 #include <sys/errno.h>
38 #include <sys/lock.h>
39 #include <sys/mutex.h>
40 #include <sys/syslog.h>
41 #include <sys/bus.h>
42 
43 #include <machine/bus.h>
44 #include <machine/atomic.h>
45 
46 #include <dev/pci/pcivar.h>
47 #include <dev/pci/pcireg.h>
48 
49 #include <dev/smbus/smbconf.h>
50 
51 #include "imcsmb_reg.h"
52 #include "imcsmb_var.h"
53 
54 /* Device methods */
55 static int imcsmb_attach(device_t dev);
56 static int imcsmb_detach(device_t dev);
57 static int imcsmb_probe(device_t dev);
58 
59 /* SMBus methods */
60 static int imcsmb_callback(device_t dev, int index, void *data);
61 static int imcsmb_readb(device_t dev, u_char slave, char cmd, char *byte);
62 static int imcsmb_readw(device_t dev, u_char slave, char cmd, short *word);
63 static int imcsmb_writeb(device_t dev, u_char slave, char cmd, char byte);
64 static int imcsmb_writew(device_t dev, u_char slave, char cmd, short word);
65 
66 /* All the read/write methods wrap around this. */
67 static int imcsmb_transfer(device_t dev, u_char slave, char cmd, void *data,
68     int word_op, int write_op);
69 
70 /**
71  * device_attach() method. Set up the softc, including getting the set of the
72  * parent imcsmb_pci's registers that we will use. Create the smbus(4) device,
73  * which any SMBus slave device drivers will connect to.
74  *
75  * @author rpokala
76  *
77  * @param[in,out] dev
78  *      Device being attached.
79  */
80 static int
81 imcsmb_attach(device_t dev)
82 {
83 	struct imcsmb_softc *sc;
84 	int rc;
85 
86 	/* Initialize private state */
87 	sc = device_get_softc(dev);
88 	sc->dev = dev;
89 	sc->imcsmb_pci = device_get_parent(dev);
90 	sc->regs = device_get_ivars(dev);
91 
92 	/* Create the smbus child */
93 	sc->smbus = device_add_child(dev, "smbus", DEVICE_UNIT_ANY);
94 	if (sc->smbus == NULL) {
95 		/* Nothing has been allocated, so there's no cleanup. */
96 		device_printf(dev, "Child smbus not added\n");
97 		rc = ENXIO;
98 		goto out;
99 	}
100 
101 	/* Attach the smbus child. */
102 	bus_attach_children(dev);
103 	rc = 0;
104 
105 out:
106 	return (rc);
107 }
108 
109 /**
110  * device_detach() method. attach() didn't do any allocations, so all that's
111  * needed here is to free up any downstream drivers and children.
112  *
113  * @author Joe Kloss
114  *
115  * @param[in] dev
116  *      Device being detached.
117  */
118 static int
119 imcsmb_detach(device_t dev)
120 {
121 	int rc;
122 
123 	/* Detach any attached drivers */
124 	rc = bus_generic_detach(dev);
125 	if (rc == 0) {
126 		/* Remove all children */
127 		rc = device_delete_children(dev);
128 	}
129 
130 	return (rc);
131 }
132 
133 /**
134  * device_probe() method. All the actual probing was done by the imcsmb_pci
135  * parent, so just report success.
136  *
137  * @author Joe Kloss
138  *
139  * @param[in,out] dev
140  *      Device being probed.
141  */
142 static int
143 imcsmb_probe(device_t dev)
144 {
145 
146 	device_set_desc(dev, "iMC SMBus controller");
147 	return (BUS_PROBE_DEFAULT);
148 }
149 
150 /**
151  * smbus_callback() method. Call the parent imcsmb_pci's request or release
152  * function to quiesce / restart firmware tasks which might use the SMBus.
153  *
154  * @author rpokala
155  *
156  * @param[in] dev
157  *      Device being requested or released.
158  *
159  * @param[in] index
160  *      Either SMB_REQUEST_BUS or SMB_RELEASE_BUS.
161  *
162  * @param[in] data
163  *      Tell's the rest of the SMBus subsystem to allow or disallow waiting;
164  *      this driver only works with SMB_DONTWAIT.
165  */
166 static int
167 imcsmb_callback(device_t dev, int index, void *data)
168 {
169 	struct imcsmb_softc *sc;
170 	int *how;
171 	int rc;
172 
173 	sc = device_get_softc(dev);
174 	how = (int *) data;
175 
176 	switch (index) {
177 	case SMB_REQUEST_BUS: {
178 		if (*how != SMB_DONTWAIT) {
179 			rc = EINVAL;
180 			goto out;
181 		}
182 		rc = imcsmb_pci_request_bus(sc->imcsmb_pci);
183 		break;
184 	}
185 	case SMB_RELEASE_BUS:
186 		imcsmb_pci_release_bus(sc->imcsmb_pci);
187 		rc = 0;
188 		break;
189 	default:
190 		rc = EINVAL;
191 		break;
192 	}
193 
194 out:
195 	return (rc);
196 }
197 
198 /**
199  * smbus_readb() method. Thin wrapper around imcsmb_transfer().
200  *
201  * @author Joe Kloss
202  *
203  * @param[in] dev
204  *
205  * @param[in] slave
206  *      The SMBus address of the target device.
207  *
208  * @param[in] cmd
209  *      The SMBus command for the target device; this is the offset for SPDs,
210  *      or the register number for TSODs.
211  *
212  * @param[out] byte
213  *      The byte which was read.
214  */
215 static int
216 imcsmb_readb(device_t dev, u_char slave, char cmd, char *byte)
217 {
218 
219 	return (imcsmb_transfer(dev, slave, cmd, byte, FALSE, FALSE));
220 }
221 
222 /**
223  * smbus_readw() method. Thin wrapper around imcsmb_transfer().
224  *
225  * @author Joe Kloss
226  *
227  * @param[in] dev
228  *
229  * @param[in] slave
230  *      The SMBus address of the target device.
231  *
232  * @param[in] cmd
233  *      The SMBus command for the target device; this is the offset for SPDs,
234  *      or the register number for TSODs.
235  *
236  * @param[out] word
237  *      The word which was read.
238  */
239 static int
240 imcsmb_readw(device_t dev, u_char slave, char cmd, short *word)
241 {
242 
243 	return (imcsmb_transfer(dev, slave, cmd, word, TRUE, FALSE));
244 }
245 
246 /**
247  * smbus_writeb() method. Thin wrapper around imcsmb_transfer().
248  *
249  * @author Joe Kloss
250  *
251  * @param[in] dev
252  *
253  * @param[in] slave
254  *      The SMBus address of the target device.
255  *
256  * @param[in] cmd
257  *      The SMBus command for the target device; this is the offset for SPDs,
258  *      or the register number for TSODs.
259  *
260  * @param[in] byte
261  *      The byte to write.
262  */
263 static int
264 imcsmb_writeb(device_t dev, u_char slave, char cmd, char byte)
265 {
266 
267 	return (imcsmb_transfer(dev, slave, cmd, &byte, FALSE, TRUE));
268 }
269 
270 /**
271  * smbus_writew() method. Thin wrapper around imcsmb_transfer().
272  *
273  * @author Joe Kloss
274  *
275  * @param[in] dev
276  *
277  * @param[in] slave
278  *      The SMBus address of the target device.
279  *
280  * @param[in] cmd
281  *      The SMBus command for the target device; this is the offset for SPDs,
282  *      or the register number for TSODs.
283  *
284  * @param[in] word
285  *      The word to write.
286  */
287 static int
288 imcsmb_writew(device_t dev, u_char slave, char cmd, short word)
289 {
290 
291 	return (imcsmb_transfer(dev, slave, cmd, &word, TRUE, TRUE));
292 }
293 
294 /**
295  * Manipulate the PCI control registers to read data from or write data to the
296  * SMBus controller.
297  *
298  * @author Joe Kloss, rpokala
299  *
300  * @param[in] dev
301  *
302  * @param[in] slave
303  *      The SMBus address of the target device.
304  *
305  * @param[in] cmd
306  *      The SMBus command for the target device; this is the offset for SPDs,
307  *      or the register number for TSODs.
308  *
309  * @param[in,out] data
310  *      Pointer to either the value to be written, or where to place the value
311  *      which was read.
312  *
313  * @param[in] word_op
314  *      Bool: is this a word operation?
315  *
316  * @param[in] write_op
317  *      Bool: is this a write operation?
318  */
319 static int
320 imcsmb_transfer(device_t dev, u_char slave, char cmd, void *data, int word_op,
321     int write_op)
322 {
323 	struct imcsmb_softc *sc;
324 	int i;
325 	int rc;
326 	uint32_t cmd_val;
327 	uint32_t cntl_val;
328 	uint32_t orig_cntl_val;
329 	uint32_t stat_val;
330 	uint16_t *word;
331 	uint16_t lword;
332 	uint8_t *byte;
333 	uint8_t lbyte;
334 
335 	sc = device_get_softc(dev);
336 	byte = data;
337 	word = data;
338 	lbyte = *byte;
339 	lword = *word;
340 
341 	/* We modify the value of the control register; save the original, so
342 	 * we can restore it later
343 	 */
344 	orig_cntl_val = pci_read_config(sc->imcsmb_pci,
345 	    sc->regs->smb_cntl, 4);
346 	cntl_val = orig_cntl_val;
347 
348 	/*
349 	 * Set up the SMBCNTL register
350 	 */
351 
352 	/* [31:28] Clear the existing value of the DTI bits, then set them to
353 	 * the four high bits of the slave address.
354 	 */
355 	cntl_val &= ~IMCSMB_CNTL_DTI_MASK;
356 	cntl_val |= ((uint32_t) slave & 0xf0) << 24;
357 
358 	/* [27:27] Set the CLK_OVERRIDE bit, to enable normal operation */
359 	cntl_val |= IMCSMB_CNTL_CLK_OVERRIDE;
360 
361 	/* [26:26] Clear the WRITE_DISABLE bit; the datasheet says this isn't
362 	 * necessary, but empirically, it is.
363 	 */
364 	cntl_val &= ~IMCSMB_CNTL_WRITE_DISABLE_BIT;
365 
366 	/* [9:9] Clear the POLL_EN bit, to stop the hardware TSOD polling. */
367 	cntl_val &= ~IMCSMB_CNTL_POLL_EN;
368 
369 	/*
370 	 * Set up the SMBCMD register
371 	 */
372 
373 	/* [31:31] Set the TRIGGER bit; when this gets written, the controller
374 	 * will issue the command.
375 	 */
376 	cmd_val = IMCSMB_CMD_TRIGGER_BIT;
377 
378 	/* [29:29] For word operations, set the WORD_ACCESS bit. */
379 	if (word_op) {
380 		cmd_val |= IMCSMB_CMD_WORD_ACCESS;
381 	}
382 
383 	/* [27:27] For write operations, set the WRITE bit. */
384 	if (write_op) {
385 		cmd_val |= IMCSMB_CMD_WRITE_BIT;
386 	}
387 
388 	/* [26:24] The three non-DTI, non-R/W bits of the slave address. */
389 	cmd_val |= (uint32_t) ((slave & 0xe) << 23);
390 
391 	/* [23:16] The command (offset in the case of an EEPROM, or register in
392 	 * the case of TSOD or NVDIMM controller).
393 	 */
394 	cmd_val |= (uint32_t) ((uint8_t) cmd << 16);
395 
396 	/* [15:0] The data to be written for a write operation. */
397 	if (write_op) {
398 		if (word_op) {
399 			/* The datasheet says the controller uses different
400 			 * endianness for word operations on I2C vs SMBus!
401 			 *      I2C: [15:8] = MSB; [7:0] = LSB
402 			 *      SMB: [15:8] = LSB; [7:0] = MSB
403 			 * As a practical matter, this controller is very
404 			 * specifically for use with DIMMs, the SPD (and
405 			 * NVDIMM controllers) are only accessed as bytes,
406 			 * the temperature sensor is only accessed as words, and
407 			 * the temperature sensors are I2C. Thus, byte-swap the
408 			 * word.
409 			 */
410 			lword = htobe16(lword);
411 		} else {
412 			/* For byte operations, the data goes in the LSB, and
413 			 * the MSB is a don't care.
414 			 */
415 			lword = (uint16_t) (lbyte & 0xff);
416 		}
417 		cmd_val |= lword;
418 	}
419 
420 	/* Write the updated value to the control register first, to disable
421 	 * the hardware TSOD polling.
422 	 */
423 	pci_write_config(sc->imcsmb_pci, sc->regs->smb_cntl, cntl_val, 4);
424 
425 	/* Poll on the BUSY bit in the status register until clear, or timeout.
426 	 * We just cleared the auto-poll bit, so we need to make sure the device
427 	 * is idle before issuing a command. We can safely timeout after 35 ms,
428 	 * as this is the maximum time the SMBus spec allows for a transaction.
429 	 */
430 	for (i = 4; i != 0; i--) {
431 		stat_val = pci_read_config(sc->imcsmb_pci, sc->regs->smb_stat,
432 		    4);
433 		if ((stat_val & IMCSMB_STATUS_BUSY_BIT) == 0) {
434 			break;
435 		}
436 		pause("imcsmb", 10 * hz / 1000);
437 	}
438 
439 	if (i == 0) {
440 		device_printf(sc->dev,
441 		    "transfer: timeout waiting for device to settle\n");
442 	}
443 
444 	/* Now that polling has stopped, we can write the command register. This
445 	 * starts the SMBus command.
446 	 */
447 	pci_write_config(sc->imcsmb_pci, sc->regs->smb_cmd, cmd_val, 4);
448 
449 	/* Wait for WRITE_DATA_DONE/READ_DATA_VALID to be set, or timeout and
450 	 * fail. We wait up to 35ms.
451 	 */
452 	for (i = 35000; i != 0; i -= 10)
453 	{
454 		DELAY(10);
455 		stat_val = pci_read_config(sc->imcsmb_pci, sc->regs->smb_stat,
456 		    4);
457 		/* For a write, the bits holding the data contain the data being
458 		 * written. You'd think that would cause the READ_DATA_VALID bit
459 		 * to be cleared, because the data bits no longer contain valid
460 		 * data from the most recent read operation. While that would be
461 		 * logical, that's not the case here: READ_DATA_VALID is only
462 		 * cleared when starting a read operation, and WRITE_DATA_DONE
463 		 * is only cleared when starting a write operation.
464 		 */
465 		if (write_op) {
466 			if ((stat_val & IMCSMB_STATUS_WRITE_DATA_DONE) != 0) {
467 				break;
468 			}
469 		} else {
470 			if ((stat_val & IMCSMB_STATUS_READ_DATA_VALID) != 0) {
471 				break;
472 			}
473 		}
474 	}
475 	if (i == 0) {
476 		rc = SMB_ETIMEOUT;
477 		device_printf(dev, "transfer timeout\n");
478 		goto out;
479 	}
480 
481 	/* It is generally the case that this bit indicates non-ACK, but it
482 	 * could also indicate other bus errors. There's no way to tell the
483 	 * difference.
484 	 */
485 	if ((stat_val & IMCSMB_STATUS_BUS_ERROR_BIT) != 0) {
486 		/* While it is not documented, empirically, SPD page-change
487 		 * commands (writes with DTI = 0x60) always complete with the
488 		 * error bit set. So, ignore it in those cases.
489 		 */
490 		if ((slave & 0xf0) != 0x60) {
491 			rc = SMB_ENOACK;
492 			goto out;
493 		}
494 	}
495 
496 	/* For a read operation, copy the data out */
497 	if (write_op == 0) {
498 		if (word_op) {
499 			/* The data is returned in bits [15:0]; as discussed
500 			 * above, byte-swap.
501 			 */
502 			lword = (uint16_t) (stat_val & 0xffff);
503 			lword = htobe16(lword);
504 			*word = lword;
505 		} else {
506 			/* The data is returned in bits [7:0] */
507 			lbyte = (uint8_t) (stat_val & 0xff);
508 			*byte = lbyte;
509 		}
510 	}
511 
512 	/* A lack of an error is, de facto, success. */
513 	rc = SMB_ENOERR;
514 
515 out:
516 	/* Restore the original value of the control register. */
517 	pci_write_config(sc->imcsmb_pci, sc->regs->smb_cntl, orig_cntl_val, 4);
518 	return (rc);
519 }
520 
521 /* Device methods */
522 static device_method_t imcsmb_methods[] = {
523 	/* Device interface */
524 	DEVMETHOD(device_attach,	imcsmb_attach),
525 	DEVMETHOD(device_detach,	imcsmb_detach),
526 	DEVMETHOD(device_probe,		imcsmb_probe),
527 
528 	/* smbus methods */
529 	DEVMETHOD(smbus_callback,	imcsmb_callback),
530 	DEVMETHOD(smbus_readb,		imcsmb_readb),
531 	DEVMETHOD(smbus_readw,		imcsmb_readw),
532 	DEVMETHOD(smbus_writeb,		imcsmb_writeb),
533 	DEVMETHOD(smbus_writew,		imcsmb_writew),
534 
535 	DEVMETHOD_END
536 };
537 
538 static driver_t imcsmb_driver = {
539 	.name = "imcsmb",
540 	.methods = imcsmb_methods,
541 	.size = sizeof(struct imcsmb_softc),
542 };
543 
544 DRIVER_MODULE(imcsmb, imcsmb_pci, imcsmb_driver, 0, 0);
545 MODULE_DEPEND(imcsmb, smbus, SMBUS_MINVER, SMBUS_PREFVER, SMBUS_MAXVER);
546 MODULE_VERSION(imcsmb, 1);
547 
548 DRIVER_MODULE(smbus, imcsmb, smbus_driver, 0, 0);
549 
550 /* vi: set ts=8 sw=4 sts=8 noet: */
551