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>
9 #define LOG_CATEGORY UCLASS_P2SB
19 #include <dm/uclass-internal.h>
21 #define PCR_COMMON_IOSF_1_0 1
23 int p2sb_set_hide(struct udevice *dev, bool hide)
25 struct p2sb_ops *ops = p2sb_get_ops(dev);
30 return ops->set_hide(dev, hide);
33 void *pcr_reg_address(struct udevice *dev, uint offset)
35 struct p2sb_child_plat *pplat = dev_get_parent_plat(dev);
36 struct udevice *p2sb = dev_get_parent(dev);
37 struct p2sb_uc_priv *upriv = dev_get_uclass_priv(p2sb);
40 /* Create an address based off of port id and offset */
41 reg_addr = upriv->mmio_base;
42 reg_addr += pplat->pid << PCR_PORTID_SHIFT;
45 return map_sysmem(reg_addr, 4);
49 * The mapping of addresses via the SBREG_BAR assumes the IOSF-SB
50 * agents are using 32-bit aligned accesses for their configuration
51 * registers. For IOSF versions greater than 1_0, IOSF-SB
52 * agents can use any access (8/16/32 bit aligned) for their
53 * configuration registers
55 static inline void check_pcr_offset_align(uint offset, uint size)
57 const size_t align = PCR_COMMON_IOSF_1_0 ? sizeof(uint32_t) : size;
59 assert(IS_ALIGNED(offset, align));
62 uint pcr_read32(struct udevice *dev, uint offset)
67 /* Ensure the PCR offset is correctly aligned */
68 assert(IS_ALIGNED(offset, sizeof(uint32_t)));
70 ptr = pcr_reg_address(dev, offset);
77 uint pcr_read16(struct udevice *dev, uint offset)
79 /* Ensure the PCR offset is correctly aligned */
80 check_pcr_offset_align(offset, sizeof(uint16_t));
82 return readw(pcr_reg_address(dev, offset));
85 uint pcr_read8(struct udevice *dev, uint offset)
87 /* Ensure the PCR offset is correctly aligned */
88 check_pcr_offset_align(offset, sizeof(uint8_t));
90 return readb(pcr_reg_address(dev, offset));
94 * After every write one needs to perform a read an innocuous register to
95 * ensure the writes are completed for certain ports. This is done for
96 * all ports so that the callers don't need the per-port knowledge for
99 static void write_completion(struct udevice *dev, uint offset)
101 readl(pcr_reg_address(dev, ALIGN_DOWN(offset, sizeof(uint32_t))));
104 void pcr_write32(struct udevice *dev, uint offset, uint indata)
106 /* Ensure the PCR offset is correctly aligned */
107 assert(IS_ALIGNED(offset, sizeof(indata)));
109 writel(indata, pcr_reg_address(dev, offset));
110 /* Ensure the writes complete */
111 write_completion(dev, offset);
114 void pcr_write16(struct udevice *dev, uint offset, uint indata)
116 /* Ensure the PCR offset is correctly aligned */
117 check_pcr_offset_align(offset, sizeof(uint16_t));
119 writew(indata, pcr_reg_address(dev, offset));
120 /* Ensure the writes complete */
121 write_completion(dev, offset);
124 void pcr_write8(struct udevice *dev, uint offset, uint indata)
126 /* Ensure the PCR offset is correctly aligned */
127 check_pcr_offset_align(offset, sizeof(uint8_t));
129 writeb(indata, pcr_reg_address(dev, offset));
130 /* Ensure the writes complete */
131 write_completion(dev, offset);
134 void pcr_clrsetbits32(struct udevice *dev, uint offset, uint clr, uint set)
138 data32 = pcr_read32(dev, offset);
141 pcr_write32(dev, offset, data32);
144 void pcr_clrsetbits16(struct udevice *dev, uint offset, uint clr, uint set)
148 data16 = pcr_read16(dev, offset);
151 pcr_write16(dev, offset, data16);
154 void pcr_clrsetbits8(struct udevice *dev, uint offset, uint clr, uint set)
158 data8 = pcr_read8(dev, offset);
161 pcr_write8(dev, offset, data8);
164 int p2sb_get_port_id(struct udevice *dev)
166 struct p2sb_child_plat *pplat = dev_get_parent_plat(dev);
171 int p2sb_set_port_id(struct udevice *dev, int portid)
173 struct p2sb_child_plat *pplat;
175 if (!CONFIG_IS_ENABLED(OF_PLATDATA))
178 pplat = dev_get_parent_plat(dev);
184 static int p2sb_child_post_bind(struct udevice *dev)
186 #if !CONFIG_IS_ENABLED(OF_PLATDATA)
187 struct p2sb_child_plat *pplat = dev_get_parent_plat(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 = sizeof(struct p2sb_uc_priv),
212 .post_bind = p2sb_post_bind,
213 .child_post_bind = p2sb_child_post_bind,
214 .per_child_plat_auto = sizeof(struct p2sb_child_plat),