1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
3 * AMD MP2 PCIe communication driver
5 * Authors: Shyam Sundar S K <Shyam-sundar.S-k@amd.com>
6 * Elie Morisse <syniurge@gmail.com>
9 #include <linux/dma-mapping.h>
10 #include <linux/interrupt.h>
11 #include <linux/module.h>
12 #include <linux/pci.h>
13 #include <linux/slab.h>
15 #include "i2c-amd-mp2.h"
17 #include <linux/io-64-nonatomic-lo-hi.h>
19 static void amd_mp2_c2p_mutex_lock(struct amd_i2c_common
*i2c_common
)
21 struct amd_mp2_dev
*privdata
= i2c_common
->mp2_dev
;
23 /* there is only one data mailbox for two i2c adapters */
24 mutex_lock(&privdata
->c2p_lock
);
25 privdata
->c2p_lock_busid
= i2c_common
->bus_id
;
28 static void amd_mp2_c2p_mutex_unlock(struct amd_i2c_common
*i2c_common
)
30 struct amd_mp2_dev
*privdata
= i2c_common
->mp2_dev
;
32 if (unlikely(privdata
->c2p_lock_busid
!= i2c_common
->bus_id
)) {
33 dev_warn(ndev_dev(privdata
),
34 "bus %d attempting to unlock C2P locked by bus %d\n",
35 i2c_common
->bus_id
, privdata
->c2p_lock_busid
);
39 mutex_unlock(&privdata
->c2p_lock
);
42 static int amd_mp2_cmd(struct amd_i2c_common
*i2c_common
,
43 union i2c_cmd_base i2c_cmd_base
)
45 struct amd_mp2_dev
*privdata
= i2c_common
->mp2_dev
;
48 i2c_common
->reqcmd
= i2c_cmd_base
.s
.i2c_cmd
;
50 reg
= privdata
->mmio
+ ((i2c_cmd_base
.s
.bus_id
== 1) ?
51 AMD_C2P_MSG1
: AMD_C2P_MSG0
);
52 writel(i2c_cmd_base
.ul
, reg
);
57 int amd_mp2_bus_enable_set(struct amd_i2c_common
*i2c_common
, bool enable
)
59 struct amd_mp2_dev
*privdata
= i2c_common
->mp2_dev
;
60 union i2c_cmd_base i2c_cmd_base
;
62 dev_dbg(ndev_dev(privdata
), "%s id: %d\n", __func__
,
66 i2c_cmd_base
.s
.i2c_cmd
= enable
? i2c_enable
: i2c_disable
;
67 i2c_cmd_base
.s
.bus_id
= i2c_common
->bus_id
;
68 i2c_cmd_base
.s
.i2c_speed
= i2c_common
->i2c_speed
;
70 amd_mp2_c2p_mutex_lock(i2c_common
);
72 return amd_mp2_cmd(i2c_common
, i2c_cmd_base
);
74 EXPORT_SYMBOL_GPL(amd_mp2_bus_enable_set
);
76 static void amd_mp2_cmd_rw_fill(struct amd_i2c_common
*i2c_common
,
77 union i2c_cmd_base
*i2c_cmd_base
,
80 i2c_cmd_base
->s
.i2c_cmd
= reqcmd
;
81 i2c_cmd_base
->s
.bus_id
= i2c_common
->bus_id
;
82 i2c_cmd_base
->s
.i2c_speed
= i2c_common
->i2c_speed
;
83 i2c_cmd_base
->s
.slave_addr
= i2c_common
->msg
->addr
;
84 i2c_cmd_base
->s
.length
= i2c_common
->msg
->len
;
87 int amd_mp2_rw(struct amd_i2c_common
*i2c_common
, enum i2c_cmd reqcmd
)
89 struct amd_mp2_dev
*privdata
= i2c_common
->mp2_dev
;
90 union i2c_cmd_base i2c_cmd_base
;
92 amd_mp2_cmd_rw_fill(i2c_common
, &i2c_cmd_base
, reqcmd
);
93 amd_mp2_c2p_mutex_lock(i2c_common
);
95 if (i2c_common
->msg
->len
<= 32) {
96 i2c_cmd_base
.s
.mem_type
= use_c2pmsg
;
97 if (reqcmd
== i2c_write
)
98 memcpy_toio(privdata
->mmio
+ AMD_C2P_MSG2
,
100 i2c_common
->msg
->len
);
102 i2c_cmd_base
.s
.mem_type
= use_dram
;
103 writeq((u64
)i2c_common
->dma_addr
,
104 privdata
->mmio
+ AMD_C2P_MSG2
);
107 return amd_mp2_cmd(i2c_common
, i2c_cmd_base
);
109 EXPORT_SYMBOL_GPL(amd_mp2_rw
);
111 static void amd_mp2_pci_check_rw_event(struct amd_i2c_common
*i2c_common
)
113 struct amd_mp2_dev
*privdata
= i2c_common
->mp2_dev
;
114 int len
= i2c_common
->eventval
.r
.length
;
115 u32 slave_addr
= i2c_common
->eventval
.r
.slave_addr
;
118 if (unlikely(len
!= i2c_common
->msg
->len
)) {
119 dev_err(ndev_dev(privdata
),
120 "length %d in event doesn't match buffer length %d!\n",
121 len
, i2c_common
->msg
->len
);
125 if (unlikely(slave_addr
!= i2c_common
->msg
->addr
)) {
126 dev_err(ndev_dev(privdata
),
127 "unexpected slave address %x (expected: %x)!\n",
128 slave_addr
, i2c_common
->msg
->addr
);
133 i2c_common
->cmd_success
= true;
136 static void __amd_mp2_process_event(struct amd_i2c_common
*i2c_common
)
138 struct amd_mp2_dev
*privdata
= i2c_common
->mp2_dev
;
139 enum status_type sts
= i2c_common
->eventval
.r
.status
;
140 enum response_type res
= i2c_common
->eventval
.r
.response
;
141 int len
= i2c_common
->eventval
.r
.length
;
143 if (res
!= command_success
) {
144 if (res
!= command_failed
)
145 dev_err(ndev_dev(privdata
), "invalid response to i2c command!\n");
149 switch (i2c_common
->reqcmd
) {
151 if (sts
== i2c_readcomplete_event
) {
152 amd_mp2_pci_check_rw_event(i2c_common
);
154 memcpy_fromio(i2c_common
->msg
->buf
,
155 privdata
->mmio
+ AMD_C2P_MSG2
,
157 } else if (sts
!= i2c_readfail_event
) {
158 dev_err(ndev_dev(privdata
),
159 "invalid i2c status after read (%d)!\n", sts
);
163 if (sts
== i2c_writecomplete_event
)
164 amd_mp2_pci_check_rw_event(i2c_common
);
165 else if (sts
!= i2c_writefail_event
)
166 dev_err(ndev_dev(privdata
),
167 "invalid i2c status after write (%d)!\n", sts
);
170 if (sts
== i2c_busenable_complete
)
171 i2c_common
->cmd_success
= true;
172 else if (sts
!= i2c_busenable_failed
)
173 dev_err(ndev_dev(privdata
),
174 "invalid i2c status after bus enable (%d)!\n",
178 if (sts
== i2c_busdisable_complete
)
179 i2c_common
->cmd_success
= true;
180 else if (sts
!= i2c_busdisable_failed
)
181 dev_err(ndev_dev(privdata
),
182 "invalid i2c status after bus disable (%d)!\n",
190 void amd_mp2_process_event(struct amd_i2c_common
*i2c_common
)
192 struct amd_mp2_dev
*privdata
= i2c_common
->mp2_dev
;
194 if (unlikely(i2c_common
->reqcmd
== i2c_none
)) {
195 dev_warn(ndev_dev(privdata
),
196 "received msg but no cmd was sent (bus = %d)!\n",
201 __amd_mp2_process_event(i2c_common
);
203 i2c_common
->reqcmd
= i2c_none
;
204 amd_mp2_c2p_mutex_unlock(i2c_common
);
206 EXPORT_SYMBOL_GPL(amd_mp2_process_event
);
208 static irqreturn_t
amd_mp2_irq_isr(int irq
, void *dev
)
210 struct amd_mp2_dev
*privdata
= dev
;
211 struct amd_i2c_common
*i2c_common
;
215 enum irqreturn ret
= IRQ_NONE
;
217 for (bus_id
= 0; bus_id
< 2; bus_id
++) {
218 i2c_common
= privdata
->busses
[bus_id
];
222 reg
= privdata
->mmio
+ ((bus_id
== 0) ?
223 AMD_P2C_MSG1
: AMD_P2C_MSG2
);
227 writel(0, privdata
->mmio
+ AMD_P2C_MSG_INTEN
);
228 i2c_common
->eventval
.ul
= val
;
229 i2c_common
->cmd_completion(i2c_common
);
235 if (ret
!= IRQ_HANDLED
) {
236 val
= readl(privdata
->mmio
+ AMD_P2C_MSG_INTEN
);
238 writel(0, privdata
->mmio
+ AMD_P2C_MSG_INTEN
);
239 dev_warn(ndev_dev(privdata
),
240 "received irq without message\n");
248 void amd_mp2_rw_timeout(struct amd_i2c_common
*i2c_common
)
250 i2c_common
->reqcmd
= i2c_none
;
251 amd_mp2_c2p_mutex_unlock(i2c_common
);
253 EXPORT_SYMBOL_GPL(amd_mp2_rw_timeout
);
255 int amd_mp2_register_cb(struct amd_i2c_common
*i2c_common
)
257 struct amd_mp2_dev
*privdata
= i2c_common
->mp2_dev
;
259 if (i2c_common
->bus_id
> 1)
262 if (privdata
->busses
[i2c_common
->bus_id
]) {
263 dev_err(ndev_dev(privdata
),
264 "Bus %d already taken!\n", i2c_common
->bus_id
);
268 privdata
->busses
[i2c_common
->bus_id
] = i2c_common
;
272 EXPORT_SYMBOL_GPL(amd_mp2_register_cb
);
274 int amd_mp2_unregister_cb(struct amd_i2c_common
*i2c_common
)
276 struct amd_mp2_dev
*privdata
= i2c_common
->mp2_dev
;
278 privdata
->busses
[i2c_common
->bus_id
] = NULL
;
282 EXPORT_SYMBOL_GPL(amd_mp2_unregister_cb
);
284 static void amd_mp2_clear_reg(struct amd_mp2_dev
*privdata
)
288 for (reg
= AMD_C2P_MSG0
; reg
<= AMD_C2P_MSG9
; reg
+= 4)
289 writel(0, privdata
->mmio
+ reg
);
291 for (reg
= AMD_P2C_MSG1
; reg
<= AMD_P2C_MSG2
; reg
+= 4)
292 writel(0, privdata
->mmio
+ reg
);
295 static int amd_mp2_pci_init(struct amd_mp2_dev
*privdata
,
296 struct pci_dev
*pci_dev
)
300 pci_set_drvdata(pci_dev
, privdata
);
302 rc
= pcim_enable_device(pci_dev
);
304 dev_err(ndev_dev(privdata
), "Failed to enable MP2 PCI device\n");
308 rc
= pcim_iomap_regions(pci_dev
, 1 << 2, pci_name(pci_dev
));
310 dev_err(ndev_dev(privdata
), "I/O memory remapping failed\n");
313 privdata
->mmio
= pcim_iomap_table(pci_dev
)[2];
315 pci_set_master(pci_dev
);
317 rc
= pci_set_dma_mask(pci_dev
, DMA_BIT_MASK(64));
319 rc
= pci_set_dma_mask(pci_dev
, DMA_BIT_MASK(32));
324 /* Set up intx irq */
325 writel(0, privdata
->mmio
+ AMD_P2C_MSG_INTEN
);
326 pci_intx(pci_dev
, 1);
327 rc
= devm_request_irq(&pci_dev
->dev
, pci_dev
->irq
, amd_mp2_irq_isr
,
328 IRQF_SHARED
, dev_name(&pci_dev
->dev
), privdata
);
330 dev_err(&pci_dev
->dev
, "Failure requesting irq %i: %d\n",
336 pci_clear_master(pci_dev
);
338 pci_set_drvdata(pci_dev
, NULL
);
342 static int amd_mp2_pci_probe(struct pci_dev
*pci_dev
,
343 const struct pci_device_id
*id
)
345 struct amd_mp2_dev
*privdata
;
348 privdata
= devm_kzalloc(&pci_dev
->dev
, sizeof(*privdata
), GFP_KERNEL
);
352 privdata
->pci_dev
= pci_dev
;
353 rc
= amd_mp2_pci_init(privdata
, pci_dev
);
357 mutex_init(&privdata
->c2p_lock
);
359 pm_runtime_set_autosuspend_delay(&pci_dev
->dev
, 1000);
360 pm_runtime_use_autosuspend(&pci_dev
->dev
);
361 pm_runtime_put_autosuspend(&pci_dev
->dev
);
362 pm_runtime_allow(&pci_dev
->dev
);
364 privdata
->probed
= true;
366 dev_info(&pci_dev
->dev
, "MP2 device registered.\n");
370 static void amd_mp2_pci_remove(struct pci_dev
*pci_dev
)
372 struct amd_mp2_dev
*privdata
= pci_get_drvdata(pci_dev
);
374 pm_runtime_forbid(&pci_dev
->dev
);
375 pm_runtime_get_noresume(&pci_dev
->dev
);
377 pci_intx(pci_dev
, 0);
378 pci_clear_master(pci_dev
);
380 amd_mp2_clear_reg(privdata
);
384 static int amd_mp2_pci_suspend(struct device
*dev
)
386 struct pci_dev
*pci_dev
= to_pci_dev(dev
);
387 struct amd_mp2_dev
*privdata
= pci_get_drvdata(pci_dev
);
388 struct amd_i2c_common
*i2c_common
;
392 for (bus_id
= 0; bus_id
< 2; bus_id
++) {
393 i2c_common
= privdata
->busses
[bus_id
];
395 i2c_common
->suspend(i2c_common
);
398 ret
= pci_save_state(pci_dev
);
400 dev_err(ndev_dev(privdata
),
401 "pci_save_state failed = %d\n", ret
);
405 pci_disable_device(pci_dev
);
409 static int amd_mp2_pci_resume(struct device
*dev
)
411 struct pci_dev
*pci_dev
= to_pci_dev(dev
);
412 struct amd_mp2_dev
*privdata
= pci_get_drvdata(pci_dev
);
413 struct amd_i2c_common
*i2c_common
;
417 pci_restore_state(pci_dev
);
418 ret
= pci_enable_device(pci_dev
);
420 dev_err(ndev_dev(privdata
),
421 "pci_enable_device failed = %d\n", ret
);
425 for (bus_id
= 0; bus_id
< 2; bus_id
++) {
426 i2c_common
= privdata
->busses
[bus_id
];
428 ret
= i2c_common
->resume(i2c_common
);
437 static UNIVERSAL_DEV_PM_OPS(amd_mp2_pci_pm_ops
, amd_mp2_pci_suspend
,
438 amd_mp2_pci_resume
, NULL
);
439 #endif /* CONFIG_PM */
441 static const struct pci_device_id amd_mp2_pci_tbl
[] = {
442 {PCI_VDEVICE(AMD
, PCI_DEVICE_ID_AMD_MP2
)},
445 MODULE_DEVICE_TABLE(pci
, amd_mp2_pci_tbl
);
447 static struct pci_driver amd_mp2_pci_driver
= {
448 .name
= "i2c_amd_mp2",
449 .id_table
= amd_mp2_pci_tbl
,
450 .probe
= amd_mp2_pci_probe
,
451 .remove
= amd_mp2_pci_remove
,
454 .pm
= &amd_mp2_pci_pm_ops
,
458 module_pci_driver(amd_mp2_pci_driver
);
460 struct amd_mp2_dev
*amd_mp2_find_device(void)
463 struct pci_dev
*pci_dev
;
465 dev
= driver_find_next_device(&amd_mp2_pci_driver
.driver
, NULL
);
469 pci_dev
= to_pci_dev(dev
);
470 return (struct amd_mp2_dev
*)pci_get_drvdata(pci_dev
);
472 EXPORT_SYMBOL_GPL(amd_mp2_find_device
);
474 MODULE_DESCRIPTION("AMD(R) PCI-E MP2 I2C Controller Driver");
475 MODULE_AUTHOR("Shyam Sundar S K <Shyam-sundar.S-k@amd.com>");
476 MODULE_AUTHOR("Elie Morisse <syniurge@gmail.com>");
477 MODULE_LICENSE("Dual BSD/GPL");