Training courses

Kernel and Embedded Linux

Bootlin training courses

Embedded Linux, kernel,
Yocto Project, Buildroot, real-time,
graphics, boot time, debugging...

Bootlin logo

Elixir Cross Referencer

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
/*-
 * Copyright (c) 2016 The NetBSD Foundation, Inc.
 * All rights reserved.
 *
 * This code is derived from software contributed to The NetBSD Foundation
 * by Masanobu SAITOH.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/*-
 * Copyright (C) 2014 Intel Corporation
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of Intel Corporation nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
#if 0
__FBSDID("$FreeBSD: head/sys/dev/ismt/ismt.c 266474 2014-05-20 19:55:06Z jimharris $");
#endif
__KERNEL_RCSID(0, "$NetBSD: ismt.c,v 1.6 2017/08/17 01:24:09 msaitoh Exp $");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/errno.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/mutex.h>
#include <sys/proc.h>

#include <sys/bus.h>

#include <dev/pci/pcidevs.h>
#include <dev/pci/pcireg.h>
#include <dev/pci/pcivar.h>

#include <dev/i2c/i2cvar.h>

#define ISMT_DESC_ENTRIES	32

/* Hardware Descriptor Constants - Control Field */
#define ISMT_DESC_CWRL	0x01	/* Command/Write Length */
#define ISMT_DESC_BLK	0X04	/* Perform Block Transaction */
#define ISMT_DESC_FAIR	0x08	/* Set fairness flag upon successful arbit. */
#define ISMT_DESC_PEC	0x10	/* Packet Error Code */
#define ISMT_DESC_I2C	0x20	/* I2C Enable */
#define ISMT_DESC_INT	0x40	/* Interrupt */
#define ISMT_DESC_SOE	0x80	/* Stop On Error */

/* Hardware Descriptor Constants - Status Field */
#define ISMT_DESC_SCS	0x01	/* Success */
#define ISMT_DESC_DLTO	0x04	/* Data Low Time Out */
#define ISMT_DESC_NAK	0x08	/* NAK Received */
#define ISMT_DESC_CRC	0x10	/* CRC Error */
#define ISMT_DESC_CLTO	0x20	/* Clock Low Time Out */
#define ISMT_DESC_COL	0x40	/* Collisions */
#define ISMT_DESC_LPR	0x80	/* Large Packet Received */

/* Macros */
#define ISMT_DESC_ADDR_RW(addr, is_read) (((addr) << 1) | (is_read))

/* iSMT General Register address offsets (SMBBAR + <addr>) */
#define ISMT_GR_GCTRL		0x000	/* General Control */
#define ISMT_GR_SMTICL		0x008	/* SMT Interrupt Cause Location */
#define ISMT_GR_ERRINTMSK	0x010	/* Error Interrupt Mask */
#define ISMT_GR_ERRAERMSK	0x014	/* Error AER Mask */
#define ISMT_GR_ERRSTS		0x018	/* Error Status */
#define ISMT_GR_ERRINFO		0x01c	/* Error Information */

/* iSMT Master Registers */
#define ISMT_MSTR_MDBA		0x100	/* Master Descriptor Base Address */
#define ISMT_MSTR_MCTRL		0x108	/* Master Control */
#define ISMT_MSTR_MSTS		0x10c	/* Master Status */
#define ISMT_MSTR_MDS		0x110	/* Master Descriptor Size */
#define ISMT_MSTR_RPOLICY	0x114	/* Retry Policy */

/* iSMT Miscellaneous Registers */
#define ISMT_SPGT	0x300	/* SMBus PHY Global Timing */

/* General Control Register (GCTRL) bit definitions */
#define ISMT_GCTRL_TRST	0x04	/* Target Reset */
#define ISMT_GCTRL_KILL	0x08	/* Kill */
#define ISMT_GCTRL_SRST	0x40	/* Soft Reset */

/* Master Control Register (MCTRL) bit definitions */
#define ISMT_MCTRL_SS	0x01		/* Start/Stop */
#define ISMT_MCTRL_MEIE	0x10		/* Master Error Interrupt Enable */
#define ISMT_MCTRL_FMHP	0x00ff0000	/* Firmware Master Head Ptr (FMHP) */

