1 // SPDX-License-Identifier: GPL-2.0+
3 * Uclass for Primary-to-sideband bus, used to access various peripherals
5 * Copyright 2019 Google LLC
6 * Written by Simon Glass <sjg@chromium.org>
15 #include <dm/uclass-internal.h>
17 #define PCR_COMMON_IOSF_1_0 1
19 static void *_pcr_reg_address(struct udevice *dev, uint offset)
21 struct p2sb_child_platdata *pplat = dev_get_parent_platdata(dev);
22 struct udevice *p2sb = dev_get_parent(dev);
23 struct p2sb_uc_priv *upriv = dev_get_uclass_priv(p2sb);
26 /* Create an address based off of port id and offset */
27 reg_addr = upriv->mmio_base;
28 reg_addr += pplat->pid << PCR_PORTID_SHIFT;
31 return map_sysmem(reg_addr, 4);
35 * The mapping of addresses via the SBREG_BAR assumes the IOSF-SB
36 * agents are using 32-bit aligned accesses for their configuration
37 * registers. For IOSF versions greater than 1_0, IOSF-SB
38 * agents can use any access (8/16/32 bit aligned) for their
39 * configuration registers
41 static inline void check_pcr_offset_align(uint offset, uint size)
43 const size_t align = PCR_COMMON_IOSF_1_0 ? sizeof(uint32_t) : size;
45 assert(IS_ALIGNED(offset, align));
48 uint pcr_read32(struct udevice *dev, uint offset)
53 /* Ensure the PCR offset is correctly aligned */
54 assert(IS_ALIGNED(offset, sizeof(uint32_t)));
56 ptr = _pcr_reg_address(dev, offset);
63 uint pcr_read16(struct udevice *dev, uint offset)
65 /* Ensure the PCR offset is correctly aligned */
66 check_pcr_offset_align(offset, sizeof(uint16_t));
68 return readw(_pcr_reg_address(dev, offset));
71 uint pcr_read8(struct udevice *dev, uint offset)
73 /* Ensure the PCR offset is correctly aligned */
74 check_pcr_offset_align(offset, sizeof(uint8_t));
76 return readb(_pcr_reg_address(dev, offset));
80 * After every write one needs to perform a read an innocuous register to
81 * ensure the writes are completed for certain ports. This is done for
82 * all ports so that the callers don't need the per-port knowledge for
85 static void write_completion(struct udevice *dev, uint offset)
87 readl(_pcr_reg_address(dev, ALIGN_DOWN(offset, sizeof(uint32_t))));
90 void pcr_write32(struct udevice *dev, uint offset, uint indata)
92 /* Ensure the PCR offset is correctly aligned */
93 assert(IS_ALIGNED(offset, sizeof(indata)));
95 writel(indata, _pcr_reg_address(dev, offset));
96 /* Ensure the writes complete */
97 write_completion(dev, offset);
100 void pcr_write16(struct udevice *dev, uint offset, uint indata)
102 /* Ensure the PCR offset is correctly aligned */
103 check_pcr_offset_align(offset, sizeof(uint16_t));
105 writew(indata, _pcr_reg_address(dev, offset));
106 /* Ensure the writes complete */
107 write_completion(dev, offset);
110 void pcr_write8(struct udevice *dev, uint offset, uint indata)
112 /* Ensure the PCR offset is correctly aligned */
113 check_pcr_offset_align(offset, sizeof(uint8_t));
115 writeb(indata, _pcr_reg_address(dev, offset));
116 /* Ensure the writes complete */
117 write_completion(dev, offset);
120 void pcr_clrsetbits32(struct udevice *dev, uint offset, uint clr, uint set)
124 data32 = pcr_read32(dev, offset);
127 pcr_write32(dev, offset, data32);
130 void pcr_clrsetbits16(struct udevice *dev, uint offset, uint clr, uint set)
134 data16 = pcr_read16(dev, offset);
137 pcr_write16(dev, offset, data16);
140 void pcr_clrsetbits8(struct udevice *dev, uint offset, uint clr, uint set)
144 data8 = pcr_read8(dev, offset);
147 pcr_write8(dev, offset, data8);
150 int p2sb_get_port_id(struct udevice *dev)
152 struct p2sb_child_platdata *pplat = dev_get_parent_platdata(dev);
157 int p2sb_set_port_id(struct udevice *dev, int portid)
159 struct udevice *ps2b;
160 struct p2sb_child_platdata *pplat;
162 if (!CONFIG_IS_ENABLED(OF_PLATDATA))
165 uclass_find_first_device(UCLASS_P2SB, &ps2b);
171 * We must allocate this, since when the device was bound it did not
173 * TODO(sjg@chromium.org): Add a parent pointer to child devices in dtoc
175 dev->parent_platdata = malloc(sizeof(*pplat));
176 if (!dev->parent_platdata)
178 pplat = dev_get_parent_platdata(dev);
184 static int p2sb_child_post_bind(struct udevice *dev)
186 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
187 struct p2sb_child_platdata *pplat = dev_get_parent_platdata(dev);
191 ret = dev_read_u32(dev, "intel,p2sb-port-id", &pid);
200 static int p2sb_post_bind(struct udevice *dev)
202 if (spl_phase() > PHASE_TPL && !CONFIG_IS_ENABLED(OF_PLATDATA))
203 return dm_scan_fdt_dev(dev);
208 UCLASS_DRIVER(p2sb) = {
211 .per_device_auto_alloc_size = sizeof(struct p2sb_uc_priv),
212 .post_bind = p2sb_post_bind,
213 .child_post_bind = p2sb_child_post_bind,
214 .per_child_platdata_auto_alloc_size =
215 sizeof(struct p2sb_child_platdata),