| 1 |
|
| 2 |
|
| 3 |
#include <console/console.h> |
| 4 |
#include <device/i2c_simple.h> |
| 5 |
#include <string.h> |
| 6 |
#include <soc/blsp.h> |
| 7 |
#include <soc/qup.h> |
| 8 |
#include <soc/gpio.h> |
| 9 |
|
| 10 |
static qup_config_t blsp1_qup0_config = { |
| 11 |
QUP_MINICORE_I2C_MASTER, |
| 12 |
400000, |
| 13 |
19200000, |
| 14 |
QUP_MODE_BLOCK, |
| 15 |
0 |
| 16 |
}; |
| 17 |
|
| 18 |
static qup_config_t blsp1_qup1_config = { |
| 19 |
QUP_MINICORE_I2C_MASTER, |
| 20 |
400000, |
| 21 |
19200000, |
| 22 |
QUP_MODE_BLOCK, |
| 23 |
0 |
| 24 |
}; |
| 25 |
|
| 26 |
static qup_config_t blsp1_qup2_config = { |
| 27 |
QUP_MINICORE_I2C_MASTER, |
| 28 |
400000, |
| 29 |
19200000, |
| 30 |
QUP_MODE_BLOCK, |
| 31 |
0 |
| 32 |
}; |
| 33 |
|
| 34 |
static qup_config_t blsp1_qup3_config = { |
| 35 |
QUP_MINICORE_I2C_MASTER, |
| 36 |
400000, |
| 37 |
19200000, |
| 38 |
QUP_MODE_BLOCK, |
| 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_ret != QUP_SUCCESS) |
| 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_ret != QUP_SUCCESS) |
| 75 |
return 1; |
| 76 |
else |
| 77 |
return 0; |
| 78 |
} |
| 79 |
|
| 80 |
static int i2c_init(blsp_qup_id_t id) |
| 81 |
{ |
| 82 |
qup_config_t *qup_config; |
| 83 |
|
| 84 |
switch (id) { |
| 85 |
case BLSP_QUP_ID_0: |
| 86 |
qup_config = &blsp1_qup0_config; |
| 87 |
break; |
| 88 |
case BLSP_QUP_ID_1: |
| 89 |
qup_config = &blsp1_qup1_config; |
| 90 |
break; |
| 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; |
| 96 |
break; |
| 97 |
default: |
| 98 |
printk(BIOS_ERR, "QUP configuration not defined for BLSP%d.\n", |
| 99 |
id); |
| 100 |
return 1; |
| 101 |
} |
| 102 |
|
| 103 |
if (qup_config->initialized) |
| 104 |
return 0; |
| 105 |
|
| 106 |
if (blsp_i2c_init(id)) { |
| 107 |
printk(BIOS_ERR, "failed to initialize blsp\n"); |
| 108 |
return 1; |
| 109 |
} |
| 110 |
|
| 111 |
if (qup_init(id, qup_config)) { |
| 112 |
printk(BIOS_ERR, "failed to initialize qup\n"); |
| 113 |
return 1; |
| 114 |
} |
| 115 |
|
| 116 |
if (qup_reset_i2c_master_status(id)) { |
| 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 |
|
| 125 |
int platform_i2c_transfer(unsigned int bus, struct i2c_msg *segments, |
| 126 |
int seg_count) |
| 127 |
{ |
| 128 |
struct i2c_msg *seg = segments; |
| 129 |
int ret = 0; |
| 130 |
|
| 131 |
if (i2c_init(bus)) |
| 132 |
return 1; |
| 133 |
|
| 134 |
while (!ret && seg_count--) { |
| 135 |
if (seg->flags & I2C_M_RD) |
| 136 |
ret = i2c_read(bus, seg->slave, seg->buf, seg->len); |
| 137 |
else |
| 138 |
ret = i2c_write(bus, seg->slave, seg->buf, seg->len, |
| 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 |
} |