/* Master Status Register (MSTS) bit definitions */
#define ISMT_MSTS_HMTP	0xff0000	/* HW Master Tail Pointer (HMTP) */
#define ISMT_MSTS_MIS	0x20		/* Master Interrupt Status (MIS) */
#define ISMT_MSTS_MEIS	0x10		/* Master Error Int Status (MEIS) */
#define ISMT_MSTS_IP	0x01		/* In Progress */

/* Master Descriptor Size (MDS) bit definitions */
#define ISMT_MDS_MASK	0xff	/* Master Descriptor Size mask (MDS) */

/* SMBus PHY Global Timing Register (SPGT) bit definitions */
#define ISMT_SPGT_SPD_MASK	0xc0000000	/* SMBus Speed mask */
#define ISMT_SPGT_SPD_80K	0x00		/* 80 kHz */
#define ISMT_SPGT_SPD_100K	(0x1 << 30)	/* 100 kHz */
#define ISMT_SPGT_SPD_400K	(0x2 << 30)	/* 400 kHz */
#define ISMT_SPGT_SPD_1M	(0x3 << 30)	/* 1 MHz */

/* MSI Control Register (MSICTL) bit definitions */
#define ISMT_MSICTL_MSIE	0x01	/* MSI Enable */

#define ISMT_MAX_BLOCK_SIZE	32 /* per SMBus spec */

#define ISMT_INTR_TIMEOUT	(hz / 50) /* 0.02s */
#define ISMT_POLL_DELAY		100	/* 100usec */
#define ISMT_POLL_COUNT		200	/* 100usec * 200 = 0.02s */

//#define ISMT_DEBUG	aprint_debug_dev
#ifndef ISMT_DEBUG
#define ISMT_DEBUG(...)
#endif

#define ISMT_LOW(a)	((a) & 0xFFFFFFFFULL)
#define ISMT_HIGH(a)	(((uint64_t)(a) >> 32) & 0xFFFFFFFFFULL)

/* iSMT Hardware Descriptor */
struct ismt_desc {
	uint8_t tgtaddr_rw;	/* target address & r/w bit */
	uint8_t wr_len_cmd;	/* write length in bytes or a command */
	uint8_t rd_len;		/* read length */
	uint8_t control;	/* control bits */
	uint8_t status;		/* status bits */
	uint8_t retry;		/* collision retry and retry count */
	uint8_t rxbytes;	/* received bytes */
	uint8_t txbytes;	/* transmitted bytes */
	uint32_t dptr_low;	/* lower 32 bit of the data pointer */
	uint32_t dptr_high;	/* upper 32 bit of the data pointer */
} __packed;

#define DESC_SIZE	(ISMT_DESC_ENTRIES * sizeof(struct ismt_desc))

#define DMA_BUFFER_SIZE	64

struct ismt_softc {
	device_t		pcidev;
	device_t		smbdev;

	struct i2c_controller	sc_i2c_tag;
	kmutex_t 		sc_i2c_mutex;

	pci_chipset_tag_t	sc_pc;
	pcitag_t		sc_pcitag;
	pci_intr_handle_t	*sc_pihp;
	void			*sc_ih;

	bus_space_tag_t		mmio_tag;
	bus_space_handle_t	mmio_handle;
	bus_size_t		mmio_size;

	uint8_t			head;

	struct ismt_desc	*desc;
	bus_dma_tag_t		desc_dma_tag;
	bus_dmamap_t		desc_dma_map;
	bus_dma_segment_t	desc_dma_seg;
	int			desc_rseg;

	uint8_t			*dma_buffer;
	bus_dma_tag_t		dma_buffer_dma_tag;
	bus_dmamap_t		dma_buffer_dma_map;
	bus_dma_segment_t	dma_buffer_dma_seg;
	int			dma_buffer_rseg;

	uint8_t			using_msi;
};

static int	ismt_intr(void *);
static int	ismt_i2c_acquire_bus(void *, int);
static void	ismt_i2c_release_bus(void *, int);
static int	ismt_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *,
    size_t, void *, size_t, int);
