coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
bdk-coreboot.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 /*
4  * This file consists of data imported from bdk-config.c
5  */
6 
7 // coreboot
8 #include <string.h>
9 #include <assert.h>
10 #include <device/i2c.h>
11 #include <device/i2c_simple.h>
12 #include <endian.h>
13 #include <soc/timer.h>
14 
15 // BDK
16 #include <libbdk-arch/bdk-numa.h>
17 #include <libbdk-hal/bdk-config.h>
18 #include <libbdk-hal/bdk-twsi.h>
19 #include <libbdk-boot/bdk-watchdog.h>
20 
21 /**
22  * Do a twsi read from a 7 bit device address using an (optional)
23  * internal address. Up to 4 bytes can be read at a time.
24  *
25  * @param twsi_id which TWSI bus to use
26  * @param dev_addr Device address (7 bit)
27  * @param internal_addr
28  * Internal address. Can be 0, 1 or 2 bytes in width
29  * @param num_bytes Number of data bytes to read (1-4)
30  * @param ia_width_bytes
31  * Internal address size in bytes (0, 1, or 2)
32  *
33  * @return Read data, or -1 on failure
34  */
35 int64_t bdk_twsix_read_ia(bdk_node_t node, int twsi_id, uint8_t dev_addr,
36  uint16_t internal_addr, int num_bytes,
37  int ia_width_bytes)
38 {
39  struct i2c_msg seg[2];
40  u32 buf;
41 
42  assert (num_bytes < 5);
43  assert (ia_width_bytes < 3);
44 
45  seg[0].flags = 0;
46  seg[0].slave = dev_addr;
47  seg[0].buf = (u8 *)&internal_addr;
48  seg[0].len = ia_width_bytes;
49  seg[1].flags = I2C_M_RD;
50  seg[1].slave = dev_addr;
51  seg[1].buf = (u8 *)&buf;
52  seg[1].len = num_bytes;
53 
54  if (i2c_transfer(twsi_id, seg, ARRAY_SIZE(seg)) < 0)
55  return -1;
56 
57  return cpu_to_be32(buf);
58 }
59 
60 /**
61  * Write 1-8 bytes to a TWSI device using an internal address.
62  *
63  * @param twsi_id which TWSI interface to use
64  * @param dev_addr TWSI device address (7 bit only)
65  * @param internal_addr
66  * TWSI internal address (0, 8, or 16 bits)
67  * @param num_bytes Number of bytes to write (1-8)
68  * @param ia_width_bytes
69  * internal address width, in bytes (0, 1, 2)
70  * @param data Data to write. Data is written MSB first on the twsi bus,
71  * and only the lower num_bytes bytes of the argument are
72  * valid. If a 2 byte write is done, only the low 2 bytes of
73  * the argument is used.
74  *
75  * @return Zero on success, -1 on error
76  */
77 int bdk_twsix_write_ia(bdk_node_t node, int twsi_id, uint8_t dev_addr,
78  uint16_t internal_addr, int num_bytes,
79  int ia_width_bytes, uint64_t data)
80 {
81  struct i2c_msg seg;
82  u8 buf[10];
83 
84  assert (num_bytes <= 8);
85  assert (ia_width_bytes < 3);
86 
87  memcpy(buf, &internal_addr, ia_width_bytes);
88  memcpy(&buf[ia_width_bytes], &data, num_bytes);
89 
90  seg.flags = 0;
91  seg.slave = dev_addr;
92  seg.buf = buf;
93  seg.len = num_bytes + ia_width_bytes;
94 
95  return platform_i2c_transfer(twsi_id, &seg, 1);
96 }
97 
98 void bdk_watchdog_set(unsigned int timeout_ms)
99 {
100  watchdog_set(0, timeout_ms);
101 }
102 
104 {
105  watchdog_poke(0);
106 }
107 
109 {
110  watchdog_disable(0);
111 }
112 
114 {
115  return watchdog_is_running(0);
116 }
void * memcpy(void *dest, const void *src, size_t n)
Definition: memcpy.c:7
#define assert(statement)
Definition: assert.h:74
void bdk_watchdog_set(unsigned int timeout_ms)
Definition: bdk-coreboot.c:98
void bdk_watchdog_poke(void)
Definition: bdk-coreboot.c:103
int64_t bdk_twsix_read_ia(bdk_node_t node, int twsi_id, uint8_t dev_addr, uint16_t internal_addr, int num_bytes, int ia_width_bytes)
Do a twsi read from a 7 bit device address using an (optional) internal address.
Definition: bdk-coreboot.c:35
int bdk_watchdog_is_running(void)
Definition: bdk-coreboot.c:113
int bdk_twsix_write_ia(bdk_node_t node, int twsi_id, uint8_t dev_addr, uint16_t internal_addr, int num_bytes, int ia_width_bytes, uint64_t data)
Write 1-8 bytes to a TWSI device using an internal address.
Definition: bdk-coreboot.c:77
void bdk_watchdog_disable(void)
Definition: bdk-coreboot.c:108
#define ARRAY_SIZE(a)
Definition: helpers.h:12
int platform_i2c_transfer(unsigned int bus, struct i2c_msg *msg, int count)
Definition: dw_i2c.c:484
static int i2c_transfer(unsigned int bus, struct i2c_msg *segments, int count)
Definition: i2c_simple.h:42
#define I2C_M_RD
Definition: i2c.h:34
static uint8_t * buf
Definition: uart.c:7
void watchdog_disable(const size_t index)
Disable the hardware watchdog.
Definition: timer.c:190
void watchdog_poke(const size_t index)
Signal the watchdog that we are still running.
Definition: timer.c:174
void watchdog_set(const size_t index, unsigned int timeout_ms)
Setup the watchdog to expire in timeout_ms milliseconds.
Definition: timer.c:137
int watchdog_is_running(const size_t index)
Return true if the watchdog is configured and running.
Definition: timer.c:209
unsigned short uint16_t
Definition: stdint.h:11
uint32_t u32
Definition: stdint.h:51
unsigned long long uint64_t
Definition: stdint.h:17
uint8_t u8
Definition: stdint.h:45
unsigned char uint8_t
Definition: stdint.h:8
signed long long int64_t
Definition: stdint.h:16
struct i2c_msg - an I2C transaction segment beginning with START @addr: Slave address,...
Definition: i2c.h:32
uint16_t len
Definition: i2c.h:39
uint16_t slave
Definition: i2c.h:38
uint16_t flags
Definition: i2c.h:33
uint8_t * buf
Definition: i2c.h:40