blob: a94b2aee67391b5a5d8ae513677f470efb5eb457 [file] [log] [blame]
Daisuke Nojiri05949142014-11-21 15:33:26 -08001/*
Vadim Bendebury6fe4e5e2014-12-06 10:44:58 -08002 * This file is part of the coreboot project.
Daisuke Nojiri05949142014-11-21 15:33:26 -08003 *
Sourabh Banerjee89208652015-02-19 16:43:26 +05304 * Copyright (C) 2014 - 2015 The Linux Foundation. All rights reserved.
Daisuke Nojiri05949142014-11-21 15:33:26 -08005 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
Vadim Bendebury6fe4e5e2014-12-06 10:44:58 -080030#include <console/console.h>
Nico Huber0f2dd1e2017-08-01 14:02:40 +020031#include <device/i2c_simple.h>
Vadim Bendebury6fe4e5e2014-12-06 10:44:58 -080032#include <string.h>
33#include <soc/gsbi.h>
34#include <soc/qup.h>
Daisuke Nojiri05949142014-11-21 15:33:26 -080035
Sourabh Banerjee89208652015-02-19 16:43:26 +053036static qup_config_t gsbi1_qup_config = {
Vadim Bendebury6fe4e5e2014-12-06 10:44:58 -080037 QUP_MINICORE_I2C_MASTER,
38 100000,
39 24000000,
Sourabh Banerjee89208652015-02-19 16:43:26 +053040 QUP_MODE_FIFO,
41 0
42};
43
44static qup_config_t gsbi4_qup_config = {
45 QUP_MINICORE_I2C_MASTER,
46 100000,
47 24000000,
48 QUP_MODE_FIFO,
49 0
50};
51
52static qup_config_t gsbi7_qup_config = {
53 QUP_MINICORE_I2C_MASTER,
54 100000,
55 24000000,
56 QUP_MODE_FIFO,
57 0
Vadim Bendebury6fe4e5e2014-12-06 10:44:58 -080058};
Daisuke Nojiri05949142014-11-21 15:33:26 -080059
60static int i2c_read(uint32_t gsbi_id, uint8_t slave,
Vadim Bendebury6fe4e5e2014-12-06 10:44:58 -080061 uint8_t *data, int data_len)
Daisuke Nojiri05949142014-11-21 15:33:26 -080062{
63 qup_data_t obj;
64 qup_return_t qup_ret = 0;
65
66 memset(&obj, 0, sizeof(obj));
67 obj.protocol = QUP_MINICORE_I2C_MASTER;
68 obj.p.iic.addr = slave;
69 obj.p.iic.data_len = data_len;
70 obj.p.iic.data = data;
71 qup_ret = qup_recv_data(gsbi_id, &obj);
72
73 if (QUP_SUCCESS != qup_ret)
74 return 1;
75 else
76 return 0;
77}
78
79static int i2c_write(uint32_t gsbi_id, uint8_t slave,
80 uint8_t *data, int data_len, uint8_t stop_seq)
81{
82 qup_data_t obj;
83 qup_return_t qup_ret = 0;
84
85 memset(&obj, 0, sizeof(obj));
86 obj.protocol = QUP_MINICORE_I2C_MASTER;
87 obj.p.iic.addr = slave;
88 obj.p.iic.data_len = data_len;
89 obj.p.iic.data = data;
90 qup_ret = qup_send_data(gsbi_id, &obj, stop_seq);
91
92 if (QUP_SUCCESS != qup_ret)
93 return 1;
94 else
95 return 0;
96}
97
Martin Roth57e89092019-10-23 21:45:23 -060098static int i2c_init(unsigned int bus)
Daisuke Nojiri05949142014-11-21 15:33:26 -080099{
Martin Roth57e89092019-10-23 21:45:23 -0600100 unsigned int gsbi_id = bus;
Sourabh Banerjee89208652015-02-19 16:43:26 +0530101 qup_config_t *qup_config;
Vadim Bendebury6fe4e5e2014-12-06 10:44:58 -0800102
Sourabh Banerjee89208652015-02-19 16:43:26 +0530103 switch (gsbi_id) {
104 case GSBI_ID_1:
105 qup_config = &gsbi1_qup_config;
106 break;
107 case GSBI_ID_4:
108 qup_config = &gsbi4_qup_config;
109 break;
110 case GSBI_ID_7:
111 qup_config = &gsbi7_qup_config;
112 break;
113 default:
114 printk(BIOS_ERR, "QUP configuration not defind for GSBI%d.\n",
115 gsbi_id);
116 return 1;
117 }
118
119 if (qup_config->initialized)
Vadim Bendebury6fe4e5e2014-12-06 10:44:58 -0800120 return 0;
121
122 if (gsbi_init(gsbi_id, GSBI_PROTO_I2C_ONLY)) {
123 printk(BIOS_ERR, "failed to initialize gsbi\n");
124 return 1;
125 }
126
Sourabh Banerjee89208652015-02-19 16:43:26 +0530127 if (qup_init(gsbi_id, qup_config)) {
Vadim Bendebury6fe4e5e2014-12-06 10:44:58 -0800128 printk(BIOS_ERR, "failed to initialize qup\n");
129 return 1;
130 }
131
132 if (qup_reset_i2c_master_status(gsbi_id)) {
133 printk(BIOS_ERR, "failed to reset i2c master status\n");
134 return 1;
135 }
136
Sourabh Banerjee89208652015-02-19 16:43:26 +0530137 qup_config->initialized = 1;
Vadim Bendebury6fe4e5e2014-12-06 10:44:58 -0800138 return 0;
139}
140
Martin Roth57e89092019-10-23 21:45:23 -0600141int platform_i2c_transfer(unsigned int bus, struct i2c_msg *segments,
Nico Huber029dfff2017-07-12 17:59:16 +0200142 int seg_count)
Vadim Bendebury6fe4e5e2014-12-06 10:44:58 -0800143{
Nico Huber029dfff2017-07-12 17:59:16 +0200144 struct i2c_msg *seg = segments;
Daisuke Nojiri05949142014-11-21 15:33:26 -0800145 int ret = 0;
146
Vadim Bendebury6fe4e5e2014-12-06 10:44:58 -0800147 if (i2c_init(bus))
148 return 1;
Daisuke Nojiri05949142014-11-21 15:33:26 -0800149
Sourabh Banerjee54cc8ba2015-02-27 19:11:19 +0530150 while (!ret && seg_count--) {
Nico Huber029dfff2017-07-12 17:59:16 +0200151 if (seg->flags & I2C_M_RD)
152 ret = i2c_read(bus, seg->slave, seg->buf, seg->len);
Daisuke Nojiri05949142014-11-21 15:33:26 -0800153 else
Nico Huber029dfff2017-07-12 17:59:16 +0200154 ret = i2c_write(bus, seg->slave, seg->buf, seg->len,
Vadim Bendebury6fe4e5e2014-12-06 10:44:58 -0800155 (seg_count ? 0 : 1));
Daisuke Nojiri05949142014-11-21 15:33:26 -0800156 seg++;
157 }
158
Vadim Bendebury6fe4e5e2014-12-06 10:44:58 -0800159 if (ret) {
160 qup_set_state(bus, QUP_STATE_RESET);
Daisuke Nojiri05949142014-11-21 15:33:26 -0800161 return 1;
162 }
163
164 return 0;
165}