static struct ismt_desc *ismt_alloc_desc(struct ismt_softc *);
static int	ismt_submit(struct ismt_softc *, struct ismt_desc *,
    i2c_addr_t, uint8_t, int);
static int	ismt_quick(struct ismt_softc *, i2c_addr_t, i2c_op_t, int);
static int	ismt_sendb(struct ismt_softc *, i2c_addr_t, i2c_op_t, char,
    int);
static int	ismt_recvb(struct ismt_softc *, i2c_addr_t, i2c_op_t, int);
static int	ismt_writeb(struct ismt_softc *, i2c_addr_t, i2c_op_t, uint8_t,
    char, int);
static int	ismt_writew(struct ismt_softc *, i2c_addr_t, i2c_op_t, uint8_t,
    uint16_t, int);
static int	ismt_readb(struct ismt_softc *, i2c_addr_t, i2c_op_t, char,
    int);
static int	ismt_readw(struct ismt_softc *, i2c_addr_t, i2c_op_t, char,
    int);

static int	ismt_match(device_t, cfdata_t, void *);
static void	ismt_attach(device_t, device_t, void *);
static int	ismt_detach(device_t, int);
static int	ismt_rescan(device_t, const char *, const int *);
static void	ismt_config_interrupts(device_t);
static void	ismt_chdet(device_t, device_t);

CFATTACH_DECL3_NEW(ismt, sizeof(struct ismt_softc),
    ismt_match, ismt_attach, ismt_detach, NULL, ismt_rescan, ismt_chdet,
    DVF_DETACH_SHUTDOWN);

static int
ismt_intr(void *arg)
{
	struct ismt_softc *sc = arg;
	uint32_t val;

	val = bus_space_read_4(sc->mmio_tag, sc->mmio_handle, ISMT_MSTR_MSTS);
	if ((sc->using_msi == 0)
	    && (val & (ISMT_MSTS_MIS | ISMT_MSTS_MEIS)) == 0)
		return 0; /* Not for me */

	ISMT_DEBUG(sc->pcidev, "%s MSTS = 0x%08x\n", __func__, val);

	val |= (ISMT_MSTS_MIS | ISMT_MSTS_MEIS);
	bus_space_write_4(sc->mmio_tag, sc->mmio_handle, ISMT_MSTR_MSTS, val);

	if (sc->using_msi)
		wakeup(sc);

	return 1;
}

static int
ismt_i2c_acquire_bus(void *cookie, int flags)
{
	struct ismt_softc *sc = cookie;

	mutex_enter(&sc->sc_i2c_mutex);
	return 0;
}

static void
ismt_i2c_release_bus(void *cookie, int flags)
{
	struct ismt_softc *sc = cookie;

	mutex_exit(&sc->sc_i2c_mutex);
}

static int
ismt_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr,
    const void *cmd, size_t cmdlen, void *buf, size_t buflen, int flags)
{
	struct ismt_softc *sc = cookie;
	uint8_t *p = buf;
	int rv;

	ISMT_DEBUG(sc->pcidev, "exec: op %d, addr 0x%02x, cmdlen %zu, "
 	    " buflen %zu, flags 0x%02x\n", op, addr, cmdlen, buflen, flags);

	if ((cmdlen == 0) && (buflen == 0))
		return ismt_quick(sc, addr, op, flags);

	if (I2C_OP_READ_P(op) && (cmdlen == 0) && (buflen == 1)) {
		rv = ismt_recvb(sc, addr, op, flags);
		if (rv == -1)
			return -1;
		*p = (uint8_t)rv;
		return 0;
	}

	if ((I2C_OP_READ_P(op)) && (cmdlen == 1) && (buflen == 1)) {
		rv = ismt_readb(sc, addr, op, *(const uint8_t*)cmd, flags);
		if (rv == -1)
			return -1;
		*p = (uint8_t)rv;
		return 0;
	}

	if ((I2C_OP_READ_P(op)) && (cmdlen == 1) && (buflen == 2)) {
		rv = ismt_readw(sc, addr, op, *(const uint8_t*)cmd, flags);
		if (rv == -1)
			return -1;
		*(uint16_t *)p = (uint16_t)rv;
		return 0;
	}

	if ((I2C_OP_WRITE_P(op)) && (cmdlen == 0) && (buflen == 1))
		return ismt_sendb(sc, addr, op, *(uint8_t*)buf, flags);

	if ((I2C_OP_WRITE_P(op)) && (cmdlen == 1) && (buflen == 1))
		return ismt_writeb(sc, addr, op, *(const uint8_t*)cmd,
		    *(uint8_t*)buf, flags);

	if ((I2C_OP_WRITE_P(op)) && (cmdlen == 1) && (buflen == 2))
		return ismt_writew(sc, addr, op,
		    *(const uint8_t*)cmd, *((uint16_t *)buf), flags);

	return -1;
}

