blob: 17304a18dcc2509a794232f3326ab7924ec3294d [file] [log] [blame]
Duncan Laurie8a14c392016-06-07 13:40:11 -07001/*
2 * This file is part of the coreboot project.
3 *
Chris Chingb8dc63b2017-12-06 14:26:15 -07004 * Copyright 2017 Intel Corporation.
Duncan Laurie8a14c392016-06-07 13:40:11 -07005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
Jonathan Neuschäfer00170042018-02-12 12:00:45 +010011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Duncan Laurie8a14c392016-06-07 13:40:11 -070012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15
Chris Chingb8dc63b2017-12-06 14:26:15 -070016#ifndef __DRIVERS_I2C_DESIGNWARE_I2C_H__
17#define __DRIVERS_I2C_DESIGNWARE_I2C_H__
Duncan Laurie8a14c392016-06-07 13:40:11 -070018
Nico Huber0f2dd1e2017-08-01 14:02:40 +020019#include <device/device.h>
Duncan Laurie8a14c392016-06-07 13:40:11 -070020#include <device/i2c.h>
21#include <stdint.h>
22
Chris Chingb8dc63b2017-12-06 14:26:15 -070023#if IS_ENABLED(CONFIG_DRIVERS_I2C_DESIGNWARE_DEBUG)
24#define DW_I2C_DEBUG BIOS_DEBUG
25
26#else
27#define DW_I2C_DEBUG BIOS_NEVER
28
29#endif // CONFIG_DRIVERS_I2C_DESIGNWARE_DEBUG
30
Duncan Laurie8a14c392016-06-07 13:40:11 -070031/*
Duncan Laurie88a1f142016-06-13 10:28:36 -070032 * Timing values are in units of clock period, with the clock speed
Chris Chingb8dc63b2017-12-06 14:26:15 -070033 * provided by the SOC in CONFIG_DRIVERS_I2C_DESIGNWARE_I2C_CLOCK_MHZ
Duncan Laurie88a1f142016-06-13 10:28:36 -070034 * Automatic configuration is done based on requested speed, but the
35 * values may need tuned depending on the board and the number of
36 * devices present on the bus.
37 */
Chris Chingb8dc63b2017-12-06 14:26:15 -070038struct dw_i2c_speed_config {
Duncan Laurie88a1f142016-06-13 10:28:36 -070039 enum i2c_speed speed;
40 /* SCL high and low period count */
41 uint16_t scl_lcnt;
42 uint16_t scl_hcnt;
43 /*
44 * SDA hold time should be 300ns in standard and fast modes
45 * and long enough for deterministic logic level change in
Jonathan Neuschäfer00170042018-02-12 12:00:45 +010046 * fast-plus and high speed modes.
Duncan Laurie88a1f142016-06-13 10:28:36 -070047 *
48 * [15:0] SDA TX Hold Time
49 * [23:16] SDA RX Hold Time
50 */
51 uint32_t sda_hold;
52};
53
54/*
55 * This I2C controller has support for 3 independent speed configs but can
56 * support both FAST_PLUS and HIGH speeds through the same set of speed
57 * config registers. These are treated separately so the speed config values
58 * can be provided via ACPI to the OS.
59 */
Chris Chingb8dc63b2017-12-06 14:26:15 -070060#define DW_I2C_SPEED_CONFIG_COUNT 4
Duncan Laurie88a1f142016-06-13 10:28:36 -070061
Chris Chingb8dc63b2017-12-06 14:26:15 -070062struct dw_i2c_bus_config {
Aaron Durbin4668ba72016-11-09 17:09:40 -060063 /* Bus should be enabled prior to ramstage with temporary base */
64 int early_init;
65 /* Bus speed in Hz, default is I2C_SPEED_FAST (400 KHz) */
66 enum i2c_speed speed;
Aaron Durbin2b3e0cd2016-11-09 23:20:30 -060067 /* If rise_time_ns is non-zero the calculations for lcnt and hcnt
68 * registers take into account the times of the bus. However, if
69 * there is a match in speed_config those register values take
70 * precedence. */
71 int rise_time_ns;
72 int fall_time_ns;
Aaron Durbinc5f10f92017-03-31 14:46:26 -050073 int data_hold_time_ns;
Aaron Durbin4668ba72016-11-09 17:09:40 -060074 /* Specific bus speed configuration */
Chris Chingb8dc63b2017-12-06 14:26:15 -070075 struct dw_i2c_speed_config speed_config[DW_I2C_SPEED_CONFIG_COUNT];
Aaron Durbin4668ba72016-11-09 17:09:40 -060076};
77
Rizwan Qureshiae6a4b62017-04-26 21:06:35 +053078/* Functions to be implemented by SoC code */
79
80/* Get base address for early init of I2C controllers. */
Aaron Durbin9aee8192018-01-22 20:29:25 -070081uintptr_t dw_i2c_get_soc_early_base(unsigned int bus);
Duncan Laurie8a14c392016-06-07 13:40:11 -070082
83/*
Rizwan Qureshiae6a4b62017-04-26 21:06:35 +053084 * Map given I2C bus number to devfn.
85 * Return value:
86 * -1 = error
87 * otherwise, devfn(>=0) corresponding to I2C bus number.
Duncan Laurie88a1f142016-06-13 10:28:36 -070088 */
Aaron Durbin9aee8192018-01-22 20:29:25 -070089int dw_i2c_soc_devfn_to_bus(unsigned int devfn);
Duncan Laurie88a1f142016-06-13 10:28:36 -070090
91/*
Rizwan Qureshiae6a4b62017-04-26 21:06:35 +053092 * Map given bus number to a I2C Controller.
93 * Return value:
94 * -1 = error
95 * otherwise, devfn(>=0) corresponding to I2C bus number.
Duncan Laurie8a14c392016-06-07 13:40:11 -070096 */
Aaron Durbin9aee8192018-01-22 20:29:25 -070097int dw_i2c_soc_bus_to_devfn(unsigned int bus);
Duncan Laurie8a14c392016-06-07 13:40:11 -070098
Rizwan Qureshiae6a4b62017-04-26 21:06:35 +053099/*
100 * SoC implemented callback for getting I2C bus configuration.
101 *
102 * Returns NULL if i2c config is not found
103 */
Aaron Durbinb94a2752018-01-24 16:58:18 -0700104const struct dw_i2c_bus_config *dw_i2c_get_soc_cfg(unsigned int bus);
Chris Chingb8dc63b2017-12-06 14:26:15 -0700105
106/* Get I2C controller base address */
107uintptr_t dw_i2c_base_address(unsigned int bus);
108
109/*
110 * Initialize this bus controller and set the speed
111 * Return value:
112 * -1 = failure
113 * 0 = success
114*/
115int dw_i2c_init(unsigned int bus, const struct dw_i2c_bus_config *bcfg);
116
117/*
118 * Generate speed config based on clock
119 * Return value:
120 * -1 = failure
121 * 0 = success
122*/
123int dw_i2c_gen_speed_config(uintptr_t dw_i2c_addr,
124 enum i2c_speed speed,
125 const struct dw_i2c_bus_config *bcfg,
126 struct dw_i2c_speed_config *config);
127
128/*
129 * Process given I2C segments in a single transfer
130 * Return value:
131 * -1 = failure
132 * 0 = success
133 */
134int dw_i2c_transfer(unsigned int bus,
135 const struct i2c_msg *segments,
136 size_t count);
137
Aaron Durbinb7d79cd2018-01-22 21:31:48 -0700138/*
139 * Map an i2c host controller device to a logical bus number.
140 * Return value:
141 * -1 = failure
142 * >=0 = logical bus number
143 */
144int dw_i2c_soc_dev_to_bus(struct device *dev);
145
146/*
147 * Common device_operations implementation to initialize the i2c host
148 * controller.
149 */
150void dw_i2c_dev_init(struct device *dev);
151
152/*
153 * Common device_operations implementation to fill ACPI SSDT table for i2c
154 * host controller.
155 */
156void dw_i2c_acpi_fill_ssdt(struct device *dev);
157
158/*
159 * Common device_operations implementation for i2c host controller ops.
160 */
161extern const struct i2c_bus_operations dw_i2c_bus_ops;
162
Chris Chingb8dc63b2017-12-06 14:26:15 -0700163#endif /* __DRIVERS_I2C_DESIGNWARE_I2C_H__ */