forked from illumos/illumos-gate
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathismt.c
More file actions
1230 lines (1091 loc) · 32.5 KB
/
ismt.c
File metadata and controls
1230 lines (1091 loc) · 32.5 KB
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
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This file and its contents are supplied under the terms of the
* Common Development and Distribution License ("CDDL"), version 1.0.
* You may only use this file in accordance with the terms of version
* 1.0 of the CDDL.
*
* A full copy of the text of the CDDL should have accompanied this
* source. A copy of the CDDL is also available via the Internet at
* http://www.illumos.org/license/CDDL.
*/
/*
* Copyright 2025 Oxide Computer Company
*/
/*
* Intel SMBus Message Transport controller driver.
*
* This is a DMA based SMBus controller that is found in some of the various
* Atom and Xeon-D based platforms. The hardware divides registers into three
* rough groups. There are the general registers, controller specific registers,
* and target specific registers. We don't implement the target.
*
* The device can support a ring of DMA transfers. We only will have one
* outstanding at a time, so we go with a simple length of 4 entries and have a
* single data DMA buffer that is used by all commands.
*
* Unlike the PCH-based SMBus controller, this controller supports both SMBus
* 2.0 commands and can perform arbitrary commands over I2C.
*/
#include <sys/modctl.h>
#include <sys/conf.h>
#include <sys/devops.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/pci.h>
#include <sys/sysmacros.h>
#include <sys/i2c/controller.h>
#include "ismt.h"
/*
* The controller has a single BAR, SMTBAR, which is found in the BAR 0/1. This
* translates to reg[1] as reg[0] contains the config space information about
* the device.
*/
#define ISMT_REGNO 1
/*
* Because we never use the offset and address for syncing, we want to cast the
* DMA sync call to void, but lets be paranoid on debug.
*/
#ifdef DEBUG
#define ISMT_DMA_SYNC(buf, flag) ASSERT0(ddi_dma_sync((buf).id_hdl, \
0, 0, flag))
#else
#define ISMT_DMA_SYNC(buf, flag) (void) ddi_dma_sync((buf).id_hdl, \
0, 0, flag)
#endif /* DEBUG */
/*
* Allocation sizes for our ring and DMA data buffer. We size the ring at 4
* entries (though it seems like we could probably get away with just one). The
* data buffer we size at 128 bytes, which covers both the maximum read and
* maximum write in one go (though we shouldn't exceed it either one). The last
* one of these, the interrupt cause log size is the hardest. It defitely is
* written before every interrupt, but whether we need one of these per entry in
* the log or it just clobbers the last location is unclear. It doesn't actually
* ask for a size so we just double the number of ring entries.
*/
#define ISMT_RING_NENTS 4
#define ISMT_RING_DMA_SIZE (ISMT_RING_NENTS * sizeof (ismt_desc_t))
#define ISMT_DATA_BUF_SIZE 128U
#define ISMT_ICL_DMA_SIZE (ISMT_RING_NENTS * sizeof (uint32_t) * 2)
typedef enum {
ISMT_INIT_PCI = 1 << 0,
ISMT_INIT_REGS = 1 << 1,
ISMT_INIT_INTR_ALLOC = 1 << 2,
ISMT_INIT_INTR_HDL = 1 << 3,
ISMT_INIT_SYNC = 1 << 4,
ISMT_INIT_INTR_EN = 1 << 5,
ISMT_INIT_I2C = 1 << 6
} ismt_init_t;
typedef struct {
ddi_dma_handle_t id_hdl;
caddr_t id_va;
ddi_acc_handle_t id_acc;
size_t id_alloc_len;
size_t id_size;
} ismt_dma_t;
typedef struct {
dev_info_t *ismt_dip;
ddi_acc_handle_t ismt_cfg;
ismt_init_t ismt_init;
/*
* Register related data
*/
caddr_t ismt_base;
off_t ismt_regsize;
ddi_acc_handle_t ismt_regs;
/*
* DMA Information
*/
ismt_dma_t ismt_ring_dma;
ismt_dma_t ismt_data_dma;
ismt_dma_t ismt_icl_dma;
/*
* Interrupt data
*/
int ismt_nintrs;
int ismt_itype;
ddi_intr_handle_t ismt_intr_hdl;
uint_t ismt_intr_pri;
/*
* Request and framework synchronization.
*/
i2c_speed_t ismt_speed;
kmutex_t ismt_mutex;
kcondvar_t ismt_cv;
i2c_ctrl_hdl_t *ismt_hdl;
uint32_t ismt_head;
uint32_t ismt_tail;
ismt_desc_t *ismt_ring;
smbus_req_t *ismt_req;
i2c_req_t *ismt_i2creq;
i2c_error_t *ismt_err;
bool ismt_req_done;
} ismt_t;
/*
* Consolidated DMA attributes for the descriptor ring and the data buffer. We
* use the stricter requirements from each because we don't actually allocate
* that much DMA memory here to make this simpler.
*/
static const ddi_dma_attr_t ismt_dma_attr = {
.dma_attr_version = DMA_ATTR_V0,
/*
* Our DMA attributes can appear anywhere in 64-bit space.
*/
.dma_attr_addr_lo = 0,
.dma_attr_addr_hi = UINT64_MAX,
/*
* Up to 255 bytes are allowed to be specified for transmit / recieve,
* where as the descriptor ring allows for up to 256 16 byte
* descriptors. We use the latter for here, with the knowledge that
* we're never allocating more than an amount that can fit due to the
* maxxfer value below.
*/
.dma_attr_count_max = 256 * sizeof (ismt_desc_t),
/*
* The descriptor ring requires 64 byte alignment, where as the data
* buffer requires byte alignment. Use 64 byte alignment.
*/
.dma_attr_align = 0x40,
/*
* Cargo culted burst sizes as PCIe probably doens't have quite the same
* concerns.
*/
.dma_attr_burstsizes = 0xfff,
/*
* We set the minimum and maximum to the sizes here. The size limits are
* really set by PCIe breaking up transactions, not anything in the
* kernel. Therefore we set the maximum to the same as
* dma_attr_count_max, partially so we can avoid complaints about DMA
* less than a page by x86 rootnex.
*/
.dma_attr_minxfer = 1,
.dma_attr_maxxfer = 256 * sizeof (ismt_desc_t),
/*
* Their are no segment restrictions and only one cookie can be used.
* For the granularity we basically set this to 1 because everything we
* allocate will be a multiple of this and we only have one cookie so it
* won't really be split..
*/
.dma_attr_seg = UINT64_MAX,
.dma_attr_sgllen = 1,
.dma_attr_granular = 1,
.dma_attr_flags = 0
};
static uint32_t
ismt_read32(ismt_t *ismt, uint32_t reg)
{
ASSERT3U(reg, <, ismt->ismt_regsize);
return (ddi_get32(ismt->ismt_regs, (uint32_t *)(ismt->ismt_base +
reg)));
}
static void
ismt_write32(ismt_t *ismt, uint32_t reg, uint32_t val)
{
ASSERT3U(reg, <, ismt->ismt_regsize);
ddi_put32(ismt->ismt_regs, (uint32_t *)(ismt->ismt_base + reg), val);
}
static void
ismt_write64(ismt_t *ismt, uint32_t reg, uint64_t val)
{
ASSERT3U(reg, <, ismt->ismt_regsize);
ddi_put64(ismt->ismt_regs, (uint64_t *)(ismt->ismt_base + reg), val);
}
static i2c_errno_t
ismt_prop_info(void *arg, i2c_prop_t prop, i2c_prop_info_t *info)
{
switch (prop) {
case I2C_PROP_BUS_SPEED:
i2c_prop_info_set_pos_bit32(info, I2C_SPEED_STD |
I2C_SPEED_FAST | I2C_SPEED_FPLUS);
break;
case SMBUS_PROP_SUP_OPS:
case I2C_PROP_MAX_READ:
case I2C_PROP_MAX_WRITE:
case SMBUS_PROP_MAX_BLOCK:
break;
default:
return (I2C_PROP_E_UNSUP);
}
i2c_prop_info_set_perm(info, I2C_PROP_PERM_RO);
return (I2C_CORE_E_OK);
}
/*
* Currently all the information that we retrun is static. If this changes, we
* should ensure we hold ismt_mutex during this.
*/
static i2c_errno_t
ismt_prop_get(void *arg, i2c_prop_t prop, void *buf, size_t buflen)
{
ismt_t *ismt = arg;
uint32_t val;
switch (prop) {
case I2C_PROP_BUS_SPEED:
val = ismt->ismt_speed;
break;
case SMBUS_PROP_SUP_OPS:
val = SMBUS_PROP_OP_QUICK_COMMAND | SMBUS_PROP_OP_SEND_BYTE |
SMBUS_PROP_OP_RECV_BYTE | SMBUS_PROP_OP_WRITE_BYTE |
SMBUS_PROP_OP_READ_BYTE | SMBUS_PROP_OP_WRITE_WORD |
SMBUS_PROP_OP_READ_WORD | SMBUS_PROP_OP_PROCESS_CALL |
SMBUS_PROP_OP_WRITE_BLOCK | SMBUS_PROP_OP_READ_BLOCK |
SMBUS_PROP_OP_BLOCK_PROCESS_CALL |
SMBUS_PROP_OP_I2C_WRITE_BLOCK |
SMBUS_PROP_OP_I2C_READ_BLOCK;
break;
case I2C_PROP_MAX_READ:
case I2C_PROP_MAX_WRITE:
val = ISMT_MAX_I2C;
break;
case SMBUS_PROP_MAX_BLOCK:
val = ISMT_MAX_SMBUS;
break;
default:
return (I2C_PROP_E_UNSUP);
}
VERIFY3U(buflen, >=, sizeof (val));
bcopy(&val, buf, sizeof (val));
return (I2C_CORE_E_OK);
}
static void
ismt_io_error(ismt_t *ismt, uint32_t sts)
{
i2c_ctrl_error_t err;
VERIFY3P(ismt->ismt_err, !=, NULL);
if (ISMT_DESC_STS_GET_NACK(sts) != 0) {
if (ISMT_DESC_STS_GET_WRLEN(sts) == 0) {
err = I2C_CTRL_E_ADDR_NACK;
} else {
err = I2C_CTRL_E_DATA_NACK;
}
} else if (ISMT_DESC_STS_GET_CRC(sts) != 0) {
/*
* As we don't enable PEC right now, we don't expect to see
* this. When we do, then this should be changed.
*/
err = I2C_CTRL_E_DRIVER;
} else if (ISMT_DESC_STS_GET_CLTO(sts) != 0) {
err = I2C_CTRL_E_SMBUS_CLOCK_LOW;
} else if (ISMT_DESC_STS_GET_COL(sts) != 0) {
err = I2C_CTRL_E_ARB_LOST;
} else if (ISMT_DESC_STS_GET_LPR(sts) != 0) {
err = I2C_CTRL_E_DRIVER;
} else {
err = I2C_CTRL_E_INTERNAL;
}
i2c_ctrl_io_error(ismt->ismt_err, I2C_CORE_E_CONTROLLER, err);
}
/*
* Process the current completion.
*/
static void
ismt_io(ismt_t *ismt)
{
ismt_desc_t *desc;
uint32_t sts;
VERIFY(MUTEX_HELD(&ismt->ismt_mutex));
ISMT_DMA_SYNC(ismt->ismt_ring_dma, DDI_DMA_SYNC_FORKERNEL);
ISMT_DMA_SYNC(ismt->ismt_data_dma, DDI_DMA_SYNC_FORKERNEL);
desc = &ismt->ismt_ring[ismt->ismt_tail];
ismt->ismt_tail = (ismt->ismt_tail + 1) % ISMT_RING_NENTS;
const uint8_t *buf = (uint8_t *)ismt->ismt_data_dma.id_va;
sts = LE_32(desc->id_status);
if (ISMT_DESC_STS_GET_SCS(sts) == 0) {
ismt_io_error(ismt, sts);
return;
}
if (ismt->ismt_i2creq != NULL) {
VERIFY3P(ismt->ismt_req, ==, NULL);
if (ismt->ismt_i2creq->ir_rlen > 0) {
VERIFY3U(ismt->ismt_i2creq->ir_rlen, ==,
ISMT_DESC_STS_GET_RDLEN(sts));
bcopy(buf, ismt->ismt_i2creq->ir_rdata,
ISMT_DESC_STS_GET_RDLEN(sts));
}
i2c_ctrl_io_success(ismt->ismt_err);
return;
}
switch (ismt->ismt_req->smbr_op) {
case SMBUS_OP_QUICK_COMMAND:
case SMBUS_OP_SEND_BYTE:
case SMBUS_OP_WRITE_BYTE:
case SMBUS_OP_WRITE_WORD:
case SMBUS_OP_WRITE_BLOCK:
case SMBUS_OP_I2C_WRITE_BLOCK:
/*
* Nothing to do for writes.
*/
break;
case SMBUS_OP_RECV_BYTE:
case SMBUS_OP_READ_BYTE:
ismt->ismt_req->smbr_rdata[0] = buf[0];
break;
case SMBUS_OP_READ_WORD:
case SMBUS_OP_PROCESS_CALL:
ismt->ismt_req->smbr_rdata[0] = buf[0];
ismt->ismt_req->smbr_rdata[1] = buf[1];
break;
case SMBUS_OP_READ_BLOCK:
case SMBUS_OP_BLOCK_PROCESS_CALL:
if (ISMT_DESC_STS_GET_RDLEN(sts) != buf[0] + 1) {
i2c_ctrl_io_error(ismt->ismt_err, I2C_CORE_E_CONTROLLER,
I2C_CTRL_E_DRIVER);
return;
}
ismt->ismt_req->smbr_rlen = buf[0];
bcopy(&buf[1], ismt->ismt_req->smbr_rdata, buf[0]);
break;
case SMBUS_OP_I2C_READ_BLOCK:
bcopy(buf, ismt->ismt_req->smbr_rdata,
ISMT_DESC_STS_GET_RDLEN(sts));
break;
case SMBUS_OP_WRITE_U32:
case SMBUS_OP_WRITE_U64:
case SMBUS_OP_READ_U32:
case SMBUS_OP_READ_U64:
case SMBUS_OP_HOST_NOTIFY:
default:
panic("programmer error: unsupported request type 0x%x should "
"not have been completed", ismt->ismt_req->smbr_op);
}
i2c_ctrl_io_success(ismt->ismt_err);
}
/*
* When we're using MSI interrupts then the hardware will automatically clear
* the controller's interrupt status register based on our configuration.
* However if we're using INTx, then we will need to take care of reading the
* various status registers and checking what has happened.
*
* One nice thing is that we'll otherwise always get an interrupt when the whole
* operation is done.
*/
static uint_t
ismt_intr(caddr_t arg1, caddr_t arg2)
{
ismt_t *ismt = (ismt_t *)arg1;
uint32_t msts;
bool mis, meis;
mutex_enter(&ismt->ismt_mutex);
if (ismt->ismt_itype == DDI_INTR_TYPE_FIXED) {
msts = ismt_read32(ismt, ISMT_R_MSTS);
mis = ISMT_R_MSTS_GET_MIS(msts);
meis = ISMT_R_MSTS_GET_MEIS(msts);
if (!mis && !meis) {
mutex_exit(&ismt->ismt_mutex);
return (DDI_INTR_UNCLAIMED);
}
ismt_write32(ismt, ISMT_R_MSTS, msts);
}
ismt_io(ismt);
ismt->ismt_req_done = true;
cv_signal(&ismt->ismt_cv);
mutex_exit(&ismt->ismt_mutex);
return (DDI_INTR_CLAIMED);
}
static void
ismt_wait(ismt_t *ismt)
{
VERIFY(MUTEX_HELD(&ismt->ismt_mutex));
VERIFY(ismt->ismt_req == NULL || ismt->ismt_i2creq == NULL);
VERIFY3P(ismt->ismt_req, !=, ismt->ismt_i2creq);
uint32_t to = i2c_ctrl_timeout_delay_us(ismt->ismt_hdl, I2C_CTRL_TO_IO);
clock_t abs = ddi_get_lbolt() + drv_usectohz(to);
while (!ismt->ismt_req_done) {
clock_t ret = cv_timedwait(&ismt->ismt_cv, &ismt->ismt_mutex,
abs);
if (ret == -1) {
break;
}
}
/*
* The command timed out. We need to set the KILL bit, complete the
* transaction, and go from there.
*/
if (!ismt->ismt_req_done) {
uint32_t val = ISMT_R_GCTRL_SET_KILL(0, 1);
ismt_write32(ismt, ISMT_R_GCTRL, val);
i2c_ctrl_io_error(ismt->ismt_err, I2C_CORE_E_CONTROLLER,
I2C_CTRL_E_REQ_TO);
ismt->ismt_req_done = true;
}
}
static void
ismt_io_reset(ismt_t *ismt)
{
VERIFY(MUTEX_HELD(&ismt->ismt_mutex));
bzero(ismt->ismt_data_dma.id_va, ISMT_DATA_BUF_SIZE);
bzero(ismt->ismt_icl_dma.id_va, ISMT_ICL_DMA_SIZE);
ismt->ismt_req = NULL;
ismt->ismt_i2creq = NULL;
ismt->ismt_err = NULL;
ismt->ismt_req_done = false;
}
/*
* Set the things that are common across all I/O requests: the address, the
* request for the fair bit, and ask for an interrupt. Hardware will ownly honor
* the bit if we're using MSIs and otherwise will always inject the interrupt,
* so we set this regardless.
*/
static void
ismt_io_cmd_init(const i2c_addr_t *addr, uint32_t *cmdp)
{
uint32_t cmd;
ASSERT3U(addr->ia_type, ==, I2C_ADDR_7BIT);
cmd = ISMT_DESC_CMD_SET_ADDR(0, addr->ia_addr);
cmd = ISMT_DESC_CMD_SET_INT(cmd, 1);
cmd = ISMT_DESC_CMD_SET_FAIR(cmd, 1);
*cmdp = cmd;
}
static void
ismt_io_cmd_submit(ismt_t *ismt, uint32_t cmd, bool data)
{
ismt_desc_t *desc;
VERIFY(MUTEX_HELD(&ismt->ismt_mutex));
desc = &ismt->ismt_ring[ismt->ismt_head];
bzero(desc, sizeof (desc));
desc->id_cmd_addr = LE_32(cmd);
if (data) {
const ddi_dma_cookie_t *c;
c = ddi_dma_cookie_one(ismt->ismt_data_dma.id_hdl);
desc->id_low = LE_32(bitx64(c->dmac_laddress, 31, 0));
desc->id_high = LE_32(bitx64(c->dmac_laddress, 63, 32));
ISMT_DMA_SYNC(ismt->ismt_data_dma, DDI_DMA_SYNC_FORDEV);
}
ISMT_DMA_SYNC(ismt->ismt_ring_dma, DDI_DMA_SYNC_FORDEV);
/*
* Proceed to tell hardware to process this command. The datasheet
* suggests we need to update the descriptor pointer and then come back
* and ask the hardware to start as we can't set SS until at least one
* descriptor has been programmed.
*/
ismt->ismt_head = (ismt->ismt_head + 1) % ISMT_RING_NENTS;
uint32_t mctrl = ismt_read32(ismt, ISMT_R_MCTRL);
mctrl = ISMT_R_MCTRL_SET_FMHP(mctrl, ismt->ismt_head);
ismt_write32(ismt, ISMT_R_MCTRL, mctrl);
mctrl = ISMT_R_MCTRL_SET_SS(mctrl, 1);
ismt_write32(ismt, ISMT_R_MCTRL, mctrl);
/*
* The command is running. We now need to wait for an interrupt or poll
* for completion. Unlike other drivers, we always have the interrupt
* enabled, which means we can't really poll. XXX is polling mode even
* worthwhile? We don't use it in the framework.
*/
ismt_wait(ismt);
}
static void
ismt_io_smbus(void *arg, uint32_t port, smbus_req_t *req)
{
ismt_t *ismt = arg;
uint8_t *buf;
uint32_t cmd;
bool data = false;
mutex_enter(&ismt->ismt_mutex);
ismt_io_reset(ismt);
ismt->ismt_req = req;
ismt->ismt_err = &req->smbr_error;
buf = (uint8_t *)ismt->ismt_data_dma.id_va;
/*
* Set up the descriptor. In particualr we need to determine whether to
* set:
*
* - The read address bit (default is write)
* - The block request bit
* - The C/WRL bit which determines whether the write field is the
* command.
* - The read and write length, if non-zero.
* - Whether we are going to use the data pointer and if we need to do
* anyhting to get it ready
*
* The read address bit and whether we use data are bools that we apply
* at the end.
*/
ismt_io_cmd_init(&req->smbr_addr, &cmd);
switch (req->smbr_op) {
case SMBUS_OP_QUICK_COMMAND:
if ((req->smbr_flags & I2C_IO_REQ_F_QUICK_WRITE) != 0) {
cmd = ISMT_DESC_CMD_SET_RW(cmd, ISMT_DESC_CMD_RW_WRITE);
} else {
cmd = ISMT_DESC_CMD_SET_RW(cmd, ISMT_DESC_CMD_RW_READ);
}
break;
case SMBUS_OP_SEND_BYTE:
cmd = ISMT_DESC_CMD_SET_RW(cmd, ISMT_DESC_CMD_RW_WRITE);
cmd = ISMT_DESC_CMD_SET_CWRL(cmd, 1);
cmd = ISMT_DESC_CMD_SET_WRLEN(cmd, req->smbr_wdata[0]);
break;
case SMBUS_OP_WRITE_BYTE:
cmd = ISMT_DESC_CMD_SET_RW(cmd, ISMT_DESC_CMD_RW_WRITE);
cmd = ISMT_DESC_CMD_SET_WRLEN(cmd, 2);
data = true;
buf[0] = req->smbr_cmd;
buf[1] = req->smbr_wdata[0];
break;
case SMBUS_OP_WRITE_WORD:
cmd = ISMT_DESC_CMD_SET_RW(cmd, ISMT_DESC_CMD_RW_WRITE);
cmd = ISMT_DESC_CMD_SET_WRLEN(cmd, 3);
data = true;
buf[0] = req->smbr_cmd;
buf[1] = req->smbr_wdata[0];
buf[2] = req->smbr_wdata[1];
break;
case SMBUS_OP_WRITE_BLOCK:
cmd = ISMT_DESC_CMD_SET_RW(cmd, ISMT_DESC_CMD_RW_WRITE);
cmd = ISMT_DESC_CMD_SET_BLK(cmd, 1);
VERIFY3U(req->smbr_wlen, <=, ISMT_MAX_SMBUS);
cmd = ISMT_DESC_CMD_SET_WRLEN(cmd, req->smbr_wlen + 1);
data = true;
buf[0] = req->smbr_cmd;
bcopy(req->smbr_wdata, &buf[1], req->smbr_wlen);
break;
case SMBUS_OP_I2C_WRITE_BLOCK:
cmd = ISMT_DESC_CMD_SET_RW(cmd, ISMT_DESC_CMD_RW_WRITE);
cmd = ISMT_DESC_CMD_SET_I2C(cmd, 1);
VERIFY3U(req->smbr_wlen, >, 0);
VERIFY3U(req->smbr_wlen, <=, ISMT_MAX_I2C);
cmd = ISMT_DESC_CMD_SET_WRLEN(cmd, req->smbr_wlen + 1);
data = true;
buf[0] = req->smbr_cmd;
bcopy(req->smbr_wdata, &buf[1], req->smbr_wlen);
break;
case SMBUS_OP_RECV_BYTE:
cmd = ISMT_DESC_CMD_SET_RW(cmd, ISMT_DESC_CMD_RW_READ);
cmd = ISMT_DESC_CMD_SET_RDLEN(cmd, 1);
data = true;
break;
case SMBUS_OP_READ_BYTE:
cmd = ISMT_DESC_CMD_SET_RW(cmd, ISMT_DESC_CMD_RW_READ);
cmd = ISMT_DESC_CMD_SET_CWRL(cmd, 1);
cmd = ISMT_DESC_CMD_SET_WRLEN(cmd, req->smbr_cmd);
cmd = ISMT_DESC_CMD_SET_RDLEN(cmd, 1);
data = true;
break;
case SMBUS_OP_READ_WORD:
cmd = ISMT_DESC_CMD_SET_RW(cmd, ISMT_DESC_CMD_RW_READ);
cmd = ISMT_DESC_CMD_SET_CWRL(cmd, 1);
cmd = ISMT_DESC_CMD_SET_WRLEN(cmd, req->smbr_cmd);
cmd = ISMT_DESC_CMD_SET_RDLEN(cmd, 2);
data = true;
break;
case SMBUS_OP_READ_BLOCK:
cmd = ISMT_DESC_CMD_SET_RW(cmd, ISMT_DESC_CMD_RW_READ);
cmd = ISMT_DESC_CMD_SET_BLK(cmd, 1);
cmd = ISMT_DESC_CMD_SET_CWRL(cmd, 1);
cmd = ISMT_DESC_CMD_SET_WRLEN(cmd, req->smbr_cmd);
VERIFY3U(req->smbr_rlen, >, 0);
VERIFY3U(req->smbr_rlen, <=, ISMT_MAX_SMBUS);
cmd = ISMT_DESC_CMD_SET_RDLEN(cmd, req->smbr_rlen + 1);
data = true;
break;
case SMBUS_OP_I2C_READ_BLOCK:
cmd = ISMT_DESC_CMD_SET_RW(cmd, ISMT_DESC_CMD_RW_READ);
cmd = ISMT_DESC_CMD_SET_I2C(cmd, 1);
cmd = ISMT_DESC_CMD_SET_CWRL(cmd, 1);
cmd = ISMT_DESC_CMD_SET_WRLEN(cmd, req->smbr_cmd);
VERIFY3U(req->smbr_rlen, >, 0);
VERIFY3U(req->smbr_rlen, <=, ISMT_MAX_I2C);
cmd = ISMT_DESC_CMD_SET_RDLEN(cmd, req->smbr_rlen);
data = true;
break;
case SMBUS_OP_PROCESS_CALL:
cmd = ISMT_DESC_CMD_SET_RW(cmd, ISMT_DESC_CMD_RW_WRITE);
cmd = ISMT_DESC_CMD_SET_WRLEN(cmd, 3);
cmd = ISMT_DESC_CMD_SET_RDLEN(cmd, 2);
data = true;
buf[0] = req->smbr_cmd;
buf[1] = req->smbr_wdata[0];
buf[2] = req->smbr_wdata[1];
break;
case SMBUS_OP_BLOCK_PROCESS_CALL:
cmd = ISMT_DESC_CMD_SET_RW(cmd, ISMT_DESC_CMD_RW_WRITE);
cmd = ISMT_DESC_CMD_SET_WRLEN(cmd, req->smbr_wlen + 1);
cmd = ISMT_DESC_CMD_SET_RDLEN(cmd, req->smbr_rlen + 1);
data = true;
buf[0] = req->smbr_cmd;
bcopy(req->smbr_wdata, &buf[1], req->smbr_wlen);
break;
/*
* As the datasheets don't have a way to directly run the U32/U64
* operations, we allow our translation layer to take care of it.
*/
case SMBUS_OP_READ_U32:
case SMBUS_OP_READ_U64:
case SMBUS_OP_WRITE_U32:
case SMBUS_OP_WRITE_U64:
case SMBUS_OP_HOST_NOTIFY:
default:
dev_err(ismt->ismt_dip, CE_WARN, "!framework passed "
"unsupported SMBus command 0x%x", req->smbr_op);
i2c_ctrl_io_error(&req->smbr_error, I2C_CORE_E_CONTROLLER,
I2C_CTRL_E_UNSUP_CMD);
goto done;
}
ismt_io_cmd_submit(ismt, cmd, data);
done:
ismt->ismt_req = NULL;
ismt->ismt_i2creq = NULL;
ismt->ismt_err = NULL;
ismt->ismt_req_done = false;
mutex_exit(&ismt->ismt_mutex);
}
static void
ismt_io_i2c(void *arg, uint32_t port, i2c_req_t *req)
{
ismt_t *ismt = arg;
uint8_t *buf;
uint32_t cmd;
bool data = false;
mutex_enter(&ismt->ismt_mutex);
ismt_io_reset(ismt);
ismt->ismt_i2creq = req;
ismt->ismt_err = &req->ir_error;
buf = (uint8_t *)ismt->ismt_data_dma.id_va;
/*
* I2C Commands are required to always set the I2C bit and we must never
* set the block bit. The remaining flags and set up depend on whether
* we're doing a write, a read, or a write folowed by a read.
*/
ismt_io_cmd_init(&req->ir_addr, &cmd);
cmd = ISMT_DESC_CMD_SET_I2C(cmd, 1);
cmd = ISMT_DESC_CMD_SET_BLK(cmd, 0);
if (req->ir_rlen > 0) {
cmd = ISMT_DESC_CMD_SET_RW(cmd, ISMT_DESC_CMD_RW_READ);
VERIFY3U(req->ir_rlen, <, ISMT_MAX_I2C);
data = true;
cmd = ISMT_DESC_CMD_SET_RDLEN(cmd, req->ir_rlen);
}
if (req->ir_wlen > 0) {
cmd = ISMT_DESC_CMD_SET_RW(cmd, ISMT_DESC_CMD_RW_WRITE);
/*
* The datasheet tells us that if we have a 1 byte write, we
* need to set C/WRL and it will encode the data byte. Otherwise
* we use the normal write length.
*/
if (req->ir_wlen == 1) {
cmd = ISMT_DESC_CMD_SET_CWRL(cmd, 1);
cmd = ISMT_DESC_CMD_SET_WRLEN(cmd, req->ir_wdata[0]);
} else {
VERIFY3U(req->ir_wlen, >, 0);
VERIFY3U(req->ir_wlen, <, ISMT_MAX_I2C);
cmd = ISMT_DESC_CMD_SET_WRLEN(cmd, req->ir_wlen);
data = true;
bcopy(req->ir_wdata, buf, req->ir_wlen);
}
}
ismt_io_cmd_submit(ismt, cmd, data);
ismt->ismt_req = NULL;
ismt->ismt_i2creq = NULL;
ismt->ismt_req_done = false;
mutex_exit(&ismt->ismt_mutex);
}
static const i2c_ctrl_ops_t ismt_ctrl_ops = {
.i2c_port_name_f = i2c_ctrl_port_name_portno,
.i2c_io_i2c_f = ismt_io_i2c,
.i2c_io_smbus_f = ismt_io_smbus,
.i2c_prop_info_f = ismt_prop_info,
.i2c_prop_get_f = ismt_prop_get
};
static bool
ismt_setup_regs(ismt_t *ismt)
{
int ret;
ddi_device_acc_attr_t attr;
bzero(&attr, sizeof (attr));
attr.devacc_attr_version = DDI_DEVICE_ATTR_V1;
attr.devacc_attr_endian_flags = DDI_STRUCTURE_LE_ACC;
attr.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
attr.devacc_attr_access = DDI_DEFAULT_ACC;
if (ddi_dev_regsize(ismt->ismt_dip, ISMT_REGNO, &ismt->ismt_regsize) !=
DDI_SUCCESS) {
dev_err(ismt->ismt_dip, CE_WARN, "failed to get regs[%u] size",
ISMT_REGNO);
return (false);
}
ret = ddi_regs_map_setup(ismt->ismt_dip, ISMT_REGNO, &ismt->ismt_base,
0, ismt->ismt_regsize, &attr, &ismt->ismt_regs);
if (ret != DDI_SUCCESS) {
dev_err(ismt->ismt_dip, CE_WARN, "failed to map regs[%u]: %u",
ISMT_REGNO, ret);
return (false);
}
ismt->ismt_init |= ISMT_INIT_REGS;
return (true);
}
static void
ismt_dma_free(ismt_dma_t *dma)
{
/* Proxy for DMA handle bound */
if (dma->id_size != 0) {
(void) ddi_dma_unbind_handle(dma->id_hdl);
dma->id_size = 0;
}
if (dma->id_acc != NULL) {
ddi_dma_mem_free(&dma->id_acc);
dma->id_acc = NULL;
dma->id_va = NULL;
dma->id_alloc_len = 0;
}
if (dma->id_hdl != NULL) {
ddi_dma_free_handle(&dma->id_hdl);
dma->id_hdl = NULL;
}
ASSERT0(dma->id_size);
ASSERT0(dma->id_alloc_len);
ASSERT3P(dma->id_acc, ==, NULL);
ASSERT3P(dma->id_hdl, ==, NULL);
ASSERT3P(dma->id_va, ==, NULL);
}
static bool
ismt_dma_alloc(ismt_t *ismt, ismt_dma_t *dma, size_t size)
{
int ret;
ddi_device_acc_attr_t acc;
uint_t flags = DDI_DMA_CONSISTENT;
bzero(dma, sizeof (ismt_dma_t));
ret = ddi_dma_alloc_handle(ismt->ismt_dip, &ismt_dma_attr,
DDI_DMA_SLEEP, NULL, &dma->id_hdl);
if (ret != DDI_SUCCESS) {
dev_err(ismt->ismt_dip, CE_WARN, "!failed to allocate DMA "
"handle: %d", ret);
return (false);
}
bzero(&acc, sizeof (acc));
acc.devacc_attr_version = DDI_DEVICE_ATTR_V1;
acc.devacc_attr_endian_flags = DDI_NEVERSWAP_ACC;
acc.devacc_attr_dataorder = DDI_STRICTORDER_ACC;
acc.devacc_attr_access = DDI_DEFAULT_ACC;
ret = ddi_dma_mem_alloc(dma->id_hdl, size, &acc, flags,
DDI_DMA_SLEEP, NULL, &dma->id_va, &dma->id_alloc_len,
&dma->id_acc);
if (ret != DDI_SUCCESS) {
dev_err(ismt->ismt_dip, CE_WARN, "!failed to allocate %lu "
"bytes of DMA memory: %d", size, ret);
ismt_dma_free(dma);
return (false);
}
bzero(dma->id_va, dma->id_alloc_len);
ret = ddi_dma_addr_bind_handle(dma->id_hdl, NULL, dma->id_va,
dma->id_alloc_len, DDI_DMA_RDWR | flags, DDI_DMA_DONTWAIT, NULL,
NULL, NULL);
if (ret != DDI_SUCCESS) {
dev_err(ismt->ismt_dip, CE_WARN, "!failed to bind %lu bytes of "
"DMA memory: %d", dma->id_alloc_len, ret);
ismt_dma_free(dma);
return (false);
}
dma->id_size = size;
return (true);
}
static bool
ismt_alloc_intr(ismt_t *ismt)
{
int ret, types;
ret = ddi_intr_get_supported_types(ismt->ismt_dip, &types);
if (ret != DDI_SUCCESS) {
dev_err(ismt->ismt_dip, CE_WARN, "failed to get supproted "
"interrupt types: 0x%d", ret);
return (false);
}
/*
* We only expect hardware to support MSIs and INTx.
*/
if ((types & DDI_INTR_TYPE_MSI) != 0) {
ret = ddi_intr_alloc(ismt->ismt_dip, &ismt->ismt_intr_hdl,
DDI_INTR_TYPE_MSI, 0, 1, &ismt->ismt_nintrs,
DDI_INTR_ALLOC_STRICT);
if (ret == DDI_SUCCESS) {
ismt->ismt_itype = DDI_INTR_TYPE_MSI;
return (true);
}
dev_err(ismt->ismt_dip, CE_WARN, "!failed to allocate MSI "
"interrupt");
}
if ((types & DDI_INTR_TYPE_FIXED) != 0) {
ret = ddi_intr_alloc(ismt->ismt_dip, &ismt->ismt_intr_hdl,
DDI_INTR_TYPE_MSI, 0, 1, &ismt->ismt_nintrs,
DDI_INTR_ALLOC_STRICT);
if (ret == DDI_SUCCESS) {
ismt->ismt_itype = DDI_INTR_TYPE_FIXED;
return (true);
}
dev_err(ismt->ismt_dip, CE_WARN, "!failed to allocate INTx "
"interrupt");
}
dev_err(ismt->ismt_dip, CE_WARN, "failed to allocate any interrupts "
"from type 0x%x", types);
return (false);
}
static bool
ismt_setup_intr(ismt_t *ismt)
{
int ret = ddi_intr_add_handler(ismt->ismt_intr_hdl, ismt_intr, ismt,
NULL);
if (ret != DDI_SUCCESS) {
dev_err(ismt->ismt_dip, CE_WARN, "failed to add interrupt "
"handler: 0x%x", ret);
return (false);
}
ismt->ismt_init |= ISMT_INIT_INTR_HDL;
ret = ddi_intr_get_pri(ismt->ismt_intr_hdl, &ismt->ismt_intr_pri);
if (ret != DDI_SUCCESS) {
dev_err(ismt->ismt_dip, CE_WARN, "failed to get interrupt "
"priority");
return (false);
}
return (true);
}
/*
* Go through and set up hardware for use. We need to explicitly take care of:
*
* - The Interrupt Cause Log
* - The Controller DMA ring
* - Firmware Head and Tail Registers
* - Enabling generation of interrupts
*
* We use the hardwar defaults for the device retry policy.
*/
static void
ismt_ctrl_init(ismt_t *ismt)
{
uint32_t val;
const ddi_dma_cookie_t *c;
c = ddi_dma_cookie_one(ismt->ismt_icl_dma.id_hdl);
ismt_write64(ismt, ISMT_R_SMTICL, c->dmac_laddress);
c = ddi_dma_cookie_one(ismt->ismt_ring_dma.id_hdl);
ismt_write64(ismt, ISMT_R_MDBA, c->dmac_laddress);
val = ISMT_R_MDS_SET_SIZE(0, ISMT_RING_NENTS - 1);
ismt_write32(ismt, ISMT_R_MDS, val);
val = ISMT_R_MCTRL_SET_FMHP(0, 0);
val = ISMT_R_MCTRL_SET_MEIE(val, 1);
ismt_write32(ismt, ISMT_R_MCTRL, val);
val = ISMT_R_MSTS_SET_HMTP(0, 0);
ismt_write32(ismt, ISMT_R_MSTS, val);
ismt->ismt_head = ismt->ismt_tail = 0;
ismt->ismt_ring = (ismt_desc_t *)ismt->ismt_ring_dma.id_va;
val = ismt_read32(ismt, ISMT_R_SPGT);
switch (ISMT_R_SPGT_GET_SPD(val)) {
case ISMT_R_SPT_SPD_80K:
case ISMT_R_SPT_SPD_100K:
ismt->ismt_speed = I2C_SPEED_STD;
break;
case ISMT_R_SPT_SPD_400K:
ismt->ismt_speed = I2C_SPEED_FAST;
break;
case ISMT_R_SPT_SPD_1M:
ismt->ismt_speed = I2C_SPEED_FPLUS;
break;
}
}
static bool
ismt_enable_intr(ismt_t *ismt)
{
int ret = ddi_intr_enable(ismt->ismt_intr_hdl);
if (ret != DDI_SUCCESS) {
dev_err(ismt->ismt_dip, CE_WARN, "failed to enable interrupt "
"handler: %d", ret);
return (false);
}
ismt->ismt_init |= ISMT_INIT_INTR_EN;
return (true);
}
static bool
ismt_register(ismt_t *ismt)
{
i2c_ctrl_reg_error_t ret;
i2c_ctrl_register_t *reg;
ret = i2c_ctrl_register_alloc(I2C_CTRL_PROVIDER, ®);
if (ret != 0) {
dev_err(ismt->ismt_dip, CE_WARN, "failed to allocate i2c "
"controller registration structure: 0x%x", ret);
return (false);
}
reg->ic_type = I2C_CTRL_TYPE_SMBUS;
reg->ic_nports = 1;
reg->ic_dip = ismt->ismt_dip;
reg->ic_drv = ismt;
reg->ic_ops = &ismt_ctrl_ops;