static struct ismt_desc *
ismt_alloc_desc(struct ismt_softc *sc)
{
	struct ismt_desc *desc;

	KASSERT(mutex_owned(&sc->sc_i2c_mutex));

	desc = &sc->desc[sc->head++];
	if (sc->head == ISMT_DESC_ENTRIES)
		sc->head = 0;

	memset(desc, 0, sizeof(*desc));

	return (desc);
}

static int
ismt_submit(struct ismt_softc *sc, struct ismt_desc *desc, i2c_addr_t slave,
    uint8_t is_read, int flags)
{
	uint32_t err, fmhp, val;
	int timeout, i;

	if (sc->using_msi == 0)
		flags |= I2C_F_POLL;
	desc->control |= ISMT_DESC_FAIR;
	if ((flags & I2C_F_POLL) == 0)
		desc->control |= ISMT_DESC_INT;

	desc->tgtaddr_rw = ISMT_DESC_ADDR_RW(slave, is_read);
	desc->dptr_low = ISMT_LOW(sc->dma_buffer_dma_map->dm_segs[0].ds_addr);
	desc->dptr_high = ISMT_HIGH(sc->dma_buffer_dma_map->dm_segs[0].ds_addr);

	bus_dmamap_sync(sc->desc_dma_tag, sc->desc_dma_map,
	    desc - &sc->desc[0], sizeof(struct ismt_desc),
	    BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);

	fmhp = sc->head << 16;
	val = bus_space_read_4(sc->mmio_tag, sc->mmio_handle, ISMT_MSTR_MCTRL);
	val &= ~ISMT_MCTRL_FMHP;
	val |= fmhp;
	bus_space_write_4(sc->mmio_tag, sc->mmio_handle, ISMT_MSTR_MCTRL, val);

	/* set the start bit */
	val = bus_space_read_4(sc->mmio_tag, sc->mmio_handle, ISMT_MSTR_MCTRL);
	val |= ISMT_MCTRL_SS;
	bus_space_write_4(sc->mmio_tag, sc->mmio_handle, ISMT_MSTR_MCTRL, val);

	i = 0;
	if ((flags & I2C_F_POLL) == 0) {
		timeout = ISMT_INTR_TIMEOUT;
		if (timeout == 0)
			timeout = 1;
		err = tsleep(sc, PWAIT, "ismt_wait", timeout);
		if (err != 0) {
			ISMT_DEBUG(sc->pcidev, "%s timeout\n", __func__);
			return -1;
		}
	} else {
		/* Polling */
		for (i = 0; i < ISMT_POLL_COUNT; i++) {
			val = bus_space_read_4(sc->mmio_tag, sc->mmio_handle,
			    ISMT_MSTR_MSTS);
			if ((val & (ISMT_MSTS_MIS | ISMT_MSTS_MEIS)) != 0) {
				ismt_intr(sc);
				err = 0;
				break;
			}
			delay(ISMT_POLL_DELAY);
		}
		if (i >= ISMT_POLL_COUNT) {
			ISMT_DEBUG(sc->pcidev, "%s polling timeout. "
			    "MSTS = %08x\n", __func__, val);
			return -1;
		}
	}

	bus_dmamap_sync(sc->desc_dma_tag, sc->desc_dma_map,
	    desc - &sc->desc[0], sizeof(struct ismt_desc),
		BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
	ISMT_DEBUG(sc->pcidev, "%s status=0x%02x\n", __func__, desc->status);

	if (desc->status & ISMT_DESC_SCS)
		return 0;

	if (desc->status & ISMT_DESC_NAK)
		return -1;

	if (desc->status & ISMT_DESC_CRC)
		return -1;

	if (desc->status & ISMT_DESC_COL)
		return -1;

	if (desc->status & ISMT_DESC_LPR)
		return -1;

	if (desc->status & (ISMT_DESC_DLTO | ISMT_DESC_CLTO))
		return -1;

	return -1;
}

static int
ismt_quick(struct ismt_softc *sc, i2c_addr_t slave, i2c_op_t op, int flags)
{
	struct ismt_desc	*desc;
	int			is_read;

	ISMT_DEBUG(sc->pcidev, "%s\n", __func__);

	desc = ismt_alloc_desc(sc);
	is_read = I2C_OP_READ_P(op);
	return (ismt_submit(sc, desc, slave, is_read, flags));
}

static int
ismt_sendb(struct ismt_softc *sc, i2c_addr_t slave, i2c_op_t op, char byte,
    int flags)
{
	struct ismt_desc	*desc;

	ISMT_DEBUG(sc->pcidev, "%s\n", __func__);

	desc = ismt_alloc_desc(sc);
	desc->control = ISMT_DESC_CWRL;
	desc->wr_len_cmd = byte;

	return (ismt_submit(sc, desc, slave, 0, flags));
}

static int
ismt_recvb(struct ismt_softc *sc, i2c_addr_t slave, i2c_op_t op, int flags)
{
	struct ismt_desc	*desc;
	int			err;

	ISMT_DEBUG(sc->pcidev, "%s\n", __func__);

	desc = ismt_alloc_desc(sc);
	desc->rd_len = 1;

	err = ismt_submit(sc, desc, slave, 1, flags);

	if (err != 0)
		return (err);

	return sc->dma_buffer[0];
}

static int
ismt_writeb(struct ismt_softc *sc, i2c_addr_t slave, i2c_op_t op, uint8_t cmd,
    char byte, int flags)
{
	struct ismt_desc	*desc;

	ISMT_DEBUG(sc->pcidev, "%s\n", __func__);

	desc = ismt_alloc_desc(sc);
	desc->wr_len_cmd = 2;
	sc->dma_buffer[0] = cmd;
	sc->dma_buffer[1] = byte;

	return (ismt_submit(sc, desc, slave, 0, flags));
}

static int
ismt_writew(struct ismt_softc *sc, i2c_addr_t slave, i2c_op_t op, uint8_t cmd,
    uint16_t word, int flags)
{
	struct ismt_desc	*desc;

	ISMT_DEBUG(sc->pcidev, "%s\n", __func__);

	desc = ismt_alloc_desc(sc);
	desc->wr_len_cmd = 3;
	sc->dma_buffer[0] = cmd;
	sc->dma_buffer[1] = word & 0xFF;
	sc->dma_buffer[2] = word >> 8;

	return (ismt_submit(sc, desc, slave, 0, flags));
}

static int
ismt_readb(struct ismt_softc *sc, i2c_addr_t slave, i2c_op_t op, char cmd,
    int flags)
{
	struct ismt_desc	*desc;
	int			err;

	ISMT_DEBUG(sc->pcidev, "%s\n", __func__);

	desc = ismt_alloc_desc(sc);
	desc->control = ISMT_DESC_CWRL;
	desc->wr_len_cmd = cmd;
	desc->rd_len = 1;

	err = ismt_submit(sc, desc, slave, 1, flags);

	if (err != 0)
		return (err);

	return sc->dma_buffer[0];
}

static int
ismt_readw(struct ismt_softc *sc, i2c_addr_t slave, i2c_op_t op, char cmd,
    int flags)
{
	struct ismt_desc	*desc;
	uint16_t		word;
	int			err;

	ISMT_DEBUG(sc->pcidev, "%s\n", __func__);

	desc = ismt_alloc_desc(sc);
	desc->control = ISMT_DESC_CWRL;
	desc->wr_len_cmd = cmd;
	desc->rd_len = 2;

	err = ismt_submit(sc, desc, slave, 1, flags);

	if (err != 0)
		return (err);

	word = sc->dma_buffer[0] | (sc->dma_buffer[1] << 8);

	return word;
}

#if 0
static int
ismt_pcall(struct ismt_softc *sc, i2c_addr_t slave, i2c_op_t op, char cmd,
    uint16_t sdata, uint16_t *rdata, int flags)
{
	struct ismt_desc	*desc;
	int			err;

	ISMT_DEBUG(sc->pcidev, "%s\n", __func__);

	desc = ismt_alloc_desc(sc);
	desc->wr_len_cmd = 3;
	desc->rd_len = 2;
	sc->dma_buffer[0] = cmd;
	sc->dma_buffer[1] = sdata & 0xff;
	sc->dma_buffer[2] = sdata >> 8;

	err = ismt_submit(sc, desc, slave, 0, flags);

	if (err != 0)
		return (err);

	*rdata = sc->dma_buffer[0] | (sc->dma_buffer[1] << 8);

	return (err);
}

static int
ismt_bwrite(struct ismt_softc *sc, i2c_addr_t slave, i2c_op_t op, char cmd,
    u_char count, char *buf, int flags)
{
	struct ismt_desc	*desc;

	ISMT_DEBUG(sc->pcidev, "%s\n", __func__);

	if (count == 0 || count > ISMT_MAX_BLOCK_SIZE)
		return -1;

	desc = ismt_alloc_desc(sc);
	desc->control = ISMT_DESC_I2C;
	desc->wr_len_cmd = count + 1;
	sc->dma_buffer[0] = cmd;
	memcpy(&sc->dma_buffer[1], buf, count);

	return (ismt_submit(sc, desc, slave, 0, flags));
}

static int
ismt_bread(struct ismt_softc *sc, i2c_addr_t slave, i2c_op_t op, char cmd,
    u_char *count, char *buf, int flags)
{
	struct ismt_desc	*desc;
	int			err;

	ISMT_DEBUG(sc->pcidev, "%s\n", __func__);

	if (*count == 0 || *count > ISMT_MAX_BLOCK_SIZE)
		return -1;

	desc = ismt_alloc_desc(sc);
	desc->control = ISMT_DESC_I2C | ISMT_DESC_CWRL;
	desc->wr_len_cmd = cmd;
	desc->rd_len = *count;

	err = ismt_submit(sc, desc, slave, 0, flags);

	if (err != 0)
		return (err);

	memcpy(buf, sc->dma_buffer, desc->rxbytes);
	*count = desc->rxbytes;

	return (err);
}
#endif

static int
ismt_detach(device_t self, int flags)
{
	struct ismt_softc	*sc;
	int rv = 0;

	ISMT_DEBUG(self, "%s\n", __func__);
	sc = device_private(self);
	if (sc->smbdev != NULL) {
		rv = config_detach(sc->smbdev, flags);
		if (rv != 0)
			return rv;
	}
	if (sc->sc_ih != NULL) {
		pci_intr_disestablish(sc->sc_pc, sc->sc_ih);
		sc->sc_ih = NULL;
	}
	if (sc->sc_pihp != NULL) {
		pci_intr_release(sc->sc_pc, sc->sc_pihp, 1);
		sc->sc_pihp = NULL;
	}

	bus_dmamap_unload(sc->desc_dma_tag, sc->desc_dma_map);
	bus_dmamap_unload(sc->dma_buffer_dma_tag, sc->dma_buffer_dma_map);

	bus_dmamem_free(sc->desc_dma_tag, &sc->desc_dma_seg, sc->desc_rseg);
	bus_dmamem_free(sc->dma_buffer_dma_tag, &sc->dma_buffer_dma_seg,
	    sc->dma_buffer_rseg);

	if (sc->mmio_size)
		bus_space_unmap(sc->mmio_tag, sc->mmio_handle, sc->mmio_size);

	mutex_destroy(&sc->sc_i2c_mutex);
	return rv;
}

static void
ismt_attach(device_t parent, device_t self, void *aux)
{
	struct ismt_softc *sc = device_private(self);
	struct pci_attach_args *pa = aux;
	const char *intrstr = NULL;
	char intrbuf[PCI_INTRSTR_LEN];
	pcireg_t reg;
	int val;

	sc->pcidev = self;
	sc->sc_pc = pa->pa_pc;
	sc->sc_pcitag = pa->pa_tag;

	/* Enable busmastering */
	reg = pci_conf_read(sc->sc_pc, sc->sc_pcitag, PCI_COMMAND_STATUS_REG);
	reg |= PCI_COMMAND_MASTER_ENABLE;
	pci_conf_write(sc->sc_pc, sc->sc_pcitag, PCI_COMMAND_STATUS_REG, reg);

	pci_aprint_devinfo(pa, NULL);

	/* Map mem space */
	if (pci_mapreg_map(pa, PCI_BAR0, PCI_MAPREG_TYPE_MEM, 0,
	    &sc->mmio_tag, &sc->mmio_handle, NULL, &sc->mmio_size)) {
		aprint_error_dev(self, "can't map mem space\n");
		goto fail;
	}

	if (pci_dma64_available(pa)) {
		sc->desc_dma_tag = pa->pa_dmat64;
		sc->dma_buffer_dma_tag = pa->pa_dmat64;
	} else {
		sc->desc_dma_tag = pa->pa_dmat;
		sc->dma_buffer_dma_tag = pa->pa_dmat;
	}
	bus_dmamem_alloc(sc->desc_dma_tag, DESC_SIZE, PAGE_SIZE, 0,
	    &sc->desc_dma_seg, ISMT_DESC_ENTRIES, &sc->desc_rseg,
	    BUS_DMA_WAITOK);
	bus_dmamem_alloc(sc->dma_buffer_dma_tag, DMA_BUFFER_SIZE, PAGE_SIZE, 0,
	    &sc->dma_buffer_dma_seg, 1, &sc->dma_buffer_rseg, BUS_DMA_WAITOK);

	bus_dmamem_map(sc->desc_dma_tag, &sc->desc_dma_seg,
	    sc->desc_rseg, DESC_SIZE, (void **)&sc->desc, BUS_DMA_COHERENT);
	bus_dmamem_map(sc->dma_buffer_dma_tag, &sc->dma_buffer_dma_seg,
	    sc->dma_buffer_rseg, DMA_BUFFER_SIZE, (void **)&sc->dma_buffer,
	    BUS_DMA_COHERENT);

	bus_dmamap_create(sc->desc_dma_tag, DESC_SIZE, 1,
	    DESC_SIZE, 0, 0, &sc->desc_dma_map);
	bus_dmamap_create(sc->dma_buffer_dma_tag, DMA_BUFFER_SIZE, 1,
	    DMA_BUFFER_SIZE, 0, 0, &sc->dma_buffer_dma_map);

	bus_dmamap_load(sc->desc_dma_tag,
	    sc->desc_dma_map, sc->desc, DESC_SIZE, NULL, 0);
	bus_dmamap_load(sc->dma_buffer_dma_tag,
	    sc->dma_buffer_dma_map, sc->dma_buffer, DMA_BUFFER_SIZE,
	    NULL, 0);

	bus_space_write_4(sc->mmio_tag, sc->mmio_handle, ISMT_MSTR_MDBA,
	    ISMT_LOW(sc->desc_dma_map->dm_segs[0].ds_addr));
	bus_space_write_4(sc->mmio_tag, sc->mmio_handle, ISMT_MSTR_MDBA + 4,
	    ISMT_HIGH(sc->desc_dma_map->dm_segs[0].ds_addr));

	/* initialize the Master Control Register (MCTRL) */
	bus_space_write_4(sc->mmio_tag, sc->mmio_handle, ISMT_MSTR_MCTRL,
	    ISMT_MCTRL_MEIE);

	/* initialize the Master Status Register (MSTS) */
	bus_space_write_4(sc->mmio_tag, sc->mmio_handle, ISMT_MSTR_MSTS, 0);

	/* initialize the Master Descriptor Size (MDS) */
	val = bus_space_read_4(sc->mmio_tag, sc->mmio_handle, ISMT_MSTR_MDS);
	val &= ~ISMT_MDS_MASK;
	val |= (ISMT_DESC_ENTRIES - 1);
	bus_space_write_4(sc->mmio_tag, sc->mmio_handle, ISMT_MSTR_MDS, val);

	if (pci_intr_alloc(pa, &sc->sc_pihp, NULL, 0)) {
		aprint_error_dev(self, "couldn't map interrupt\n");
		return;
	}
	intrstr = pci_intr_string(pa->pa_pc, sc->sc_pihp[0], intrbuf,
	    sizeof(intrbuf));
	sc->sc_ih = pci_intr_establish_xname(pa->pa_pc, sc->sc_pihp[0],
	    IPL_BIO, ismt_intr, sc, device_xname(self));
	if (sc->sc_ih == NULL) {
		aprint_error_dev(sc->pcidev, "unable to establish %s\n",
		    (pci_intr_type(pa->pa_pc, sc->sc_pihp[0])
			== PCI_INTR_TYPE_MSI) ? "MSI" : "INTx");
		/* Polling */
	}

	if (pci_intr_type(pa->pa_pc, sc->sc_pihp[0]) == PCI_INTR_TYPE_MSI)
		sc->using_msi = 1;

	aprint_normal_dev(sc->pcidev, "interrupting at %s\n", intrstr);

	sc->smbdev = NULL;
	mutex_init(&sc->sc_i2c_mutex, MUTEX_DEFAULT, IPL_NONE);
	if (!pmf_device_register(self, NULL, NULL))
		aprint_error_dev(self, "couldn't establish power handler\n");

	config_interrupts(self, ismt_config_interrupts);
	return;

fail:
	ismt_detach(sc->pcidev, 0);

	return;
}

static int
ismt_match(device_t parent, cfdata_t match, void *aux)
{
	struct pci_attach_args *pa = aux;

	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL)
		return 0;

	switch (PCI_PRODUCT(pa->pa_id)) {
	case PCI_PRODUCT_INTEL_S1200_SMBUS_0:
	case PCI_PRODUCT_INTEL_S1200_SMBUS_1:
	case PCI_PRODUCT_INTEL_C2000_SMBUS:
	case PCI_PRODUCT_INTEL_C3K_SMBUS:
		break;
	default:
		return 0;
	}

	return 1;
}

