Daisuke Nojiri | 0594914 | 2014-11-21 15:33:26 -0800 | [diff] [blame] | 1 | /* |
Vadim Bendebury | 6fe4e5e | 2014-12-06 10:44:58 -0800 | [diff] [blame] | 2 | * This file is part of the coreboot project. |
Daisuke Nojiri | 0594914 | 2014-11-21 15:33:26 -0800 | [diff] [blame] | 3 | * |
Sourabh Banerjee | 8920865 | 2015-02-19 16:43:26 +0530 | [diff] [blame^] | 4 | * Copyright (C) 2014 - 2015 The Linux Foundation. All rights reserved. |
Daisuke Nojiri | 0594914 | 2014-11-21 15:33:26 -0800 | [diff] [blame] | 5 | * |
| 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 Bendebury | 6fe4e5e | 2014-12-06 10:44:58 -0800 | [diff] [blame] | 30 | #include <arch/io.h> |
Daisuke Nojiri | 0594914 | 2014-11-21 15:33:26 -0800 | [diff] [blame] | 31 | #include <assert.h> |
Vadim Bendebury | 6fe4e5e | 2014-12-06 10:44:58 -0800 | [diff] [blame] | 32 | #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 Nojiri | 0594914 | 2014-11-21 15:33:26 -0800 | [diff] [blame] | 39 | |
Sourabh Banerjee | 8920865 | 2015-02-19 16:43:26 +0530 | [diff] [blame^] | 40 | static qup_config_t gsbi1_qup_config = { |
Vadim Bendebury | 6fe4e5e | 2014-12-06 10:44:58 -0800 | [diff] [blame] | 41 | QUP_MINICORE_I2C_MASTER, |
| 42 | 100000, |
| 43 | 24000000, |
Sourabh Banerjee | 8920865 | 2015-02-19 16:43:26 +0530 | [diff] [blame^] | 44 | QUP_MODE_FIFO, |
| 45 | 0 |
| 46 | }; |
| 47 | |
| 48 | static qup_config_t gsbi4_qup_config = { |
| 49 | QUP_MINICORE_I2C_MASTER, |
| 50 | 100000, |
| 51 | 24000000, |
| 52 | QUP_MODE_FIFO, |
| 53 | 0 |
| 54 | }; |
| 55 | |
| 56 | static qup_config_t gsbi7_qup_config = { |
| 57 | QUP_MINICORE_I2C_MASTER, |
| 58 | 100000, |
| 59 | 24000000, |
| 60 | QUP_MODE_FIFO, |
| 61 | 0 |
Vadim Bendebury | 6fe4e5e | 2014-12-06 10:44:58 -0800 | [diff] [blame] | 62 | }; |
Daisuke Nojiri | 0594914 | 2014-11-21 15:33:26 -0800 | [diff] [blame] | 63 | |
| 64 | static int i2c_read(uint32_t gsbi_id, uint8_t slave, |
Vadim Bendebury | 6fe4e5e | 2014-12-06 10:44:58 -0800 | [diff] [blame] | 65 | uint8_t *data, int data_len) |
Daisuke Nojiri | 0594914 | 2014-11-21 15:33:26 -0800 | [diff] [blame] | 66 | { |
| 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 | |
| 83 | static 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 Bendebury | 6fe4e5e | 2014-12-06 10:44:58 -0800 | [diff] [blame] | 102 | static int i2c_init(unsigned bus) |
Daisuke Nojiri | 0594914 | 2014-11-21 15:33:26 -0800 | [diff] [blame] | 103 | { |
Vadim Bendebury | 6fe4e5e | 2014-12-06 10:44:58 -0800 | [diff] [blame] | 104 | unsigned gsbi_id = bus; |
Sourabh Banerjee | 8920865 | 2015-02-19 16:43:26 +0530 | [diff] [blame^] | 105 | qup_config_t *qup_config; |
Vadim Bendebury | 6fe4e5e | 2014-12-06 10:44:58 -0800 | [diff] [blame] | 106 | |
Sourabh Banerjee | 8920865 | 2015-02-19 16:43:26 +0530 | [diff] [blame^] | 107 | 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 Bendebury | 6fe4e5e | 2014-12-06 10:44:58 -0800 | [diff] [blame] | 124 | 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 Banerjee | 8920865 | 2015-02-19 16:43:26 +0530 | [diff] [blame^] | 131 | if (qup_init(gsbi_id, qup_config)) { |
Vadim Bendebury | 6fe4e5e | 2014-12-06 10:44:58 -0800 | [diff] [blame] | 132 | 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 Banerjee | 8920865 | 2015-02-19 16:43:26 +0530 | [diff] [blame^] | 141 | qup_config->initialized = 1; |
Vadim Bendebury | 6fe4e5e | 2014-12-06 10:44:58 -0800 | [diff] [blame] | 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | int platform_i2c_transfer(unsigned bus, struct i2c_seg *segments, int seg_count) |
| 146 | { |
| 147 | struct i2c_seg *seg = segments; |
Daisuke Nojiri | 0594914 | 2014-11-21 15:33:26 -0800 | [diff] [blame] | 148 | int ret = 0; |
| 149 | |
Vadim Bendebury | 6fe4e5e | 2014-12-06 10:44:58 -0800 | [diff] [blame] | 150 | if (i2c_init(bus)) |
| 151 | return 1; |
Daisuke Nojiri | 0594914 | 2014-11-21 15:33:26 -0800 | [diff] [blame] | 152 | |
| 153 | while (seg_count--) { |
| 154 | if (seg->read) |
Vadim Bendebury | 6fe4e5e | 2014-12-06 10:44:58 -0800 | [diff] [blame] | 155 | ret = i2c_read(bus, seg->chip, seg->buf, seg->len); |
Daisuke Nojiri | 0594914 | 2014-11-21 15:33:26 -0800 | [diff] [blame] | 156 | else |
Vadim Bendebury | 6fe4e5e | 2014-12-06 10:44:58 -0800 | [diff] [blame] | 157 | ret = i2c_write(bus, seg->chip, seg->buf, seg->len, |
| 158 | (seg_count ? 0 : 1)); |
Daisuke Nojiri | 0594914 | 2014-11-21 15:33:26 -0800 | [diff] [blame] | 159 | seg++; |
| 160 | } |
| 161 | |
Vadim Bendebury | 6fe4e5e | 2014-12-06 10:44:58 -0800 | [diff] [blame] | 162 | if (ret) { |
| 163 | qup_set_state(bus, QUP_STATE_RESET); |
Daisuke Nojiri | 0594914 | 2014-11-21 15:33:26 -0800 | [diff] [blame] | 164 | return 1; |
| 165 | } |
| 166 | |
| 167 | return 0; |
| 168 | } |