Patrick Georgi | 7051707 | 2020-05-10 18:47:05 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: BSD-3-Clause */ |
Varadarajan Narayanan | a6935c2 | 2016-03-02 16:57:10 +0530 | [diff] [blame] | 2 | |
Varadarajan Narayanan | a6935c2 | 2016-03-02 16:57:10 +0530 | [diff] [blame] | 3 | #include <console/console.h> |
Nico Huber | 0f2dd1e | 2017-08-01 14:02:40 +0200 | [diff] [blame] | 4 | #include <device/i2c_simple.h> |
Varadarajan Narayanan | a6935c2 | 2016-03-02 16:57:10 +0530 | [diff] [blame] | 5 | #include <string.h> |
Varadarajan Narayanan | 2596764 | 2016-03-08 15:02:56 +0530 | [diff] [blame] | 6 | #include <soc/blsp.h> |
Varadarajan Narayanan | a6935c2 | 2016-03-02 16:57:10 +0530 | [diff] [blame] | 7 | #include <soc/qup.h> |
Varadarajan Narayanan | 2596764 | 2016-03-08 15:02:56 +0530 | [diff] [blame] | 8 | #include <soc/gpio.h> |
Varadarajan Narayanan | a6935c2 | 2016-03-02 16:57:10 +0530 | [diff] [blame] | 9 | |
Varadarajan Narayanan | 2596764 | 2016-03-08 15:02:56 +0530 | [diff] [blame] | 10 | static qup_config_t blsp1_qup0_config = { |
Varadarajan Narayanan | a6935c2 | 2016-03-02 16:57:10 +0530 | [diff] [blame] | 11 | QUP_MINICORE_I2C_MASTER, |
| 12 | 100000, |
Varadarajan Narayanan | 2596764 | 2016-03-08 15:02:56 +0530 | [diff] [blame] | 13 | 19050000, |
Varadarajan Narayanan | e6a2ecf | 2016-05-17 11:18:30 +0530 | [diff] [blame] | 14 | QUP_MODE_BLOCK, |
Varadarajan Narayanan | a6935c2 | 2016-03-02 16:57:10 +0530 | [diff] [blame] | 15 | 0 |
| 16 | }; |
| 17 | |
Varadarajan Narayanan | 2596764 | 2016-03-08 15:02:56 +0530 | [diff] [blame] | 18 | static qup_config_t blsp1_qup1_config = { |
Varadarajan Narayanan | a6935c2 | 2016-03-02 16:57:10 +0530 | [diff] [blame] | 19 | QUP_MINICORE_I2C_MASTER, |
| 20 | 100000, |
Varadarajan Narayanan | 2596764 | 2016-03-08 15:02:56 +0530 | [diff] [blame] | 21 | 19050000, |
Varadarajan Narayanan | e6a2ecf | 2016-05-17 11:18:30 +0530 | [diff] [blame] | 22 | QUP_MODE_BLOCK, |
Varadarajan Narayanan | a6935c2 | 2016-03-02 16:57:10 +0530 | [diff] [blame] | 23 | 0 |
| 24 | }; |
| 25 | |
Varadarajan Narayanan | 2596764 | 2016-03-08 15:02:56 +0530 | [diff] [blame] | 26 | static qup_config_t blsp1_qup2_config = { |
Varadarajan Narayanan | a6935c2 | 2016-03-02 16:57:10 +0530 | [diff] [blame] | 27 | QUP_MINICORE_I2C_MASTER, |
| 28 | 100000, |
Varadarajan Narayanan | 2596764 | 2016-03-08 15:02:56 +0530 | [diff] [blame] | 29 | 19050000, |
Varadarajan Narayanan | e6a2ecf | 2016-05-17 11:18:30 +0530 | [diff] [blame] | 30 | QUP_MODE_BLOCK, |
Varadarajan Narayanan | 2596764 | 2016-03-08 15:02:56 +0530 | [diff] [blame] | 31 | 0 |
| 32 | }; |
| 33 | |
| 34 | static qup_config_t blsp1_qup3_config = { |
| 35 | QUP_MINICORE_I2C_MASTER, |
| 36 | 100000, |
| 37 | 19050000, |
Varadarajan Narayanan | e6a2ecf | 2016-05-17 11:18:30 +0530 | [diff] [blame] | 38 | QUP_MODE_BLOCK, |
Varadarajan Narayanan | a6935c2 | 2016-03-02 16:57:10 +0530 | [diff] [blame] | 39 | 0 |
| 40 | }; |
| 41 | |
| 42 | static int i2c_read(uint32_t gsbi_id, uint8_t slave, |
| 43 | uint8_t *data, int data_len) |
| 44 | { |
| 45 | qup_data_t obj; |
| 46 | qup_return_t qup_ret = 0; |
| 47 | |
| 48 | memset(&obj, 0, sizeof(obj)); |
| 49 | obj.protocol = QUP_MINICORE_I2C_MASTER; |
| 50 | obj.p.iic.addr = slave; |
| 51 | obj.p.iic.data_len = data_len; |
| 52 | obj.p.iic.data = data; |
| 53 | qup_ret = qup_recv_data(gsbi_id, &obj); |
| 54 | |
| 55 | if (QUP_SUCCESS != qup_ret) |
| 56 | return 1; |
| 57 | else |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | static int i2c_write(uint32_t gsbi_id, uint8_t slave, |
| 62 | uint8_t *data, int data_len, uint8_t stop_seq) |
| 63 | { |
| 64 | qup_data_t obj; |
| 65 | qup_return_t qup_ret = 0; |
| 66 | |
| 67 | memset(&obj, 0, sizeof(obj)); |
| 68 | obj.protocol = QUP_MINICORE_I2C_MASTER; |
| 69 | obj.p.iic.addr = slave; |
| 70 | obj.p.iic.data_len = data_len; |
| 71 | obj.p.iic.data = data; |
| 72 | qup_ret = qup_send_data(gsbi_id, &obj, stop_seq); |
| 73 | |
| 74 | if (QUP_SUCCESS != qup_ret) |
| 75 | return 1; |
| 76 | else |
| 77 | return 0; |
| 78 | } |
| 79 | |
Varadarajan Narayanan | 2596764 | 2016-03-08 15:02:56 +0530 | [diff] [blame] | 80 | static int i2c_init(blsp_qup_id_t id) |
Varadarajan Narayanan | a6935c2 | 2016-03-02 16:57:10 +0530 | [diff] [blame] | 81 | { |
Varadarajan Narayanan | a6935c2 | 2016-03-02 16:57:10 +0530 | [diff] [blame] | 82 | qup_config_t *qup_config; |
| 83 | |
Varadarajan Narayanan | 2596764 | 2016-03-08 15:02:56 +0530 | [diff] [blame] | 84 | switch (id) { |
| 85 | case BLSP_QUP_ID_0: |
| 86 | qup_config = &blsp1_qup0_config; |
Varadarajan Narayanan | a6935c2 | 2016-03-02 16:57:10 +0530 | [diff] [blame] | 87 | break; |
Varadarajan Narayanan | 2596764 | 2016-03-08 15:02:56 +0530 | [diff] [blame] | 88 | case BLSP_QUP_ID_1: |
| 89 | qup_config = &blsp1_qup1_config; |
Varadarajan Narayanan | a6935c2 | 2016-03-02 16:57:10 +0530 | [diff] [blame] | 90 | break; |
Varadarajan Narayanan | 2596764 | 2016-03-08 15:02:56 +0530 | [diff] [blame] | 91 | case BLSP_QUP_ID_2: |
| 92 | qup_config = &blsp1_qup2_config; |
| 93 | break; |
| 94 | case BLSP_QUP_ID_3: |
| 95 | qup_config = &blsp1_qup3_config; |
Varadarajan Narayanan | a6935c2 | 2016-03-02 16:57:10 +0530 | [diff] [blame] | 96 | break; |
| 97 | default: |
Varadarajan Narayanan | 2596764 | 2016-03-08 15:02:56 +0530 | [diff] [blame] | 98 | printk(BIOS_ERR, "QUP configuration not defined for BLSP%d.\n", |
| 99 | id); |
Varadarajan Narayanan | a6935c2 | 2016-03-02 16:57:10 +0530 | [diff] [blame] | 100 | return 1; |
| 101 | } |
| 102 | |
| 103 | if (qup_config->initialized) |
| 104 | return 0; |
| 105 | |
Varadarajan Narayanan | 2596764 | 2016-03-08 15:02:56 +0530 | [diff] [blame] | 106 | if (blsp_i2c_init(id)) { |
| 107 | printk(BIOS_ERR, "failed to initialize blsp\n"); |
Varadarajan Narayanan | a6935c2 | 2016-03-02 16:57:10 +0530 | [diff] [blame] | 108 | return 1; |
| 109 | } |
| 110 | |
Varadarajan Narayanan | 2596764 | 2016-03-08 15:02:56 +0530 | [diff] [blame] | 111 | if (qup_init(id, qup_config)) { |
Varadarajan Narayanan | a6935c2 | 2016-03-02 16:57:10 +0530 | [diff] [blame] | 112 | printk(BIOS_ERR, "failed to initialize qup\n"); |
| 113 | return 1; |
| 114 | } |
| 115 | |
Varadarajan Narayanan | 2596764 | 2016-03-08 15:02:56 +0530 | [diff] [blame] | 116 | if (qup_reset_i2c_master_status(id)) { |
Varadarajan Narayanan | a6935c2 | 2016-03-02 16:57:10 +0530 | [diff] [blame] | 117 | printk(BIOS_ERR, "failed to reset i2c master status\n"); |
| 118 | return 1; |
| 119 | } |
| 120 | |
| 121 | qup_config->initialized = 1; |
| 122 | return 0; |
| 123 | } |
| 124 | |
Martin Roth | 57e8909 | 2019-10-23 21:45:23 -0600 | [diff] [blame] | 125 | int platform_i2c_transfer(unsigned int bus, struct i2c_msg *segments, |
Nico Huber | 029dfff | 2017-07-12 17:59:16 +0200 | [diff] [blame] | 126 | int seg_count) |
Varadarajan Narayanan | a6935c2 | 2016-03-02 16:57:10 +0530 | [diff] [blame] | 127 | { |
Nico Huber | 029dfff | 2017-07-12 17:59:16 +0200 | [diff] [blame] | 128 | struct i2c_msg *seg = segments; |
Varadarajan Narayanan | a6935c2 | 2016-03-02 16:57:10 +0530 | [diff] [blame] | 129 | int ret = 0; |
| 130 | |
| 131 | if (i2c_init(bus)) |
| 132 | return 1; |
| 133 | |
| 134 | while (!ret && seg_count--) { |
Nico Huber | 029dfff | 2017-07-12 17:59:16 +0200 | [diff] [blame] | 135 | if (seg->flags & I2C_M_RD) |
| 136 | ret = i2c_read(bus, seg->slave, seg->buf, seg->len); |
Varadarajan Narayanan | a6935c2 | 2016-03-02 16:57:10 +0530 | [diff] [blame] | 137 | else |
Nico Huber | 029dfff | 2017-07-12 17:59:16 +0200 | [diff] [blame] | 138 | ret = i2c_write(bus, seg->slave, seg->buf, seg->len, |
Varadarajan Narayanan | a6935c2 | 2016-03-02 16:57:10 +0530 | [diff] [blame] | 139 | (seg_count ? 0 : 1)); |
| 140 | seg++; |
| 141 | } |
| 142 | |
| 143 | if (ret) { |
| 144 | qup_set_state(bus, QUP_STATE_RESET); |
| 145 | return 1; |
| 146 | } |
| 147 | |
| 148 | return 0; |
| 149 | } |