static int
ismt_rescan(device_t self, const char *ifattr, const int *flags)
{
	struct ismt_softc *sc = device_private(self);
	struct i2cbus_attach_args iba;

	if (!ifattr_match(ifattr, "i2cbus"))
		return 0;

	if (sc->smbdev)
		return 0;

	/* Attach I2C bus */
	sc->sc_i2c_tag.ic_cookie = sc;
	sc->sc_i2c_tag.ic_acquire_bus = ismt_i2c_acquire_bus;
	sc->sc_i2c_tag.ic_release_bus = ismt_i2c_release_bus;
	sc->sc_i2c_tag.ic_exec = ismt_i2c_exec;

	memset(&iba, 0, sizeof(iba));
	iba.iba_type = I2C_TYPE_SMBUS;
	iba.iba_tag = &sc->sc_i2c_tag;
	sc->smbdev = config_found_ia(self, ifattr, &iba, iicbus_print);

	return 0;
}

static void
ismt_config_interrupts(device_t self)
{
	int flags = 0;

	ismt_rescan(self, "i2cbus", &flags);
}

static void
ismt_chdet(device_t self, device_t child)
{
	struct ismt_softc *sc = device_private(self);

	if (sc->smbdev == child)
		sc->smbdev = NULL;

}

MODULE(MODULE_CLASS_DRIVER, ismt, "pci");

#ifdef _MODULE
#include "ioconf.c"
#endif

static int
ismt_modcmd(modcmd_t cmd, void *opaque)
{
	int error = 0;

	switch (cmd) {
	case MODULE_CMD_INIT:
#ifdef _MODULE
		error = config_init_component(cfdriver_ioconf_ismt,
		    cfattach_ioconf_ismt, cfdata_ioconf_ismt);
#endif
		return error;
	case MODULE_CMD_FINI:
#ifdef _MODULE
		error = config_fini_component(cfdriver_ioconf_ismt,
		    cfattach_ioconf_ismt, cfdata_ioconf_ismt);
#endif
		return error;
	default:
		return ENOTTY;
	}
}