gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / edac / amd8131_edac.c
blob93c82bc17493ae073791b96de7874b7e9b62fe95
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * amd8131_edac.c, AMD8131 hypertransport chip EDAC kernel module
5 * Copyright (c) 2008 Wind River Systems, Inc.
7 * Authors: Cao Qingtao <qingtao.cao@windriver.com>
8 * Benjamin Walsh <benjamin.walsh@windriver.com>
9 * Hu Yongqi <yongqi.hu@windriver.com>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/interrupt.h>
15 #include <linux/io.h>
16 #include <linux/bitops.h>
17 #include <linux/edac.h>
18 #include <linux/pci_ids.h>
20 #include "edac_module.h"
21 #include "amd8131_edac.h"
23 #define AMD8131_EDAC_REVISION " Ver: 1.0.0"
24 #define AMD8131_EDAC_MOD_STR "amd8131_edac"
26 /* Wrapper functions for accessing PCI configuration space */
27 static void edac_pci_read_dword(struct pci_dev *dev, int reg, u32 *val32)
29 int ret;
31 ret = pci_read_config_dword(dev, reg, val32);
32 if (ret != 0)
33 printk(KERN_ERR AMD8131_EDAC_MOD_STR
34 " PCI Access Read Error at 0x%x\n", reg);
37 static void edac_pci_write_dword(struct pci_dev *dev, int reg, u32 val32)
39 int ret;
41 ret = pci_write_config_dword(dev, reg, val32);
42 if (ret != 0)
43 printk(KERN_ERR AMD8131_EDAC_MOD_STR
44 " PCI Access Write Error at 0x%x\n", reg);
47 static char * const bridge_str[] = {
48 [NORTH_A] = "NORTH A",
49 [NORTH_B] = "NORTH B",
50 [SOUTH_A] = "SOUTH A",
51 [SOUTH_B] = "SOUTH B",
52 [NO_BRIDGE] = "NO BRIDGE",
55 /* Support up to two AMD8131 chipsets on a platform */
56 static struct amd8131_dev_info amd8131_devices[] = {
58 .inst = NORTH_A,
59 .devfn = DEVFN_PCIX_BRIDGE_NORTH_A,
60 .ctl_name = "AMD8131_PCIX_NORTH_A",
63 .inst = NORTH_B,
64 .devfn = DEVFN_PCIX_BRIDGE_NORTH_B,
65 .ctl_name = "AMD8131_PCIX_NORTH_B",
68 .inst = SOUTH_A,
69 .devfn = DEVFN_PCIX_BRIDGE_SOUTH_A,
70 .ctl_name = "AMD8131_PCIX_SOUTH_A",
73 .inst = SOUTH_B,
74 .devfn = DEVFN_PCIX_BRIDGE_SOUTH_B,
75 .ctl_name = "AMD8131_PCIX_SOUTH_B",
77 {.inst = NO_BRIDGE,},
80 static void amd8131_pcix_init(struct amd8131_dev_info *dev_info)
82 u32 val32;
83 struct pci_dev *dev = dev_info->dev;
85 /* First clear error detection flags */
86 edac_pci_read_dword(dev, REG_MEM_LIM, &val32);
87 if (val32 & MEM_LIMIT_MASK)
88 edac_pci_write_dword(dev, REG_MEM_LIM, val32);
90 /* Clear Discard Timer Timedout flag */
91 edac_pci_read_dword(dev, REG_INT_CTLR, &val32);
92 if (val32 & INT_CTLR_DTS)
93 edac_pci_write_dword(dev, REG_INT_CTLR, val32);
95 /* Clear CRC Error flag on link side A */
96 edac_pci_read_dword(dev, REG_LNK_CTRL_A, &val32);
97 if (val32 & LNK_CTRL_CRCERR_A)
98 edac_pci_write_dword(dev, REG_LNK_CTRL_A, val32);
100 /* Clear CRC Error flag on link side B */
101 edac_pci_read_dword(dev, REG_LNK_CTRL_B, &val32);
102 if (val32 & LNK_CTRL_CRCERR_B)
103 edac_pci_write_dword(dev, REG_LNK_CTRL_B, val32);
106 * Then enable all error detections.
108 * Setup Discard Timer Sync Flood Enable,
109 * System Error Enable and Parity Error Enable.
111 edac_pci_read_dword(dev, REG_INT_CTLR, &val32);
112 val32 |= INT_CTLR_PERR | INT_CTLR_SERR | INT_CTLR_DTSE;
113 edac_pci_write_dword(dev, REG_INT_CTLR, val32);
115 /* Enable overall SERR Error detection */
116 edac_pci_read_dword(dev, REG_STS_CMD, &val32);
117 val32 |= STS_CMD_SERREN;
118 edac_pci_write_dword(dev, REG_STS_CMD, val32);
120 /* Setup CRC Flood Enable for link side A */
121 edac_pci_read_dword(dev, REG_LNK_CTRL_A, &val32);
122 val32 |= LNK_CTRL_CRCFEN;
123 edac_pci_write_dword(dev, REG_LNK_CTRL_A, val32);
125 /* Setup CRC Flood Enable for link side B */
126 edac_pci_read_dword(dev, REG_LNK_CTRL_B, &val32);
127 val32 |= LNK_CTRL_CRCFEN;
128 edac_pci_write_dword(dev, REG_LNK_CTRL_B, val32);
131 static void amd8131_pcix_exit(struct amd8131_dev_info *dev_info)
133 u32 val32;
134 struct pci_dev *dev = dev_info->dev;
136 /* Disable SERR, PERR and DTSE Error detection */
137 edac_pci_read_dword(dev, REG_INT_CTLR, &val32);
138 val32 &= ~(INT_CTLR_PERR | INT_CTLR_SERR | INT_CTLR_DTSE);
139 edac_pci_write_dword(dev, REG_INT_CTLR, val32);
141 /* Disable overall System Error detection */
142 edac_pci_read_dword(dev, REG_STS_CMD, &val32);
143 val32 &= ~STS_CMD_SERREN;
144 edac_pci_write_dword(dev, REG_STS_CMD, val32);
146 /* Disable CRC Sync Flood on link side A */
147 edac_pci_read_dword(dev, REG_LNK_CTRL_A, &val32);
148 val32 &= ~LNK_CTRL_CRCFEN;
149 edac_pci_write_dword(dev, REG_LNK_CTRL_A, val32);
151 /* Disable CRC Sync Flood on link side B */
152 edac_pci_read_dword(dev, REG_LNK_CTRL_B, &val32);
153 val32 &= ~LNK_CTRL_CRCFEN;
154 edac_pci_write_dword(dev, REG_LNK_CTRL_B, val32);
157 static void amd8131_pcix_check(struct edac_pci_ctl_info *edac_dev)
159 struct amd8131_dev_info *dev_info = edac_dev->pvt_info;
160 struct pci_dev *dev = dev_info->dev;
161 u32 val32;
163 /* Check PCI-X Bridge Memory Base-Limit Register for errors */
164 edac_pci_read_dword(dev, REG_MEM_LIM, &val32);
165 if (val32 & MEM_LIMIT_MASK) {
166 printk(KERN_INFO "Error(s) in mem limit register "
167 "on %s bridge\n", dev_info->ctl_name);
168 printk(KERN_INFO "DPE: %d, RSE: %d, RMA: %d\n"
169 "RTA: %d, STA: %d, MDPE: %d\n",
170 val32 & MEM_LIMIT_DPE,
171 val32 & MEM_LIMIT_RSE,
172 val32 & MEM_LIMIT_RMA,
173 val32 & MEM_LIMIT_RTA,
174 val32 & MEM_LIMIT_STA,
175 val32 & MEM_LIMIT_MDPE);
177 val32 |= MEM_LIMIT_MASK;
178 edac_pci_write_dword(dev, REG_MEM_LIM, val32);
180 edac_pci_handle_npe(edac_dev, edac_dev->ctl_name);
183 /* Check if Discard Timer timed out */
184 edac_pci_read_dword(dev, REG_INT_CTLR, &val32);
185 if (val32 & INT_CTLR_DTS) {
186 printk(KERN_INFO "Error(s) in interrupt and control register "
187 "on %s bridge\n", dev_info->ctl_name);
188 printk(KERN_INFO "DTS: %d\n", val32 & INT_CTLR_DTS);
190 val32 |= INT_CTLR_DTS;
191 edac_pci_write_dword(dev, REG_INT_CTLR, val32);
193 edac_pci_handle_npe(edac_dev, edac_dev->ctl_name);
196 /* Check if CRC error happens on link side A */
197 edac_pci_read_dword(dev, REG_LNK_CTRL_A, &val32);
198 if (val32 & LNK_CTRL_CRCERR_A) {
199 printk(KERN_INFO "Error(s) in link conf and control register "
200 "on %s bridge\n", dev_info->ctl_name);
201 printk(KERN_INFO "CRCERR: %d\n", val32 & LNK_CTRL_CRCERR_A);
203 val32 |= LNK_CTRL_CRCERR_A;
204 edac_pci_write_dword(dev, REG_LNK_CTRL_A, val32);
206 edac_pci_handle_npe(edac_dev, edac_dev->ctl_name);
209 /* Check if CRC error happens on link side B */
210 edac_pci_read_dword(dev, REG_LNK_CTRL_B, &val32);
211 if (val32 & LNK_CTRL_CRCERR_B) {
212 printk(KERN_INFO "Error(s) in link conf and control register "
213 "on %s bridge\n", dev_info->ctl_name);
214 printk(KERN_INFO "CRCERR: %d\n", val32 & LNK_CTRL_CRCERR_B);
216 val32 |= LNK_CTRL_CRCERR_B;
217 edac_pci_write_dword(dev, REG_LNK_CTRL_B, val32);
219 edac_pci_handle_npe(edac_dev, edac_dev->ctl_name);
223 static struct amd8131_info amd8131_chipset = {
224 .err_dev = PCI_DEVICE_ID_AMD_8131_APIC,
225 .devices = amd8131_devices,
226 .init = amd8131_pcix_init,
227 .exit = amd8131_pcix_exit,
228 .check = amd8131_pcix_check,
232 * There are 4 PCIX Bridges on ATCA-6101 that share the same PCI Device ID,
233 * so amd8131_probe() would be called by kernel 4 times, with different
234 * address of pci_dev for each of them each time.
236 static int amd8131_probe(struct pci_dev *dev, const struct pci_device_id *id)
238 struct amd8131_dev_info *dev_info;
240 for (dev_info = amd8131_chipset.devices; dev_info->inst != NO_BRIDGE;
241 dev_info++)
242 if (dev_info->devfn == dev->devfn)
243 break;
245 if (dev_info->inst == NO_BRIDGE) /* should never happen */
246 return -ENODEV;
249 * We can't call pci_get_device() as we are used to do because
250 * there are 4 of them but pci_dev_get() instead.
252 dev_info->dev = pci_dev_get(dev);
254 if (pci_enable_device(dev_info->dev)) {
255 pci_dev_put(dev_info->dev);
256 printk(KERN_ERR "failed to enable:"
257 "vendor %x, device %x, devfn %x, name %s\n",
258 PCI_VENDOR_ID_AMD, amd8131_chipset.err_dev,
259 dev_info->devfn, dev_info->ctl_name);
260 return -ENODEV;
264 * we do not allocate extra private structure for
265 * edac_pci_ctl_info, but make use of existing
266 * one instead.
268 dev_info->edac_idx = edac_pci_alloc_index();
269 dev_info->edac_dev = edac_pci_alloc_ctl_info(0, dev_info->ctl_name);
270 if (!dev_info->edac_dev)
271 return -ENOMEM;
273 dev_info->edac_dev->pvt_info = dev_info;
274 dev_info->edac_dev->dev = &dev_info->dev->dev;
275 dev_info->edac_dev->mod_name = AMD8131_EDAC_MOD_STR;
276 dev_info->edac_dev->ctl_name = dev_info->ctl_name;
277 dev_info->edac_dev->dev_name = dev_name(&dev_info->dev->dev);
279 if (edac_op_state == EDAC_OPSTATE_POLL)
280 dev_info->edac_dev->edac_check = amd8131_chipset.check;
282 if (amd8131_chipset.init)
283 amd8131_chipset.init(dev_info);
285 if (edac_pci_add_device(dev_info->edac_dev, dev_info->edac_idx) > 0) {
286 printk(KERN_ERR "failed edac_pci_add_device() for %s\n",
287 dev_info->ctl_name);
288 edac_pci_free_ctl_info(dev_info->edac_dev);
289 return -ENODEV;
292 printk(KERN_INFO "added one device on AMD8131 "
293 "vendor %x, device %x, devfn %x, name %s\n",
294 PCI_VENDOR_ID_AMD, amd8131_chipset.err_dev,
295 dev_info->devfn, dev_info->ctl_name);
297 return 0;
300 static void amd8131_remove(struct pci_dev *dev)
302 struct amd8131_dev_info *dev_info;
304 for (dev_info = amd8131_chipset.devices; dev_info->inst != NO_BRIDGE;
305 dev_info++)
306 if (dev_info->devfn == dev->devfn)
307 break;
309 if (dev_info->inst == NO_BRIDGE) /* should never happen */
310 return;
312 if (dev_info->edac_dev) {
313 edac_pci_del_device(dev_info->edac_dev->dev);
314 edac_pci_free_ctl_info(dev_info->edac_dev);
317 if (amd8131_chipset.exit)
318 amd8131_chipset.exit(dev_info);
320 pci_dev_put(dev_info->dev);
323 static const struct pci_device_id amd8131_edac_pci_tbl[] = {
325 PCI_VEND_DEV(AMD, 8131_BRIDGE),
326 .subvendor = PCI_ANY_ID,
327 .subdevice = PCI_ANY_ID,
328 .class = 0,
329 .class_mask = 0,
330 .driver_data = 0,
334 } /* table is NULL-terminated */
336 MODULE_DEVICE_TABLE(pci, amd8131_edac_pci_tbl);
338 static struct pci_driver amd8131_edac_driver = {
339 .name = AMD8131_EDAC_MOD_STR,
340 .probe = amd8131_probe,
341 .remove = amd8131_remove,
342 .id_table = amd8131_edac_pci_tbl,
345 static int __init amd8131_edac_init(void)
347 printk(KERN_INFO "AMD8131 EDAC driver " AMD8131_EDAC_REVISION "\n");
348 printk(KERN_INFO "\t(c) 2008 Wind River Systems, Inc.\n");
350 /* Only POLL mode supported so far */
351 edac_op_state = EDAC_OPSTATE_POLL;
353 return pci_register_driver(&amd8131_edac_driver);
356 static void __exit amd8131_edac_exit(void)
358 pci_unregister_driver(&amd8131_edac_driver);
361 module_init(amd8131_edac_init);
362 module_exit(amd8131_edac_exit);
364 MODULE_LICENSE("GPL");
365 MODULE_AUTHOR("Cao Qingtao <qingtao.cao@windriver.com>\n");
366 MODULE_DESCRIPTION("AMD8131 HyperTransport PCI-X Tunnel EDAC kernel module");