blob: e121ae84968e626a1d33af1bf21941d98de22720 [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 <arch/io.h>
Daisuke Nojiri05949142014-11-21 15:33:26 -080031#include <assert.h>
Vadim Bendebury6fe4e5e2014-12-06 10:44:58 -080032#include <console/console.h>
33#include <delay.h>
34#include <device/i2c.h>
35#include <stdlib.h>
36#include <string.h>
37#include <soc/gsbi.h>
38#include <soc/qup.h>
Daisuke Nojiri05949142014-11-21 15:33:26 -080039
Sourabh Banerjee89208652015-02-19 16:43:26 +053040static qup_config_t gsbi1_qup_config = {
Vadim Bendebury6fe4e5e2014-12-06 10:44:58 -080041 QUP_MINICORE_I2C_MASTER,
42 100000,
43 24000000,
Sourabh Banerjee89208652015-02-19 16:43:26 +053044 QUP_MODE_FIFO,
45 0
46};
47
48static qup_config_t gsbi4_qup_config = {
49 QUP_MINICORE_I2C_MASTER,
50 100000,
51 24000000,
52 QUP_MODE_FIFO,
53 0
54};
55
56static qup_config_t gsbi7_qup_config = {
57 QUP_MINICORE_I2C_MASTER,
58 100000,
59 24000000,
60 QUP_MODE_FIFO,
61 0
Vadim Bendebury6fe4e5e2014-12-06 10:44:58 -080062};
Daisuke Nojiri05949142014-11-21 15:33:26 -080063
64static int i2c_read(uint32_t gsbi_id, uint8_t slave,
Vadim Bendebury6fe4e5e2014-12-06 10:44:58 -080065 uint8_t *data, int data_len)
Daisuke Nojiri05949142014-11-21 15:33:26 -080066{
67 qup_data_t obj;
68 qup_return_t qup_ret = 0;
69
70 memset(&obj, 0, sizeof(obj));
71 obj.protocol = QUP_MINICORE_I2C_MASTER;
72 obj.p.iic.addr = slave;
73 obj.p.iic.data_len = data_len;
74 obj.p.iic.data = data;
75 qup_ret = qup_recv_data(gsbi_id, &obj);
76
77 if (QUP_SUCCESS != qup_ret)
78 return 1;
79 else
80 return 0;
81}
82
83static int i2c_write(uint32_t gsbi_id, uint8_t slave,
84 uint8_t *data, int data_len, uint8_t stop_seq)
85{
86 qup_data_t obj;
87 qup_return_t qup_ret = 0;
88
89 memset(&obj, 0, sizeof(obj));
90 obj.protocol = QUP_MINICORE_I2C_MASTER;
91 obj.p.iic.addr = slave;
92 obj.p.iic.data_len = data_len;
93 obj.p.iic.data = data;
94 qup_ret = qup_send_data(gsbi_id, &obj, stop_seq);
95
96 if (QUP_SUCCESS != qup_ret)
97 return 1;
98 else
99 return 0;
100}
101
Vadim Bendebury6fe4e5e2014-12-06 10:44:58 -0800102static int i2c_init(unsigned bus)
Daisuke Nojiri05949142014-11-21 15:33:26 -0800103{
Vadim Bendebury6fe4e5e2014-12-06 10:44:58 -0800104 unsigned gsbi_id = bus;
Sourabh Banerjee89208652015-02-19 16:43:26 +0530105 qup_config_t *qup_config;
Vadim Bendebury6fe4e5e2014-12-06 10:44:58 -0800106
Sourabh Banerjee89208652015-02-19 16:43:26 +0530107 switch (gsbi_id) {
108 case GSBI_ID_1:
109 qup_config = &gsbi1_qup_config;
110 break;
111 case GSBI_ID_4:
112 qup_config = &gsbi4_qup_config;
113 break;
114 case GSBI_ID_7:
115 qup_config = &gsbi7_qup_config;
116 break;
117 default:
118 printk(BIOS_ERR, "QUP configuration not defind for GSBI%d.\n",
119 gsbi_id);
120 return 1;
121 }
122
123 if (qup_config->initialized)
Vadim Bendebury6fe4e5e2014-12-06 10:44:58 -0800124 return 0;
125
126 if (gsbi_init(gsbi_id, GSBI_PROTO_I2C_ONLY)) {
127 printk(BIOS_ERR, "failed to initialize gsbi\n");
128 return 1;
129 }
130
Sourabh Banerjee89208652015-02-19 16:43:26 +0530131 if (qup_init(gsbi_id, qup_config)) {
Vadim Bendebury6fe4e5e2014-12-06 10:44:58 -0800132 printk(BIOS_ERR, "failed to initialize qup\n");
133 return 1;
134 }
135
136 if (qup_reset_i2c_master_status(gsbi_id)) {
137 printk(BIOS_ERR, "failed to reset i2c master status\n");
138 return 1;
139 }
140
Sourabh Banerjee89208652015-02-19 16:43:26 +0530141 qup_config->initialized = 1;
Vadim Bendebury6fe4e5e2014-12-06 10:44:58 -0800142 return 0;
143}
144
145int platform_i2c_transfer(unsigned bus, struct i2c_seg *segments, int seg_count)
146{
147 struct i2c_seg *seg = segments;
Daisuke Nojiri05949142014-11-21 15:33:26 -0800148 int ret = 0;
149
Vadim Bendebury6fe4e5e2014-12-06 10:44:58 -0800150 if (i2c_init(bus))
151 return 1;
Daisuke Nojiri05949142014-11-21 15:33:26 -0800152
Sourabh Banerjee54cc8ba2015-02-27 19:11:19 +0530153 while (!ret && seg_count--) {
Daisuke Nojiri05949142014-11-21 15:33:26 -0800154 if (seg->read)
Vadim Bendebury6fe4e5e2014-12-06 10:44:58 -0800155 ret = i2c_read(bus, seg->chip, seg->buf, seg->len);
Daisuke Nojiri05949142014-11-21 15:33:26 -0800156 else
Vadim Bendebury6fe4e5e2014-12-06 10:44:58 -0800157 ret = i2c_write(bus, seg->chip, seg->buf, seg->len,
158 (seg_count ? 0 : 1));
Daisuke Nojiri05949142014-11-21 15:33:26 -0800159 seg++;
160 }
161
Vadim Bendebury6fe4e5e2014-12-06 10:44:58 -0800162 if (ret) {
163 qup_set_state(bus, QUP_STATE_RESET);
Daisuke Nojiri05949142014-11-21 15:33:26 -0800164 return 1;
165 }
166
167 return 0;
168}