blob: 8d363d60498b09875121d4c0975cb0e182cad6ff [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Paul Burton234882c2013-11-08 11:18:50 +00002/*
3 * Copyright (C) 2013 Imagination Technologies
Paul Burtonb8551b92017-10-30 16:58:21 -07004 * Author: Paul Burton <paul.burton@mips.com>
Paul Burton234882c2013-11-08 11:18:50 +00005 */
6
Daniel Schwierzeckcbf106c2021-07-15 20:53:58 +02007#include <dm.h>
Simon Glass97589732020-05-10 11:40:02 -06008#include <init.h>
Paul Burton234882c2013-11-08 11:18:50 +00009#include <msc01.h>
10#include <pci.h>
11#include <pci_msc01.h>
12#include <asm/io.h>
13
14#define PCI_ACCESS_READ 0
15#define PCI_ACCESS_WRITE 1
16
17struct msc01_pci_controller {
18 struct pci_controller hose;
19 void *base;
20};
21
22static inline struct msc01_pci_controller *
23hose_to_msc01(struct pci_controller *hose)
24{
25 return container_of(hose, struct msc01_pci_controller, hose);
26}
27
28static int msc01_config_access(struct msc01_pci_controller *msc01,
29 unsigned char access_type, pci_dev_t bdf,
30 int where, u32 *data)
31{
32 const u32 aborts = MSC01_PCI_INTSTAT_MA_MSK | MSC01_PCI_INTSTAT_TA_MSK;
33 void *intstat = msc01->base + MSC01_PCI_INTSTAT_OFS;
34 void *cfgdata = msc01->base + MSC01_PCI_CFGDATA_OFS;
35 unsigned int bus = PCI_BUS(bdf);
36 unsigned int dev = PCI_DEV(bdf);
Pali Rohár3fe44782021-11-26 11:42:44 +010037 unsigned int func = PCI_FUNC(bdf);
Paul Burton234882c2013-11-08 11:18:50 +000038
39 /* clear abort status */
40 __raw_writel(aborts, intstat);
41
42 /* setup address */
Pali Rohár3fe44782021-11-26 11:42:44 +010043 __raw_writel((PCI_CONF1_ADDRESS(bus, dev, func, where) & ~PCI_CONF1_ENABLE),
Paul Burton234882c2013-11-08 11:18:50 +000044 msc01->base + MSC01_PCI_CFGADDR_OFS);
45
46 /* perform access */
47 if (access_type == PCI_ACCESS_WRITE)
48 __raw_writel(*data, cfgdata);
49 else
50 *data = __raw_readl(cfgdata);
51
52 /* check for aborts */
53 if (__raw_readl(intstat) & aborts) {
54 /* clear abort status */
55 __raw_writel(aborts, intstat);
56 return -1;
57 }
58
59 return 0;
60}
61
Daniel Schwierzeckcbf106c2021-07-15 20:53:58 +020062static int msc01_pci_read_config(const struct udevice *dev, pci_dev_t bdf,
63 uint where, ulong *val, enum pci_size_t size)
64{
65 struct msc01_pci_controller *msc01 = dev_get_priv(dev);
66 u32 data = 0;
67
68 if (msc01_config_access(msc01, PCI_ACCESS_READ, bdf, where, &data)) {
69 *val = pci_get_ff(size);
70 return 0;
71 }
72
73 *val = pci_conv_32_to_size(data, where, size);
74
75 return 0;
76}
77
78static int msc01_pci_write_config(struct udevice *dev, pci_dev_t bdf,
79 uint where, ulong val, enum pci_size_t size)
80{
81 struct msc01_pci_controller *msc01 = dev_get_priv(dev);
82 u32 data = 0;
83
84 if (size == PCI_SIZE_32) {
85 data = val;
86 } else {
87 u32 old;
88
89 if (msc01_config_access(msc01, PCI_ACCESS_READ, bdf, where, &old))
90 return 0;
91
92 data = pci_conv_size_to_32(old, val, where, size);
93 }
94
95 msc01_config_access(msc01, PCI_ACCESS_WRITE, bdf, where, &data);
96
97 return 0;
98}
99
100static int msc01_pci_probe(struct udevice *dev)
101{
102 struct msc01_pci_controller *msc01 = dev_get_priv(dev);
103
104 msc01->base = dev_remap_addr(dev);
105 if (!msc01->base)
106 return -EINVAL;
107
108 return 0;
109}
110
111static const struct dm_pci_ops msc01_pci_ops = {
112 .read_config = msc01_pci_read_config,
113 .write_config = msc01_pci_write_config,
114};
115
116static const struct udevice_id msc01_pci_ids[] = {
117 { .compatible = "mips,pci-msc01" },
118 { }
119};
120
121U_BOOT_DRIVER(msc01_pci) = {
122 .name = "msc01_pci",
123 .id = UCLASS_PCI,
124 .of_match = msc01_pci_ids,
125 .ops = &msc01_pci_ops,
126 .probe = msc01_pci_probe,
127 .priv_auto = sizeof(struct msc01_pci_controller),
128};