aspeed_i2c.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650
  1. /*
  2. * ARM Aspeed I2C controller
  3. *
  4. * Copyright (C) 2016 IBM Corp.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. #include "qemu/osdep.h"
  21. #include "hw/sysbus.h"
  22. #include "migration/vmstate.h"
  23. #include "qemu/cutils.h"
  24. #include "qemu/log.h"
  25. #include "qemu/module.h"
  26. #include "qemu/error-report.h"
  27. #include "qapi/error.h"
  28. #include "hw/i2c/aspeed_i2c.h"
  29. #include "hw/irq.h"
  30. #include "hw/qdev-properties.h"
  31. #include "hw/registerfields.h"
  32. #include "trace.h"
  33. /* Enable SLAVE_ADDR_RX_MATCH always */
  34. #define R_I2CD_INTR_STS_ALWAYS_ENABLE R_I2CD_INTR_STS_SLAVE_ADDR_RX_MATCH_MASK
  35. static inline void aspeed_i2c_bus_raise_interrupt(AspeedI2CBus *bus)
  36. {
  37. AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(bus->controller);
  38. uint32_t reg_intr_sts = aspeed_i2c_bus_intr_sts_offset(bus);
  39. uint32_t intr_ctrl_reg = aspeed_i2c_bus_intr_ctrl_offset(bus);
  40. uint32_t intr_ctrl_mask = bus->regs[intr_ctrl_reg] |
  41. R_I2CD_INTR_STS_ALWAYS_ENABLE;
  42. bool raise_irq;
  43. if (trace_event_get_state_backends(TRACE_ASPEED_I2C_BUS_RAISE_INTERRUPT)) {
  44. g_autofree char *buf = g_strdup_printf("%s%s%s%s%s%s%s",
  45. aspeed_i2c_bus_pkt_mode_en(bus) &&
  46. ARRAY_FIELD_EX32(bus->regs, I2CM_INTR_STS, PKT_CMD_DONE) ?
  47. "pktdone|" : "",
  48. SHARED_ARRAY_FIELD_EX32(bus->regs, reg_intr_sts, TX_NAK) ?
  49. "nak|" : "",
  50. SHARED_ARRAY_FIELD_EX32(bus->regs, reg_intr_sts, TX_ACK) ?
  51. "ack|" : "",
  52. SHARED_ARRAY_FIELD_EX32(bus->regs, reg_intr_sts, RX_DONE) ?
  53. "done|" : "",
  54. ARRAY_FIELD_EX32(bus->regs, I2CD_INTR_STS, SLAVE_ADDR_RX_MATCH) ?
  55. "slave-match|" : "",
  56. SHARED_ARRAY_FIELD_EX32(bus->regs, reg_intr_sts, NORMAL_STOP) ?
  57. "stop|" : "",
  58. SHARED_ARRAY_FIELD_EX32(bus->regs, reg_intr_sts, ABNORMAL) ?
  59. "abnormal" : "");
  60. trace_aspeed_i2c_bus_raise_interrupt(bus->regs[reg_intr_sts], buf);
  61. }
  62. raise_irq = bus->regs[reg_intr_sts] & intr_ctrl_mask ;
  63. /* In packet mode we don't mask off INTR_STS */
  64. if (!aspeed_i2c_bus_pkt_mode_en(bus)) {
  65. bus->regs[reg_intr_sts] &= intr_ctrl_mask;
  66. }
  67. if (raise_irq) {
  68. bus->controller->intr_status |= 1 << bus->id;
  69. qemu_irq_raise(aic->bus_get_irq(bus));
  70. }
  71. }
  72. static inline void aspeed_i2c_bus_raise_slave_interrupt(AspeedI2CBus *bus)
  73. {
  74. AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(bus->controller);
  75. if (!bus->regs[R_I2CS_INTR_STS]) {
  76. return;
  77. }
  78. bus->controller->intr_status |= 1 << bus->id;
  79. qemu_irq_raise(aic->bus_get_irq(bus));
  80. }
  81. static uint64_t aspeed_i2c_bus_old_read(AspeedI2CBus *bus, hwaddr offset,
  82. unsigned size)
  83. {
  84. AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(bus->controller);
  85. uint64_t value = bus->regs[offset / sizeof(*bus->regs)];
  86. switch (offset) {
  87. case A_I2CD_FUN_CTRL:
  88. case A_I2CD_AC_TIMING1:
  89. case A_I2CD_AC_TIMING2:
  90. case A_I2CD_INTR_CTRL:
  91. case A_I2CD_INTR_STS:
  92. case A_I2CD_DEV_ADDR:
  93. case A_I2CD_POOL_CTRL:
  94. case A_I2CD_BYTE_BUF:
  95. /* Value is already set, don't do anything. */
  96. break;
  97. case A_I2CD_CMD:
  98. value = SHARED_FIELD_DP32(value, BUS_BUSY_STS, i2c_bus_busy(bus->bus));
  99. break;
  100. case A_I2CD_DMA_ADDR:
  101. if (!aic->has_dma) {
  102. qemu_log_mask(LOG_GUEST_ERROR, "%s: No DMA support\n", __func__);
  103. value = -1;
  104. break;
  105. }
  106. value = extract64(bus->dma_dram_offset, 0, 32);
  107. break;
  108. case A_I2CD_DMA_LEN:
  109. if (!aic->has_dma) {
  110. qemu_log_mask(LOG_GUEST_ERROR, "%s: No DMA support\n", __func__);
  111. value = -1;
  112. }
  113. break;
  114. default:
  115. qemu_log_mask(LOG_GUEST_ERROR,
  116. "%s: Bad offset 0x%" HWADDR_PRIx "\n", __func__, offset);
  117. value = -1;
  118. break;
  119. }
  120. trace_aspeed_i2c_bus_read(bus->id, offset, size, value);
  121. return value;
  122. }
  123. static uint64_t aspeed_i2c_bus_new_read(AspeedI2CBus *bus, hwaddr offset,
  124. unsigned size)
  125. {
  126. AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(bus->controller);
  127. uint64_t value = bus->regs[offset / sizeof(*bus->regs)];
  128. switch (offset) {
  129. case A_I2CC_FUN_CTRL:
  130. case A_I2CC_AC_TIMING:
  131. case A_I2CC_POOL_CTRL:
  132. case A_I2CM_INTR_CTRL:
  133. case A_I2CM_INTR_STS:
  134. case A_I2CC_MS_TXRX_BYTE_BUF:
  135. case A_I2CM_DMA_LEN:
  136. case A_I2CM_DMA_TX_ADDR:
  137. case A_I2CM_DMA_RX_ADDR:
  138. case A_I2CM_DMA_LEN_STS:
  139. case A_I2CC_DMA_LEN:
  140. case A_I2CS_DEV_ADDR:
  141. case A_I2CS_DMA_RX_ADDR:
  142. case A_I2CS_DMA_LEN:
  143. case A_I2CS_CMD:
  144. case A_I2CS_INTR_CTRL:
  145. case A_I2CS_DMA_LEN_STS:
  146. /* Value is already set, don't do anything. */
  147. break;
  148. case A_I2CC_DMA_ADDR:
  149. value = extract64(bus->dma_dram_offset, 0, 32);
  150. break;
  151. case A_I2CS_INTR_STS:
  152. break;
  153. case A_I2CM_CMD:
  154. value = SHARED_FIELD_DP32(value, BUS_BUSY_STS, i2c_bus_busy(bus->bus));
  155. break;
  156. case A_I2CM_DMA_TX_ADDR_HI:
  157. case A_I2CM_DMA_RX_ADDR_HI:
  158. case A_I2CS_DMA_TX_ADDR_HI:
  159. case A_I2CS_DMA_RX_ADDR_HI:
  160. if (!aic->has_dma64) {
  161. qemu_log_mask(LOG_GUEST_ERROR, "%s: No DMA 64 bits support\n",
  162. __func__);
  163. value = -1;
  164. }
  165. break;
  166. default:
  167. qemu_log_mask(LOG_GUEST_ERROR,
  168. "%s: Bad offset 0x%" HWADDR_PRIx "\n", __func__, offset);
  169. value = -1;
  170. break;
  171. }
  172. trace_aspeed_i2c_bus_read(bus->id, offset, size, value);
  173. return value;
  174. }
  175. static uint64_t aspeed_i2c_bus_read(void *opaque, hwaddr offset,
  176. unsigned size)
  177. {
  178. AspeedI2CBus *bus = opaque;
  179. if (aspeed_i2c_is_new_mode(bus->controller)) {
  180. return aspeed_i2c_bus_new_read(bus, offset, size);
  181. }
  182. return aspeed_i2c_bus_old_read(bus, offset, size);
  183. }
  184. static void aspeed_i2c_set_state(AspeedI2CBus *bus, uint8_t state)
  185. {
  186. if (aspeed_i2c_is_new_mode(bus->controller)) {
  187. SHARED_ARRAY_FIELD_DP32(bus->regs, R_I2CC_MS_TXRX_BYTE_BUF, TX_STATE,
  188. state);
  189. } else {
  190. SHARED_ARRAY_FIELD_DP32(bus->regs, R_I2CD_CMD, TX_STATE, state);
  191. }
  192. }
  193. static uint8_t aspeed_i2c_get_state(AspeedI2CBus *bus)
  194. {
  195. if (aspeed_i2c_is_new_mode(bus->controller)) {
  196. return SHARED_ARRAY_FIELD_EX32(bus->regs, R_I2CC_MS_TXRX_BYTE_BUF,
  197. TX_STATE);
  198. }
  199. return SHARED_ARRAY_FIELD_EX32(bus->regs, R_I2CD_CMD, TX_STATE);
  200. }
  201. static int aspeed_i2c_dma_read(AspeedI2CBus *bus, uint8_t *data)
  202. {
  203. MemTxResult result;
  204. AspeedI2CState *s = bus->controller;
  205. uint32_t reg_dma_len = aspeed_i2c_bus_dma_len_offset(bus);
  206. result = address_space_read(&s->dram_as, bus->dma_dram_offset,
  207. MEMTXATTRS_UNSPECIFIED, data, 1);
  208. if (result != MEMTX_OK) {
  209. qemu_log_mask(LOG_GUEST_ERROR,
  210. "%s: DRAM read failed @%" PRIx64 "\n",
  211. __func__, bus->dma_dram_offset);
  212. return -1;
  213. }
  214. bus->dma_dram_offset++;
  215. bus->regs[reg_dma_len]--;
  216. return 0;
  217. }
  218. static int aspeed_i2c_bus_send(AspeedI2CBus *bus)
  219. {
  220. AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(bus->controller);
  221. int ret = -1;
  222. int i;
  223. uint32_t reg_cmd = aspeed_i2c_bus_cmd_offset(bus);
  224. uint32_t reg_pool_ctrl = aspeed_i2c_bus_pool_ctrl_offset(bus);
  225. uint32_t reg_byte_buf = aspeed_i2c_bus_byte_buf_offset(bus);
  226. uint32_t reg_dma_len = aspeed_i2c_bus_dma_len_offset(bus);
  227. int pool_tx_count = SHARED_ARRAY_FIELD_EX32(bus->regs, reg_pool_ctrl,
  228. TX_COUNT) + 1;
  229. if (SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, TX_BUFF_EN)) {
  230. for (i = 0; i < pool_tx_count; i++) {
  231. uint8_t *pool_base = aic->bus_pool_base(bus);
  232. trace_aspeed_i2c_bus_send("BUF", i + 1, pool_tx_count,
  233. pool_base[i]);
  234. ret = i2c_send(bus->bus, pool_base[i]);
  235. if (ret) {
  236. break;
  237. }
  238. }
  239. SHARED_ARRAY_FIELD_DP32(bus->regs, reg_cmd, TX_BUFF_EN, 0);
  240. } else if (SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, TX_DMA_EN)) {
  241. /* In new mode, clear how many bytes we TXed */
  242. if (aspeed_i2c_is_new_mode(bus->controller)) {
  243. ARRAY_FIELD_DP32(bus->regs, I2CM_DMA_LEN_STS, TX_LEN, 0);
  244. }
  245. while (bus->regs[reg_dma_len]) {
  246. uint8_t data;
  247. aspeed_i2c_dma_read(bus, &data);
  248. trace_aspeed_i2c_bus_send("DMA", bus->regs[reg_dma_len],
  249. bus->regs[reg_dma_len], data);
  250. ret = i2c_send(bus->bus, data);
  251. if (ret) {
  252. break;
  253. }
  254. /* In new mode, keep track of how many bytes we TXed */
  255. if (aspeed_i2c_is_new_mode(bus->controller)) {
  256. ARRAY_FIELD_DP32(bus->regs, I2CM_DMA_LEN_STS, TX_LEN,
  257. ARRAY_FIELD_EX32(bus->regs, I2CM_DMA_LEN_STS,
  258. TX_LEN) + 1);
  259. }
  260. }
  261. SHARED_ARRAY_FIELD_DP32(bus->regs, reg_cmd, TX_DMA_EN, 0);
  262. } else {
  263. trace_aspeed_i2c_bus_send("BYTE", 0, 1,
  264. bus->regs[reg_byte_buf]);
  265. ret = i2c_send(bus->bus, bus->regs[reg_byte_buf]);
  266. }
  267. return ret;
  268. }
  269. static void aspeed_i2c_bus_recv(AspeedI2CBus *bus)
  270. {
  271. AspeedI2CState *s = bus->controller;
  272. AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(s);
  273. uint8_t data;
  274. int i;
  275. uint32_t reg_cmd = aspeed_i2c_bus_cmd_offset(bus);
  276. uint32_t reg_pool_ctrl = aspeed_i2c_bus_pool_ctrl_offset(bus);
  277. uint32_t reg_byte_buf = aspeed_i2c_bus_byte_buf_offset(bus);
  278. uint32_t reg_dma_len = aspeed_i2c_bus_dma_len_offset(bus);
  279. int pool_rx_count = SHARED_ARRAY_FIELD_EX32(bus->regs, reg_pool_ctrl,
  280. RX_SIZE) + 1;
  281. if (SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, RX_BUFF_EN)) {
  282. uint8_t *pool_base = aic->bus_pool_base(bus);
  283. if (SHARED_ARRAY_FIELD_EX32(bus->regs, reg_pool_ctrl,
  284. BUF_ORGANIZATION)) {
  285. pool_base += 16;
  286. }
  287. for (i = 0; i < pool_rx_count; i++) {
  288. pool_base[i] = i2c_recv(bus->bus);
  289. trace_aspeed_i2c_bus_recv("BUF", i + 1, pool_rx_count,
  290. pool_base[i]);
  291. }
  292. /* Update RX count */
  293. SHARED_ARRAY_FIELD_DP32(bus->regs, reg_pool_ctrl, RX_COUNT, i & 0xff);
  294. SHARED_ARRAY_FIELD_DP32(bus->regs, reg_cmd, RX_BUFF_EN, 0);
  295. } else if (SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, RX_DMA_EN)) {
  296. /* In new mode, clear how many bytes we RXed */
  297. if (aspeed_i2c_is_new_mode(bus->controller)) {
  298. ARRAY_FIELD_DP32(bus->regs, I2CM_DMA_LEN_STS, RX_LEN, 0);
  299. }
  300. while (bus->regs[reg_dma_len]) {
  301. MemTxResult result;
  302. data = i2c_recv(bus->bus);
  303. trace_aspeed_i2c_bus_recv("DMA", bus->regs[reg_dma_len],
  304. bus->regs[reg_dma_len], data);
  305. result = address_space_write(&s->dram_as, bus->dma_dram_offset,
  306. MEMTXATTRS_UNSPECIFIED, &data, 1);
  307. if (result != MEMTX_OK) {
  308. qemu_log_mask(LOG_GUEST_ERROR,
  309. "%s: DRAM write failed @%" PRIx64 "\n",
  310. __func__, bus->dma_dram_offset);
  311. return;
  312. }
  313. bus->dma_dram_offset++;
  314. bus->regs[reg_dma_len]--;
  315. /* In new mode, keep track of how many bytes we RXed */
  316. if (aspeed_i2c_is_new_mode(bus->controller)) {
  317. ARRAY_FIELD_DP32(bus->regs, I2CM_DMA_LEN_STS, RX_LEN,
  318. ARRAY_FIELD_EX32(bus->regs, I2CM_DMA_LEN_STS,
  319. RX_LEN) + 1);
  320. }
  321. }
  322. SHARED_ARRAY_FIELD_DP32(bus->regs, reg_cmd, RX_DMA_EN, 0);
  323. } else {
  324. data = i2c_recv(bus->bus);
  325. trace_aspeed_i2c_bus_recv("BYTE", 1, 1, bus->regs[reg_byte_buf]);
  326. SHARED_ARRAY_FIELD_DP32(bus->regs, reg_byte_buf, RX_BUF, data);
  327. }
  328. }
  329. static void aspeed_i2c_handle_rx_cmd(AspeedI2CBus *bus)
  330. {
  331. uint32_t reg_cmd = aspeed_i2c_bus_cmd_offset(bus);
  332. uint32_t reg_intr_sts = aspeed_i2c_bus_intr_sts_offset(bus);
  333. aspeed_i2c_set_state(bus, I2CD_MRXD);
  334. aspeed_i2c_bus_recv(bus);
  335. SHARED_ARRAY_FIELD_DP32(bus->regs, reg_intr_sts, RX_DONE, 1);
  336. if (SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, M_S_RX_CMD_LAST)) {
  337. i2c_nack(bus->bus);
  338. }
  339. SHARED_ARRAY_FIELD_DP32(bus->regs, reg_cmd, M_RX_CMD, 0);
  340. SHARED_ARRAY_FIELD_DP32(bus->regs, reg_cmd, M_S_RX_CMD_LAST, 0);
  341. aspeed_i2c_set_state(bus, I2CD_MACTIVE);
  342. }
  343. static uint8_t aspeed_i2c_get_addr(AspeedI2CBus *bus)
  344. {
  345. AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(bus->controller);
  346. uint32_t reg_byte_buf = aspeed_i2c_bus_byte_buf_offset(bus);
  347. uint32_t reg_cmd = aspeed_i2c_bus_cmd_offset(bus);
  348. if (aspeed_i2c_bus_pkt_mode_en(bus)) {
  349. return (ARRAY_FIELD_EX32(bus->regs, I2CM_CMD, PKT_DEV_ADDR) << 1) |
  350. SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, M_RX_CMD);
  351. }
  352. if (SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, TX_BUFF_EN)) {
  353. uint8_t *pool_base = aic->bus_pool_base(bus);
  354. return pool_base[0];
  355. } else if (SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, TX_DMA_EN)) {
  356. uint8_t data;
  357. aspeed_i2c_dma_read(bus, &data);
  358. return data;
  359. } else {
  360. return bus->regs[reg_byte_buf];
  361. }
  362. }
  363. static bool aspeed_i2c_check_sram(AspeedI2CBus *bus)
  364. {
  365. AspeedI2CState *s = bus->controller;
  366. AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(s);
  367. uint32_t reg_cmd = aspeed_i2c_bus_cmd_offset(bus);
  368. bool dma_en = SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, RX_DMA_EN) ||
  369. SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, TX_DMA_EN) ||
  370. SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, RX_BUFF_EN) ||
  371. SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, TX_BUFF_EN);
  372. if (!aic->check_sram) {
  373. return true;
  374. }
  375. /*
  376. * AST2500: SRAM must be enabled before using the Buffer Pool or
  377. * DMA mode.
  378. */
  379. if (!FIELD_EX32(s->ctrl_global, I2C_CTRL_GLOBAL, SRAM_EN) && dma_en) {
  380. qemu_log_mask(LOG_GUEST_ERROR, "%s: SRAM is not enabled\n", __func__);
  381. return false;
  382. }
  383. return true;
  384. }
  385. static void aspeed_i2c_bus_cmd_dump(AspeedI2CBus *bus)
  386. {
  387. g_autofree char *cmd_flags = NULL;
  388. uint32_t count;
  389. uint32_t reg_cmd = aspeed_i2c_bus_cmd_offset(bus);
  390. uint32_t reg_pool_ctrl = aspeed_i2c_bus_pool_ctrl_offset(bus);
  391. uint32_t reg_intr_sts = aspeed_i2c_bus_intr_sts_offset(bus);
  392. uint32_t reg_dma_len = aspeed_i2c_bus_dma_len_offset(bus);
  393. if (SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, RX_BUFF_EN)) {
  394. count = SHARED_ARRAY_FIELD_EX32(bus->regs, reg_pool_ctrl, TX_COUNT) + 1;
  395. } else if (SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, RX_DMA_EN)) {
  396. count = bus->regs[reg_dma_len];
  397. } else { /* BYTE mode */
  398. count = 1;
  399. }
  400. cmd_flags = g_strdup_printf("%s%s%s%s%s%s%s%s%s",
  401. SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, M_START_CMD) ? "start|" : "",
  402. SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, RX_DMA_EN) ? "rxdma|" : "",
  403. SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, TX_DMA_EN) ? "txdma|" : "",
  404. SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, RX_BUFF_EN) ? "rxbuf|" : "",
  405. SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, TX_BUFF_EN) ? "txbuf|" : "",
  406. SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, M_TX_CMD) ? "tx|" : "",
  407. SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, M_RX_CMD) ? "rx|" : "",
  408. SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, M_S_RX_CMD_LAST) ? "last|" : "",
  409. SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, M_STOP_CMD) ? "stop|" : "");
  410. trace_aspeed_i2c_bus_cmd(bus->regs[reg_cmd], cmd_flags, count,
  411. bus->regs[reg_intr_sts]);
  412. }
  413. /*
  414. * The state machine needs some refinement. It is only used to track
  415. * invalid STOP commands for the moment.
  416. */
  417. static void aspeed_i2c_bus_handle_cmd(AspeedI2CBus *bus, uint64_t value)
  418. {
  419. uint32_t reg_intr_sts = aspeed_i2c_bus_intr_sts_offset(bus);
  420. uint32_t reg_cmd = aspeed_i2c_bus_cmd_offset(bus);
  421. uint32_t reg_dma_len = aspeed_i2c_bus_dma_len_offset(bus);
  422. if (!aspeed_i2c_check_sram(bus)) {
  423. return;
  424. }
  425. if (trace_event_get_state_backends(TRACE_ASPEED_I2C_BUS_CMD)) {
  426. aspeed_i2c_bus_cmd_dump(bus);
  427. }
  428. if (SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, M_START_CMD)) {
  429. uint8_t state = aspeed_i2c_get_state(bus) & I2CD_MACTIVE ?
  430. I2CD_MSTARTR : I2CD_MSTART;
  431. uint8_t addr;
  432. aspeed_i2c_set_state(bus, state);
  433. addr = aspeed_i2c_get_addr(bus);
  434. if (i2c_start_transfer(bus->bus, extract32(addr, 1, 7),
  435. extract32(addr, 0, 1))) {
  436. SHARED_ARRAY_FIELD_DP32(bus->regs, reg_intr_sts, TX_NAK, 1);
  437. if (aspeed_i2c_bus_pkt_mode_en(bus)) {
  438. ARRAY_FIELD_DP32(bus->regs, I2CM_INTR_STS, PKT_CMD_FAIL, 1);
  439. }
  440. } else {
  441. /* START doesn't set TX_ACK in packet mode */
  442. if (!aspeed_i2c_bus_pkt_mode_en(bus)) {
  443. SHARED_ARRAY_FIELD_DP32(bus->regs, reg_intr_sts, TX_ACK, 1);
  444. }
  445. }
  446. SHARED_ARRAY_FIELD_DP32(bus->regs, reg_cmd, M_START_CMD, 0);
  447. if (SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, TX_DMA_EN)) {
  448. if (bus->regs[reg_dma_len] == 0) {
  449. SHARED_ARRAY_FIELD_DP32(bus->regs, reg_cmd, M_TX_CMD, 0);
  450. }
  451. } else if (!SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, TX_BUFF_EN)) {
  452. SHARED_ARRAY_FIELD_DP32(bus->regs, reg_cmd, M_TX_CMD, 0);
  453. }
  454. /* No slave found */
  455. if (!i2c_bus_busy(bus->bus)) {
  456. if (aspeed_i2c_bus_pkt_mode_en(bus)) {
  457. ARRAY_FIELD_DP32(bus->regs, I2CM_INTR_STS, PKT_CMD_FAIL, 1);
  458. ARRAY_FIELD_DP32(bus->regs, I2CM_INTR_STS, PKT_CMD_DONE, 1);
  459. }
  460. return;
  461. }
  462. aspeed_i2c_set_state(bus, I2CD_MACTIVE);
  463. }
  464. if (SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, M_TX_CMD)) {
  465. aspeed_i2c_set_state(bus, I2CD_MTXD);
  466. if (aspeed_i2c_bus_send(bus)) {
  467. SHARED_ARRAY_FIELD_DP32(bus->regs, reg_intr_sts, TX_NAK, 1);
  468. i2c_end_transfer(bus->bus);
  469. } else {
  470. SHARED_ARRAY_FIELD_DP32(bus->regs, reg_intr_sts, TX_ACK, 1);
  471. }
  472. SHARED_ARRAY_FIELD_DP32(bus->regs, reg_cmd, M_TX_CMD, 0);
  473. aspeed_i2c_set_state(bus, I2CD_MACTIVE);
  474. }
  475. if ((SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, M_RX_CMD) ||
  476. SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, M_S_RX_CMD_LAST)) &&
  477. !SHARED_ARRAY_FIELD_EX32(bus->regs, reg_intr_sts, RX_DONE)) {
  478. aspeed_i2c_handle_rx_cmd(bus);
  479. }
  480. if (SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, M_STOP_CMD)) {
  481. if (!(aspeed_i2c_get_state(bus) & I2CD_MACTIVE)) {
  482. qemu_log_mask(LOG_GUEST_ERROR, "%s: abnormal stop\n", __func__);
  483. SHARED_ARRAY_FIELD_DP32(bus->regs, reg_intr_sts, ABNORMAL, 1);
  484. if (aspeed_i2c_bus_pkt_mode_en(bus)) {
  485. ARRAY_FIELD_DP32(bus->regs, I2CM_INTR_STS, PKT_CMD_FAIL, 1);
  486. }
  487. } else {
  488. aspeed_i2c_set_state(bus, I2CD_MSTOP);
  489. i2c_end_transfer(bus->bus);
  490. SHARED_ARRAY_FIELD_DP32(bus->regs, reg_intr_sts, NORMAL_STOP, 1);
  491. }
  492. SHARED_ARRAY_FIELD_DP32(bus->regs, reg_cmd, M_STOP_CMD, 0);
  493. aspeed_i2c_set_state(bus, I2CD_IDLE);
  494. i2c_schedule_pending_master(bus->bus);
  495. }
  496. if (aspeed_i2c_bus_pkt_mode_en(bus)) {
  497. ARRAY_FIELD_DP32(bus->regs, I2CM_INTR_STS, PKT_CMD_DONE, 1);
  498. }
  499. }
  500. static void aspeed_i2c_bus_new_write(AspeedI2CBus *bus, hwaddr offset,
  501. uint64_t value, unsigned size)
  502. {
  503. AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(bus->controller);
  504. bool handle_rx;
  505. bool w1t;
  506. trace_aspeed_i2c_bus_write(bus->id, offset, size, value);
  507. switch (offset) {
  508. case A_I2CC_FUN_CTRL:
  509. bus->regs[R_I2CC_FUN_CTRL] = value;
  510. break;
  511. case A_I2CC_AC_TIMING:
  512. bus->regs[R_I2CC_AC_TIMING] = value & 0x1ffff0ff;
  513. break;
  514. case A_I2CC_MS_TXRX_BYTE_BUF:
  515. SHARED_ARRAY_FIELD_DP32(bus->regs, R_I2CC_MS_TXRX_BYTE_BUF, TX_BUF,
  516. value);
  517. break;
  518. case A_I2CC_POOL_CTRL:
  519. bus->regs[R_I2CC_POOL_CTRL] &= ~0xffffff;
  520. bus->regs[R_I2CC_POOL_CTRL] |= (value & 0xffffff);
  521. break;
  522. case A_I2CM_INTR_CTRL:
  523. bus->regs[R_I2CM_INTR_CTRL] = value & 0x0007f07f;
  524. break;
  525. case A_I2CM_INTR_STS:
  526. handle_rx = SHARED_ARRAY_FIELD_EX32(bus->regs, R_I2CM_INTR_STS, RX_DONE)
  527. && SHARED_FIELD_EX32(value, RX_DONE);
  528. /* In packet mode, clearing PKT_CMD_DONE clears other interrupts. */
  529. if (aspeed_i2c_bus_pkt_mode_en(bus) &&
  530. FIELD_EX32(value, I2CM_INTR_STS, PKT_CMD_DONE)) {
  531. bus->regs[R_I2CM_INTR_STS] &= 0xf0001000;
  532. if (!bus->regs[R_I2CM_INTR_STS]) {
  533. bus->controller->intr_status &= ~(1 << bus->id);
  534. qemu_irq_lower(aic->bus_get_irq(bus));
  535. }
  536. aspeed_i2c_bus_raise_slave_interrupt(bus);
  537. break;
  538. }
  539. bus->regs[R_I2CM_INTR_STS] &= ~(value & 0xf007f07f);
  540. if (!bus->regs[R_I2CM_INTR_STS]) {
  541. bus->controller->intr_status &= ~(1 << bus->id);
  542. qemu_irq_lower(aic->bus_get_irq(bus));
  543. }
  544. if (handle_rx && (SHARED_ARRAY_FIELD_EX32(bus->regs, R_I2CM_CMD,
  545. M_RX_CMD) ||
  546. SHARED_ARRAY_FIELD_EX32(bus->regs, R_I2CM_CMD,
  547. M_S_RX_CMD_LAST))) {
  548. aspeed_i2c_handle_rx_cmd(bus);
  549. aspeed_i2c_bus_raise_interrupt(bus);
  550. }
  551. break;
  552. case A_I2CM_CMD:
  553. if (!aspeed_i2c_bus_is_enabled(bus)) {
  554. break;
  555. }
  556. if (!aspeed_i2c_bus_is_master(bus)) {
  557. qemu_log_mask(LOG_GUEST_ERROR, "%s: Master mode is not enabled\n",
  558. __func__);
  559. break;
  560. }
  561. if (!aic->has_dma &&
  562. (SHARED_FIELD_EX32(value, RX_DMA_EN) ||
  563. SHARED_FIELD_EX32(value, TX_DMA_EN))) {
  564. qemu_log_mask(LOG_GUEST_ERROR, "%s: No DMA support\n", __func__);
  565. break;
  566. }
  567. if (bus->regs[R_I2CM_INTR_STS] & 0xffff0000) {
  568. qemu_log_mask(LOG_UNIMP, "%s: Packet mode is not implemented\n",
  569. __func__);
  570. break;
  571. }
  572. value &= 0xff0ffbfb;
  573. if (ARRAY_FIELD_EX32(bus->regs, I2CM_CMD, W1_CTRL)) {
  574. bus->regs[R_I2CM_CMD] |= value;
  575. } else {
  576. bus->regs[R_I2CM_CMD] = value;
  577. }
  578. aspeed_i2c_bus_handle_cmd(bus, value);
  579. aspeed_i2c_bus_raise_interrupt(bus);
  580. break;
  581. case A_I2CM_DMA_TX_ADDR:
  582. bus->regs[R_I2CM_DMA_TX_ADDR] = FIELD_EX32(value, I2CM_DMA_TX_ADDR,
  583. ADDR);
  584. bus->dma_dram_offset =
  585. deposit64(bus->dma_dram_offset, 0, 32,
  586. FIELD_EX32(value, I2CM_DMA_TX_ADDR, ADDR));
  587. bus->regs[R_I2CC_DMA_LEN] = ARRAY_FIELD_EX32(bus->regs, I2CM_DMA_LEN,
  588. TX_BUF_LEN) + 1;
  589. break;
  590. case A_I2CM_DMA_RX_ADDR:
  591. bus->regs[R_I2CM_DMA_RX_ADDR] = FIELD_EX32(value, I2CM_DMA_RX_ADDR,
  592. ADDR);
  593. bus->dma_dram_offset =
  594. deposit64(bus->dma_dram_offset, 0, 32,
  595. FIELD_EX32(value, I2CM_DMA_RX_ADDR, ADDR));
  596. bus->regs[R_I2CC_DMA_LEN] = ARRAY_FIELD_EX32(bus->regs, I2CM_DMA_LEN,
  597. RX_BUF_LEN) + 1;
  598. break;
  599. case A_I2CM_DMA_LEN:
  600. w1t = FIELD_EX32(value, I2CM_DMA_LEN, RX_BUF_LEN_W1T) ||
  601. FIELD_EX32(value, I2CM_DMA_LEN, TX_BUF_LEN_W1T);
  602. /* If none of the w1t bits are set, just write to the reg as normal. */
  603. if (!w1t) {
  604. bus->regs[R_I2CM_DMA_LEN] = value;
  605. break;
  606. }
  607. if (FIELD_EX32(value, I2CM_DMA_LEN, RX_BUF_LEN_W1T)) {
  608. ARRAY_FIELD_DP32(bus->regs, I2CM_DMA_LEN, RX_BUF_LEN,
  609. FIELD_EX32(value, I2CM_DMA_LEN, RX_BUF_LEN));
  610. }
  611. if (FIELD_EX32(value, I2CM_DMA_LEN, TX_BUF_LEN_W1T)) {
  612. ARRAY_FIELD_DP32(bus->regs, I2CM_DMA_LEN, TX_BUF_LEN,
  613. FIELD_EX32(value, I2CM_DMA_LEN, TX_BUF_LEN));
  614. }
  615. break;
  616. case A_I2CM_DMA_LEN_STS:
  617. /* Writes clear to 0 */
  618. bus->regs[R_I2CM_DMA_LEN_STS] = 0;
  619. break;
  620. case A_I2CC_DMA_ADDR:
  621. case A_I2CC_DMA_LEN:
  622. /* RO */
  623. break;
  624. case A_I2CS_DEV_ADDR:
  625. bus->regs[R_I2CS_DEV_ADDR] = value;
  626. break;
  627. case A_I2CS_DMA_RX_ADDR:
  628. bus->regs[R_I2CS_DMA_RX_ADDR] = value;
  629. break;
  630. case A_I2CS_DMA_LEN:
  631. assert(FIELD_EX32(value, I2CS_DMA_LEN, TX_BUF_LEN) == 0);
  632. if (FIELD_EX32(value, I2CS_DMA_LEN, RX_BUF_LEN_W1T)) {
  633. ARRAY_FIELD_DP32(bus->regs, I2CS_DMA_LEN, RX_BUF_LEN,
  634. FIELD_EX32(value, I2CS_DMA_LEN, RX_BUF_LEN));
  635. } else {
  636. bus->regs[R_I2CS_DMA_LEN] = value;
  637. }
  638. break;
  639. case A_I2CS_CMD:
  640. if (FIELD_EX32(value, I2CS_CMD, W1_CTRL)) {
  641. bus->regs[R_I2CS_CMD] |= value;
  642. } else {
  643. bus->regs[R_I2CS_CMD] = value;
  644. }
  645. i2c_slave_set_address(bus->slave, bus->regs[R_I2CS_DEV_ADDR]);
  646. break;
  647. case A_I2CS_INTR_CTRL:
  648. bus->regs[R_I2CS_INTR_CTRL] = value;
  649. break;
  650. case A_I2CS_INTR_STS:
  651. if (ARRAY_FIELD_EX32(bus->regs, I2CS_INTR_CTRL, PKT_CMD_DONE)) {
  652. if (ARRAY_FIELD_EX32(bus->regs, I2CS_INTR_STS, PKT_CMD_DONE) &&
  653. FIELD_EX32(value, I2CS_INTR_STS, PKT_CMD_DONE)) {
  654. bus->regs[R_I2CS_INTR_STS] &= 0xfffc0000;
  655. }
  656. } else {
  657. bus->regs[R_I2CS_INTR_STS] &= ~value;
  658. }
  659. if (!bus->regs[R_I2CS_INTR_STS]) {
  660. bus->controller->intr_status &= ~(1 << bus->id);
  661. qemu_irq_lower(aic->bus_get_irq(bus));
  662. }
  663. aspeed_i2c_bus_raise_interrupt(bus);
  664. break;
  665. case A_I2CS_DMA_LEN_STS:
  666. bus->regs[R_I2CS_DMA_LEN_STS] = 0;
  667. break;
  668. case A_I2CS_DMA_TX_ADDR:
  669. qemu_log_mask(LOG_UNIMP, "%s: Slave mode DMA TX is not implemented\n",
  670. __func__);
  671. break;
  672. /*
  673. * The AST2700 support the maximum DRAM size is 8 GB.
  674. * The DRAM offset range is from 0x0_0000_0000 to
  675. * 0x1_FFFF_FFFF and it is enough to use bits [33:0]
  676. * saving the dram offset.
  677. * Therefore, save the high part physical address bit[1:0]
  678. * of Tx/Rx buffer address as dma_dram_offset bit[33:32].
  679. */
  680. case A_I2CM_DMA_TX_ADDR_HI:
  681. if (!aic->has_dma64) {
  682. qemu_log_mask(LOG_GUEST_ERROR, "%s: No DMA 64 bits support\n",
  683. __func__);
  684. break;
  685. }
  686. bus->regs[R_I2CM_DMA_TX_ADDR_HI] = FIELD_EX32(value,
  687. I2CM_DMA_TX_ADDR_HI,
  688. ADDR_HI);
  689. bus->dma_dram_offset = deposit64(bus->dma_dram_offset, 32, 32,
  690. extract32(value, 0, 2));
  691. break;
  692. case A_I2CM_DMA_RX_ADDR_HI:
  693. if (!aic->has_dma64) {
  694. qemu_log_mask(LOG_GUEST_ERROR, "%s: No DMA 64 bits support\n",
  695. __func__);
  696. break;
  697. }
  698. bus->regs[R_I2CM_DMA_RX_ADDR_HI] = FIELD_EX32(value,
  699. I2CM_DMA_RX_ADDR_HI,
  700. ADDR_HI);
  701. bus->dma_dram_offset = deposit64(bus->dma_dram_offset, 32, 32,
  702. extract32(value, 0, 2));
  703. break;
  704. case A_I2CS_DMA_TX_ADDR_HI:
  705. qemu_log_mask(LOG_UNIMP,
  706. "%s: Slave mode DMA TX Addr high is not implemented\n",
  707. __func__);
  708. break;
  709. case A_I2CS_DMA_RX_ADDR_HI:
  710. if (!aic->has_dma64) {
  711. qemu_log_mask(LOG_GUEST_ERROR, "%s: No DMA 64 bits support\n",
  712. __func__);
  713. break;
  714. }
  715. bus->regs[R_I2CS_DMA_RX_ADDR_HI] = FIELD_EX32(value,
  716. I2CS_DMA_RX_ADDR_HI,
  717. ADDR_HI);
  718. bus->dma_dram_offset = deposit64(bus->dma_dram_offset, 32, 32,
  719. extract32(value, 0, 2));
  720. break;
  721. default:
  722. qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n",
  723. __func__, offset);
  724. }
  725. }
  726. static void aspeed_i2c_bus_old_write(AspeedI2CBus *bus, hwaddr offset,
  727. uint64_t value, unsigned size)
  728. {
  729. AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(bus->controller);
  730. bool handle_rx;
  731. trace_aspeed_i2c_bus_write(bus->id, offset, size, value);
  732. switch (offset) {
  733. case A_I2CD_FUN_CTRL:
  734. if (SHARED_FIELD_EX32(value, SLAVE_EN)) {
  735. i2c_slave_set_address(bus->slave, bus->regs[R_I2CD_DEV_ADDR]);
  736. }
  737. bus->regs[R_I2CD_FUN_CTRL] = value & 0x0071C3FF;
  738. break;
  739. case A_I2CD_AC_TIMING1:
  740. bus->regs[R_I2CD_AC_TIMING1] = value & 0xFFFFF0F;
  741. break;
  742. case A_I2CD_AC_TIMING2:
  743. bus->regs[R_I2CD_AC_TIMING2] = value & 0x7;
  744. break;
  745. case A_I2CD_INTR_CTRL:
  746. bus->regs[R_I2CD_INTR_CTRL] = value & 0x7FFF;
  747. break;
  748. case A_I2CD_INTR_STS:
  749. handle_rx = SHARED_ARRAY_FIELD_EX32(bus->regs, R_I2CD_INTR_STS, RX_DONE)
  750. && SHARED_FIELD_EX32(value, RX_DONE);
  751. bus->regs[R_I2CD_INTR_STS] &= ~(value & 0x7FFF);
  752. if (!bus->regs[R_I2CD_INTR_STS]) {
  753. bus->controller->intr_status &= ~(1 << bus->id);
  754. qemu_irq_lower(aic->bus_get_irq(bus));
  755. }
  756. if (handle_rx) {
  757. if (SHARED_ARRAY_FIELD_EX32(bus->regs, R_I2CD_CMD, M_RX_CMD) ||
  758. SHARED_ARRAY_FIELD_EX32(bus->regs, R_I2CD_CMD,
  759. M_S_RX_CMD_LAST)) {
  760. aspeed_i2c_handle_rx_cmd(bus);
  761. aspeed_i2c_bus_raise_interrupt(bus);
  762. } else if (aspeed_i2c_get_state(bus) == I2CD_STXD) {
  763. i2c_ack(bus->bus);
  764. }
  765. }
  766. break;
  767. case A_I2CD_DEV_ADDR:
  768. bus->regs[R_I2CD_DEV_ADDR] = value;
  769. break;
  770. case A_I2CD_POOL_CTRL:
  771. bus->regs[R_I2CD_POOL_CTRL] &= ~0xffffff;
  772. bus->regs[R_I2CD_POOL_CTRL] |= (value & 0xffffff);
  773. break;
  774. case A_I2CD_BYTE_BUF:
  775. SHARED_ARRAY_FIELD_DP32(bus->regs, R_I2CD_BYTE_BUF, TX_BUF, value);
  776. break;
  777. case A_I2CD_CMD:
  778. if (!aspeed_i2c_bus_is_enabled(bus)) {
  779. break;
  780. }
  781. if (!aspeed_i2c_bus_is_master(bus)) {
  782. qemu_log_mask(LOG_GUEST_ERROR, "%s: Master mode is not enabled\n",
  783. __func__);
  784. break;
  785. }
  786. if (!aic->has_dma &&
  787. (SHARED_FIELD_EX32(value, RX_DMA_EN) ||
  788. SHARED_FIELD_EX32(value, TX_DMA_EN))) {
  789. qemu_log_mask(LOG_GUEST_ERROR, "%s: No DMA support\n", __func__);
  790. break;
  791. }
  792. bus->regs[R_I2CD_CMD] &= ~0xFFFF;
  793. bus->regs[R_I2CD_CMD] |= value & 0xFFFF;
  794. aspeed_i2c_bus_handle_cmd(bus, value);
  795. aspeed_i2c_bus_raise_interrupt(bus);
  796. break;
  797. case A_I2CD_DMA_ADDR:
  798. if (!aic->has_dma) {
  799. qemu_log_mask(LOG_GUEST_ERROR, "%s: No DMA support\n", __func__);
  800. break;
  801. }
  802. bus->dma_dram_offset = deposit64(bus->dma_dram_offset, 0, 32,
  803. value & 0x3ffffffc);
  804. break;
  805. case A_I2CD_DMA_LEN:
  806. if (!aic->has_dma) {
  807. qemu_log_mask(LOG_GUEST_ERROR, "%s: No DMA support\n", __func__);
  808. break;
  809. }
  810. bus->regs[R_I2CD_DMA_LEN] = value & 0xfff;
  811. if (!bus->regs[R_I2CD_DMA_LEN]) {
  812. qemu_log_mask(LOG_UNIMP, "%s: invalid DMA length\n", __func__);
  813. }
  814. break;
  815. default:
  816. qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n",
  817. __func__, offset);
  818. }
  819. }
  820. static void aspeed_i2c_bus_write(void *opaque, hwaddr offset,
  821. uint64_t value, unsigned size)
  822. {
  823. AspeedI2CBus *bus = opaque;
  824. if (aspeed_i2c_is_new_mode(bus->controller)) {
  825. aspeed_i2c_bus_new_write(bus, offset, value, size);
  826. } else {
  827. aspeed_i2c_bus_old_write(bus, offset, value, size);
  828. }
  829. }
  830. static uint64_t aspeed_i2c_ctrl_read(void *opaque, hwaddr offset,
  831. unsigned size)
  832. {
  833. AspeedI2CState *s = opaque;
  834. switch (offset) {
  835. case A_I2C_CTRL_STATUS:
  836. return s->intr_status;
  837. case A_I2C_CTRL_GLOBAL:
  838. return s->ctrl_global;
  839. case A_I2C_CTRL_NEW_CLK_DIVIDER:
  840. if (aspeed_i2c_is_new_mode(s)) {
  841. return s->new_clk_divider;
  842. }
  843. qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n",
  844. __func__, offset);
  845. break;
  846. default:
  847. qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n",
  848. __func__, offset);
  849. break;
  850. }
  851. return -1;
  852. }
  853. static void aspeed_i2c_ctrl_write(void *opaque, hwaddr offset,
  854. uint64_t value, unsigned size)
  855. {
  856. AspeedI2CState *s = opaque;
  857. switch (offset) {
  858. case A_I2C_CTRL_GLOBAL:
  859. s->ctrl_global = value;
  860. break;
  861. case A_I2C_CTRL_NEW_CLK_DIVIDER:
  862. if (aspeed_i2c_is_new_mode(s)) {
  863. s->new_clk_divider = value;
  864. } else {
  865. qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx
  866. "\n", __func__, offset);
  867. }
  868. break;
  869. case A_I2C_CTRL_STATUS:
  870. default:
  871. qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n",
  872. __func__, offset);
  873. break;
  874. }
  875. }
  876. static const MemoryRegionOps aspeed_i2c_bus_ops = {
  877. .read = aspeed_i2c_bus_read,
  878. .write = aspeed_i2c_bus_write,
  879. .endianness = DEVICE_LITTLE_ENDIAN,
  880. };
  881. static const MemoryRegionOps aspeed_i2c_ctrl_ops = {
  882. .read = aspeed_i2c_ctrl_read,
  883. .write = aspeed_i2c_ctrl_write,
  884. .endianness = DEVICE_LITTLE_ENDIAN,
  885. };
  886. static uint64_t aspeed_i2c_share_pool_read(void *opaque, hwaddr offset,
  887. unsigned size)
  888. {
  889. AspeedI2CState *s = opaque;
  890. uint64_t ret = 0;
  891. int i;
  892. for (i = 0; i < size; i++) {
  893. ret |= (uint64_t) s->share_pool[offset + i] << (8 * i);
  894. }
  895. return ret;
  896. }
  897. static void aspeed_i2c_share_pool_write(void *opaque, hwaddr offset,
  898. uint64_t value, unsigned size)
  899. {
  900. AspeedI2CState *s = opaque;
  901. int i;
  902. for (i = 0; i < size; i++) {
  903. s->share_pool[offset + i] = (value >> (8 * i)) & 0xFF;
  904. }
  905. }
  906. static const MemoryRegionOps aspeed_i2c_share_pool_ops = {
  907. .read = aspeed_i2c_share_pool_read,
  908. .write = aspeed_i2c_share_pool_write,
  909. .endianness = DEVICE_LITTLE_ENDIAN,
  910. .valid = {
  911. .min_access_size = 1,
  912. .max_access_size = 4,
  913. },
  914. };
  915. static uint64_t aspeed_i2c_bus_pool_read(void *opaque, hwaddr offset,
  916. unsigned size)
  917. {
  918. AspeedI2CBus *s = opaque;
  919. uint64_t ret = 0;
  920. int i;
  921. for (i = 0; i < size; i++) {
  922. ret |= (uint64_t) s->pool[offset + i] << (8 * i);
  923. }
  924. return ret;
  925. }
  926. static void aspeed_i2c_bus_pool_write(void *opaque, hwaddr offset,
  927. uint64_t value, unsigned size)
  928. {
  929. AspeedI2CBus *s = opaque;
  930. int i;
  931. for (i = 0; i < size; i++) {
  932. s->pool[offset + i] = (value >> (8 * i)) & 0xFF;
  933. }
  934. }
  935. static const MemoryRegionOps aspeed_i2c_bus_pool_ops = {
  936. .read = aspeed_i2c_bus_pool_read,
  937. .write = aspeed_i2c_bus_pool_write,
  938. .endianness = DEVICE_LITTLE_ENDIAN,
  939. .valid = {
  940. .min_access_size = 1,
  941. .max_access_size = 4,
  942. },
  943. };
  944. static const VMStateDescription aspeed_i2c_bus_vmstate = {
  945. .name = TYPE_ASPEED_I2C,
  946. .version_id = 6,
  947. .minimum_version_id = 6,
  948. .fields = (const VMStateField[]) {
  949. VMSTATE_UINT32_ARRAY(regs, AspeedI2CBus, ASPEED_I2C_NEW_NUM_REG),
  950. VMSTATE_UINT8_ARRAY(pool, AspeedI2CBus, ASPEED_I2C_BUS_POOL_SIZE),
  951. VMSTATE_UINT64(dma_dram_offset, AspeedI2CBus),
  952. VMSTATE_END_OF_LIST()
  953. }
  954. };
  955. static const VMStateDescription aspeed_i2c_vmstate = {
  956. .name = TYPE_ASPEED_I2C,
  957. .version_id = 3,
  958. .minimum_version_id = 3,
  959. .fields = (const VMStateField[]) {
  960. VMSTATE_UINT32(intr_status, AspeedI2CState),
  961. VMSTATE_STRUCT_ARRAY(busses, AspeedI2CState,
  962. ASPEED_I2C_NR_BUSSES, 1, aspeed_i2c_bus_vmstate,
  963. AspeedI2CBus),
  964. VMSTATE_UINT8_ARRAY(share_pool, AspeedI2CState,
  965. ASPEED_I2C_SHARE_POOL_SIZE),
  966. VMSTATE_END_OF_LIST()
  967. }
  968. };
  969. static void aspeed_i2c_reset(DeviceState *dev)
  970. {
  971. AspeedI2CState *s = ASPEED_I2C(dev);
  972. s->intr_status = 0;
  973. }
  974. static void aspeed_i2c_instance_init(Object *obj)
  975. {
  976. AspeedI2CState *s = ASPEED_I2C(obj);
  977. AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(s);
  978. int i;
  979. for (i = 0; i < aic->num_busses; i++) {
  980. object_initialize_child(obj, "bus[*]", &s->busses[i],
  981. TYPE_ASPEED_I2C_BUS);
  982. }
  983. }
  984. /*
  985. * Address Definitions (AST2400 and AST2500)
  986. *
  987. * 0x000 ... 0x03F: Global Register
  988. * 0x040 ... 0x07F: Device 1
  989. * 0x080 ... 0x0BF: Device 2
  990. * 0x0C0 ... 0x0FF: Device 3
  991. * 0x100 ... 0x13F: Device 4
  992. * 0x140 ... 0x17F: Device 5
  993. * 0x180 ... 0x1BF: Device 6
  994. * 0x1C0 ... 0x1FF: Device 7
  995. * 0x200 ... 0x20F: Device 1 buffer (AST2500 unused in linux driver)
  996. * 0x210 ... 0x21F: Device 2 buffer
  997. * 0x220 ... 0x22F: Device 3 buffer
  998. * 0x230 ... 0x23F: Device 4 buffer
  999. * 0x240 ... 0x24F: Device 5 buffer
  1000. * 0x250 ... 0x25F: Device 6 buffer
  1001. * 0x260 ... 0x26F: Device 7 buffer
  1002. * 0x270 ... 0x27F: Device 8 buffer
  1003. * 0x280 ... 0x28F: Device 9 buffer
  1004. * 0x290 ... 0x29F: Device 10 buffer
  1005. * 0x2A0 ... 0x2AF: Device 11 buffer
  1006. * 0x2B0 ... 0x2BF: Device 12 buffer
  1007. * 0x2C0 ... 0x2CF: Device 13 buffer
  1008. * 0x2D0 ... 0x2DF: Device 14 buffer
  1009. * 0x2E0 ... 0x2FF: Reserved
  1010. * 0x300 ... 0x33F: Device 8
  1011. * 0x340 ... 0x37F: Device 9
  1012. * 0x380 ... 0x3BF: Device 10
  1013. * 0x3C0 ... 0x3FF: Device 11
  1014. * 0x400 ... 0x43F: Device 12
  1015. * 0x440 ... 0x47F: Device 13
  1016. * 0x480 ... 0x4BF: Device 14
  1017. * 0x800 ... 0xFFF: Buffer Pool (AST2400 unused in linux driver)
  1018. *
  1019. * Address Definitions (AST2600 and AST1030)
  1020. * 0x000 ... 0x07F: Global Register
  1021. * 0x080 ... 0x0FF: Device 1
  1022. * 0x100 ... 0x17F: Device 2
  1023. * 0x180 ... 0x1FF: Device 3
  1024. * 0x200 ... 0x27F: Device 4
  1025. * 0x280 ... 0x2FF: Device 5
  1026. * 0x300 ... 0x37F: Device 6
  1027. * 0x380 ... 0x3FF: Device 7
  1028. * 0x400 ... 0x47F: Device 8
  1029. * 0x480 ... 0x4FF: Device 9
  1030. * 0x500 ... 0x57F: Device 10
  1031. * 0x580 ... 0x5FF: Device 11
  1032. * 0x600 ... 0x67F: Device 12
  1033. * 0x680 ... 0x6FF: Device 13
  1034. * 0x700 ... 0x77F: Device 14
  1035. * 0x780 ... 0x7FF: Device 15 (15 and 16 unused in AST1030)
  1036. * 0x800 ... 0x87F: Device 16
  1037. * 0xC00 ... 0xC1F: Device 1 buffer
  1038. * 0xC20 ... 0xC3F: Device 2 buffer
  1039. * 0xC40 ... 0xC5F: Device 3 buffer
  1040. * 0xC60 ... 0xC7F: Device 4 buffer
  1041. * 0xC80 ... 0xC9F: Device 5 buffer
  1042. * 0xCA0 ... 0xCBF: Device 6 buffer
  1043. * 0xCC0 ... 0xCDF: Device 7 buffer
  1044. * 0xCE0 ... 0xCFF: Device 8 buffer
  1045. * 0xD00 ... 0xD1F: Device 9 buffer
  1046. * 0xD20 ... 0xD3F: Device 10 buffer
  1047. * 0xD40 ... 0xD5F: Device 11 buffer
  1048. * 0xD60 ... 0xD7F: Device 12 buffer
  1049. * 0xD80 ... 0xD9F: Device 13 buffer
  1050. * 0xDA0 ... 0xDBF: Device 14 buffer
  1051. * 0xDC0 ... 0xDDF: Device 15 buffer (15 and 16 unused in AST1030)
  1052. * 0xDE0 ... 0xDFF: Device 16 buffer
  1053. *
  1054. * Address Definitions (AST2700)
  1055. * 0x000 ... 0x0FF: Global Register
  1056. * 0x100 ... 0x17F: Device 0
  1057. * 0x1A0 ... 0x1BF: Device 0 buffer
  1058. * 0x200 ... 0x27F: Device 1
  1059. * 0x2A0 ... 0x2BF: Device 1 buffer
  1060. * 0x300 ... 0x37F: Device 2
  1061. * 0x3A0 ... 0x3BF: Device 2 buffer
  1062. * 0x400 ... 0x47F: Device 3
  1063. * 0x4A0 ... 0x4BF: Device 3 buffer
  1064. * 0x500 ... 0x57F: Device 4
  1065. * 0x5A0 ... 0x5BF: Device 4 buffer
  1066. * 0x600 ... 0x67F: Device 5
  1067. * 0x6A0 ... 0x6BF: Device 5 buffer
  1068. * 0x700 ... 0x77F: Device 6
  1069. * 0x7A0 ... 0x7BF: Device 6 buffer
  1070. * 0x800 ... 0x87F: Device 7
  1071. * 0x8A0 ... 0x8BF: Device 7 buffer
  1072. * 0x900 ... 0x97F: Device 8
  1073. * 0x9A0 ... 0x9BF: Device 8 buffer
  1074. * 0xA00 ... 0xA7F: Device 9
  1075. * 0xAA0 ... 0xABF: Device 9 buffer
  1076. * 0xB00 ... 0xB7F: Device 10
  1077. * 0xBA0 ... 0xBBF: Device 10 buffer
  1078. * 0xC00 ... 0xC7F: Device 11
  1079. * 0xCA0 ... 0xCBF: Device 11 buffer
  1080. * 0xD00 ... 0xD7F: Device 12
  1081. * 0xDA0 ... 0xDBF: Device 12 buffer
  1082. * 0xE00 ... 0xE7F: Device 13
  1083. * 0xEA0 ... 0xEBF: Device 13 buffer
  1084. * 0xF00 ... 0xF7F: Device 14
  1085. * 0xFA0 ... 0xFBF: Device 14 buffer
  1086. * 0x1000 ... 0x107F: Device 15
  1087. * 0x10A0 ... 0x10BF: Device 15 buffer
  1088. */
  1089. static void aspeed_i2c_realize(DeviceState *dev, Error **errp)
  1090. {
  1091. int i;
  1092. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  1093. AspeedI2CState *s = ASPEED_I2C(dev);
  1094. AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(s);
  1095. uint32_t reg_offset = aic->reg_size + aic->reg_gap_size;
  1096. uint32_t pool_offset = aic->pool_size + aic->pool_gap_size;
  1097. sysbus_init_irq(sbd, &s->irq);
  1098. memory_region_init_io(&s->iomem, OBJECT(s), &aspeed_i2c_ctrl_ops, s,
  1099. "aspeed.i2c", aic->mem_size);
  1100. sysbus_init_mmio(sbd, &s->iomem);
  1101. for (i = 0; i < aic->num_busses; i++) {
  1102. Object *bus = OBJECT(&s->busses[i]);
  1103. int offset = i < aic->gap ? 1 : 5;
  1104. if (!object_property_set_link(bus, "controller", OBJECT(s), errp)) {
  1105. return;
  1106. }
  1107. if (!object_property_set_uint(bus, "bus-id", i, errp)) {
  1108. return;
  1109. }
  1110. if (!sysbus_realize(SYS_BUS_DEVICE(bus), errp)) {
  1111. return;
  1112. }
  1113. memory_region_add_subregion(&s->iomem, reg_offset * (i + offset),
  1114. &s->busses[i].mr);
  1115. }
  1116. if (aic->has_share_pool) {
  1117. memory_region_init_io(&s->pool_iomem, OBJECT(s),
  1118. &aspeed_i2c_share_pool_ops, s,
  1119. "aspeed.i2c-share-pool", aic->pool_size);
  1120. memory_region_add_subregion(&s->iomem, aic->pool_base,
  1121. &s->pool_iomem);
  1122. } else {
  1123. for (i = 0; i < aic->num_busses; i++) {
  1124. memory_region_add_subregion(&s->iomem,
  1125. aic->pool_base + (pool_offset * i),
  1126. &s->busses[i].mr_pool);
  1127. }
  1128. }
  1129. if (aic->has_dma) {
  1130. if (!s->dram_mr) {
  1131. error_setg(errp, TYPE_ASPEED_I2C ": 'dram' link not set");
  1132. return;
  1133. }
  1134. address_space_init(&s->dram_as, s->dram_mr,
  1135. TYPE_ASPEED_I2C "-dma-dram");
  1136. }
  1137. }
  1138. static const Property aspeed_i2c_properties[] = {
  1139. DEFINE_PROP_LINK("dram", AspeedI2CState, dram_mr,
  1140. TYPE_MEMORY_REGION, MemoryRegion *),
  1141. };
  1142. static void aspeed_i2c_class_init(ObjectClass *klass, void *data)
  1143. {
  1144. DeviceClass *dc = DEVICE_CLASS(klass);
  1145. dc->vmsd = &aspeed_i2c_vmstate;
  1146. device_class_set_legacy_reset(dc, aspeed_i2c_reset);
  1147. device_class_set_props(dc, aspeed_i2c_properties);
  1148. dc->realize = aspeed_i2c_realize;
  1149. dc->desc = "Aspeed I2C Controller";
  1150. }
  1151. static const TypeInfo aspeed_i2c_info = {
  1152. .name = TYPE_ASPEED_I2C,
  1153. .parent = TYPE_SYS_BUS_DEVICE,
  1154. .instance_init = aspeed_i2c_instance_init,
  1155. .instance_size = sizeof(AspeedI2CState),
  1156. .class_init = aspeed_i2c_class_init,
  1157. .class_size = sizeof(AspeedI2CClass),
  1158. .abstract = true,
  1159. };
  1160. static int aspeed_i2c_bus_new_slave_event(AspeedI2CBus *bus,
  1161. enum i2c_event event)
  1162. {
  1163. switch (event) {
  1164. case I2C_START_SEND_ASYNC:
  1165. if (!SHARED_ARRAY_FIELD_EX32(bus->regs, R_I2CS_CMD, RX_DMA_EN)) {
  1166. qemu_log_mask(LOG_GUEST_ERROR,
  1167. "%s: Slave mode RX DMA is not enabled\n", __func__);
  1168. return -1;
  1169. }
  1170. ARRAY_FIELD_DP32(bus->regs, I2CS_DMA_LEN_STS, RX_LEN, 0);
  1171. bus->dma_dram_offset =
  1172. deposit64(bus->dma_dram_offset, 0, 32,
  1173. ARRAY_FIELD_EX32(bus->regs, I2CS_DMA_RX_ADDR, ADDR));
  1174. bus->regs[R_I2CC_DMA_LEN] =
  1175. ARRAY_FIELD_EX32(bus->regs, I2CS_DMA_LEN, RX_BUF_LEN) + 1;
  1176. i2c_ack(bus->bus);
  1177. break;
  1178. case I2C_FINISH:
  1179. ARRAY_FIELD_DP32(bus->regs, I2CS_INTR_STS, PKT_CMD_DONE, 1);
  1180. ARRAY_FIELD_DP32(bus->regs, I2CS_INTR_STS, SLAVE_ADDR_RX_MATCH, 1);
  1181. SHARED_ARRAY_FIELD_DP32(bus->regs, R_I2CS_INTR_STS, NORMAL_STOP, 1);
  1182. SHARED_ARRAY_FIELD_DP32(bus->regs, R_I2CS_INTR_STS, RX_DONE, 1);
  1183. aspeed_i2c_bus_raise_slave_interrupt(bus);
  1184. break;
  1185. default:
  1186. qemu_log_mask(LOG_UNIMP, "%s: i2c event %d unimplemented\n",
  1187. __func__, event);
  1188. return -1;
  1189. }
  1190. return 0;
  1191. }
  1192. static int aspeed_i2c_bus_slave_event(I2CSlave *slave, enum i2c_event event)
  1193. {
  1194. BusState *qbus = qdev_get_parent_bus(DEVICE(slave));
  1195. AspeedI2CBus *bus = ASPEED_I2C_BUS(qbus->parent);
  1196. uint32_t reg_intr_sts = aspeed_i2c_bus_intr_sts_offset(bus);
  1197. uint32_t reg_byte_buf = aspeed_i2c_bus_byte_buf_offset(bus);
  1198. uint32_t reg_dev_addr = aspeed_i2c_bus_dev_addr_offset(bus);
  1199. uint32_t dev_addr = SHARED_ARRAY_FIELD_EX32(bus->regs, reg_dev_addr,
  1200. SLAVE_DEV_ADDR1);
  1201. if (aspeed_i2c_is_new_mode(bus->controller)) {
  1202. return aspeed_i2c_bus_new_slave_event(bus, event);
  1203. }
  1204. switch (event) {
  1205. case I2C_START_SEND_ASYNC:
  1206. /* Bit[0] == 0 indicates "send". */
  1207. SHARED_ARRAY_FIELD_DP32(bus->regs, reg_byte_buf, RX_BUF, dev_addr << 1);
  1208. ARRAY_FIELD_DP32(bus->regs, I2CD_INTR_STS, SLAVE_ADDR_RX_MATCH, 1);
  1209. SHARED_ARRAY_FIELD_DP32(bus->regs, reg_intr_sts, RX_DONE, 1);
  1210. aspeed_i2c_set_state(bus, I2CD_STXD);
  1211. break;
  1212. case I2C_FINISH:
  1213. SHARED_ARRAY_FIELD_DP32(bus->regs, reg_intr_sts, NORMAL_STOP, 1);
  1214. aspeed_i2c_set_state(bus, I2CD_IDLE);
  1215. break;
  1216. default:
  1217. return -1;
  1218. }
  1219. aspeed_i2c_bus_raise_interrupt(bus);
  1220. return 0;
  1221. }
  1222. static void aspeed_i2c_bus_new_slave_send_async(AspeedI2CBus *bus, uint8_t data)
  1223. {
  1224. assert(address_space_write(&bus->controller->dram_as,
  1225. bus->dma_dram_offset,
  1226. MEMTXATTRS_UNSPECIFIED, &data, 1) == MEMTX_OK);
  1227. bus->dma_dram_offset++;
  1228. bus->regs[R_I2CC_DMA_LEN]--;
  1229. ARRAY_FIELD_DP32(bus->regs, I2CS_DMA_LEN_STS, RX_LEN,
  1230. ARRAY_FIELD_EX32(bus->regs, I2CS_DMA_LEN_STS, RX_LEN) + 1);
  1231. i2c_ack(bus->bus);
  1232. }
  1233. static void aspeed_i2c_bus_slave_send_async(I2CSlave *slave, uint8_t data)
  1234. {
  1235. BusState *qbus = qdev_get_parent_bus(DEVICE(slave));
  1236. AspeedI2CBus *bus = ASPEED_I2C_BUS(qbus->parent);
  1237. uint32_t reg_intr_sts = aspeed_i2c_bus_intr_sts_offset(bus);
  1238. uint32_t reg_byte_buf = aspeed_i2c_bus_byte_buf_offset(bus);
  1239. if (aspeed_i2c_is_new_mode(bus->controller)) {
  1240. return aspeed_i2c_bus_new_slave_send_async(bus, data);
  1241. }
  1242. SHARED_ARRAY_FIELD_DP32(bus->regs, reg_byte_buf, RX_BUF, data);
  1243. SHARED_ARRAY_FIELD_DP32(bus->regs, reg_intr_sts, RX_DONE, 1);
  1244. aspeed_i2c_bus_raise_interrupt(bus);
  1245. }
  1246. static void aspeed_i2c_bus_slave_class_init(ObjectClass *klass, void *data)
  1247. {
  1248. DeviceClass *dc = DEVICE_CLASS(klass);
  1249. I2CSlaveClass *sc = I2C_SLAVE_CLASS(klass);
  1250. dc->desc = "Aspeed I2C Bus Slave";
  1251. sc->event = aspeed_i2c_bus_slave_event;
  1252. sc->send_async = aspeed_i2c_bus_slave_send_async;
  1253. }
  1254. static const TypeInfo aspeed_i2c_bus_slave_info = {
  1255. .name = TYPE_ASPEED_I2C_BUS_SLAVE,
  1256. .parent = TYPE_I2C_SLAVE,
  1257. .instance_size = sizeof(AspeedI2CBusSlave),
  1258. .class_init = aspeed_i2c_bus_slave_class_init,
  1259. };
  1260. static void aspeed_i2c_bus_reset(DeviceState *dev)
  1261. {
  1262. AspeedI2CBus *s = ASPEED_I2C_BUS(dev);
  1263. memset(s->regs, 0, sizeof(s->regs));
  1264. i2c_end_transfer(s->bus);
  1265. }
  1266. static void aspeed_i2c_bus_realize(DeviceState *dev, Error **errp)
  1267. {
  1268. AspeedI2CBus *s = ASPEED_I2C_BUS(dev);
  1269. AspeedI2CClass *aic;
  1270. g_autofree char *name = g_strdup_printf(TYPE_ASPEED_I2C_BUS ".%d", s->id);
  1271. g_autofree char *pool_name = g_strdup_printf("%s.pool", name);
  1272. if (!s->controller) {
  1273. error_setg(errp, TYPE_ASPEED_I2C_BUS ": 'controller' link not set");
  1274. return;
  1275. }
  1276. aic = ASPEED_I2C_GET_CLASS(s->controller);
  1277. sysbus_init_irq(SYS_BUS_DEVICE(dev), &s->irq);
  1278. s->bus = i2c_init_bus(dev, name);
  1279. s->slave = i2c_slave_create_simple(s->bus, TYPE_ASPEED_I2C_BUS_SLAVE,
  1280. 0xff);
  1281. memory_region_init_io(&s->mr, OBJECT(s), &aspeed_i2c_bus_ops,
  1282. s, name, aic->reg_size);
  1283. sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->mr);
  1284. memory_region_init_io(&s->mr_pool, OBJECT(s), &aspeed_i2c_bus_pool_ops,
  1285. s, pool_name, aic->pool_size);
  1286. sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->mr_pool);
  1287. }
  1288. static const Property aspeed_i2c_bus_properties[] = {
  1289. DEFINE_PROP_UINT8("bus-id", AspeedI2CBus, id, 0),
  1290. DEFINE_PROP_LINK("controller", AspeedI2CBus, controller, TYPE_ASPEED_I2C,
  1291. AspeedI2CState *),
  1292. };
  1293. static void aspeed_i2c_bus_class_init(ObjectClass *klass, void *data)
  1294. {
  1295. DeviceClass *dc = DEVICE_CLASS(klass);
  1296. dc->desc = "Aspeed I2C Bus";
  1297. dc->realize = aspeed_i2c_bus_realize;
  1298. device_class_set_legacy_reset(dc, aspeed_i2c_bus_reset);
  1299. device_class_set_props(dc, aspeed_i2c_bus_properties);
  1300. }
  1301. static const TypeInfo aspeed_i2c_bus_info = {
  1302. .name = TYPE_ASPEED_I2C_BUS,
  1303. .parent = TYPE_SYS_BUS_DEVICE,
  1304. .instance_size = sizeof(AspeedI2CBus),
  1305. .class_init = aspeed_i2c_bus_class_init,
  1306. };
  1307. static qemu_irq aspeed_2400_i2c_bus_get_irq(AspeedI2CBus *bus)
  1308. {
  1309. return bus->controller->irq;
  1310. }
  1311. static uint8_t *aspeed_2400_i2c_bus_pool_base(AspeedI2CBus *bus)
  1312. {
  1313. uint8_t *pool_page =
  1314. &bus->controller->share_pool[ARRAY_FIELD_EX32(bus->regs,
  1315. I2CD_FUN_CTRL,
  1316. POOL_PAGE_SEL) * 0x100];
  1317. return &pool_page[ARRAY_FIELD_EX32(bus->regs, I2CD_POOL_CTRL, OFFSET)];
  1318. }
  1319. static void aspeed_2400_i2c_class_init(ObjectClass *klass, void *data)
  1320. {
  1321. DeviceClass *dc = DEVICE_CLASS(klass);
  1322. AspeedI2CClass *aic = ASPEED_I2C_CLASS(klass);
  1323. dc->desc = "ASPEED 2400 I2C Controller";
  1324. aic->num_busses = 14;
  1325. aic->reg_size = 0x40;
  1326. aic->gap = 7;
  1327. aic->bus_get_irq = aspeed_2400_i2c_bus_get_irq;
  1328. aic->has_share_pool = true;
  1329. aic->pool_size = 0x800;
  1330. aic->pool_base = 0x800;
  1331. aic->bus_pool_base = aspeed_2400_i2c_bus_pool_base;
  1332. aic->mem_size = 0x1000;
  1333. }
  1334. static const TypeInfo aspeed_2400_i2c_info = {
  1335. .name = TYPE_ASPEED_2400_I2C,
  1336. .parent = TYPE_ASPEED_I2C,
  1337. .class_init = aspeed_2400_i2c_class_init,
  1338. };
  1339. static qemu_irq aspeed_2500_i2c_bus_get_irq(AspeedI2CBus *bus)
  1340. {
  1341. return bus->controller->irq;
  1342. }
  1343. static uint8_t *aspeed_2500_i2c_bus_pool_base(AspeedI2CBus *bus)
  1344. {
  1345. return bus->pool;
  1346. }
  1347. static void aspeed_2500_i2c_class_init(ObjectClass *klass, void *data)
  1348. {
  1349. DeviceClass *dc = DEVICE_CLASS(klass);
  1350. AspeedI2CClass *aic = ASPEED_I2C_CLASS(klass);
  1351. dc->desc = "ASPEED 2500 I2C Controller";
  1352. aic->num_busses = 14;
  1353. aic->reg_size = 0x40;
  1354. aic->gap = 7;
  1355. aic->bus_get_irq = aspeed_2500_i2c_bus_get_irq;
  1356. aic->pool_size = 0x10;
  1357. aic->pool_base = 0x200;
  1358. aic->bus_pool_base = aspeed_2500_i2c_bus_pool_base;
  1359. aic->check_sram = true;
  1360. aic->has_dma = true;
  1361. aic->mem_size = 0x1000;
  1362. }
  1363. static const TypeInfo aspeed_2500_i2c_info = {
  1364. .name = TYPE_ASPEED_2500_I2C,
  1365. .parent = TYPE_ASPEED_I2C,
  1366. .class_init = aspeed_2500_i2c_class_init,
  1367. };
  1368. static qemu_irq aspeed_2600_i2c_bus_get_irq(AspeedI2CBus *bus)
  1369. {
  1370. return bus->irq;
  1371. }
  1372. static void aspeed_2600_i2c_class_init(ObjectClass *klass, void *data)
  1373. {
  1374. DeviceClass *dc = DEVICE_CLASS(klass);
  1375. AspeedI2CClass *aic = ASPEED_I2C_CLASS(klass);
  1376. dc->desc = "ASPEED 2600 I2C Controller";
  1377. aic->num_busses = 16;
  1378. aic->reg_size = 0x80;
  1379. aic->gap = -1; /* no gap */
  1380. aic->bus_get_irq = aspeed_2600_i2c_bus_get_irq;
  1381. aic->pool_size = 0x20;
  1382. aic->pool_base = 0xC00;
  1383. aic->bus_pool_base = aspeed_2500_i2c_bus_pool_base;
  1384. aic->has_dma = true;
  1385. aic->mem_size = 0x1000;
  1386. }
  1387. static const TypeInfo aspeed_2600_i2c_info = {
  1388. .name = TYPE_ASPEED_2600_I2C,
  1389. .parent = TYPE_ASPEED_I2C,
  1390. .class_init = aspeed_2600_i2c_class_init,
  1391. };
  1392. static void aspeed_1030_i2c_class_init(ObjectClass *klass, void *data)
  1393. {
  1394. DeviceClass *dc = DEVICE_CLASS(klass);
  1395. AspeedI2CClass *aic = ASPEED_I2C_CLASS(klass);
  1396. dc->desc = "ASPEED 1030 I2C Controller";
  1397. aic->num_busses = 14;
  1398. aic->reg_size = 0x80;
  1399. aic->gap = -1; /* no gap */
  1400. aic->bus_get_irq = aspeed_2600_i2c_bus_get_irq;
  1401. aic->pool_size = 0x20;
  1402. aic->pool_base = 0xC00;
  1403. aic->bus_pool_base = aspeed_2500_i2c_bus_pool_base;
  1404. aic->has_dma = true;
  1405. aic->mem_size = 0x10000;
  1406. }
  1407. static const TypeInfo aspeed_1030_i2c_info = {
  1408. .name = TYPE_ASPEED_1030_I2C,
  1409. .parent = TYPE_ASPEED_I2C,
  1410. .class_init = aspeed_1030_i2c_class_init,
  1411. };
  1412. static void aspeed_2700_i2c_class_init(ObjectClass *klass, void *data)
  1413. {
  1414. DeviceClass *dc = DEVICE_CLASS(klass);
  1415. AspeedI2CClass *aic = ASPEED_I2C_CLASS(klass);
  1416. dc->desc = "ASPEED 2700 I2C Controller";
  1417. aic->num_busses = 16;
  1418. aic->reg_size = 0x80;
  1419. aic->reg_gap_size = 0x80;
  1420. aic->gap = -1; /* no gap */
  1421. aic->bus_get_irq = aspeed_2600_i2c_bus_get_irq;
  1422. aic->pool_size = 0x20;
  1423. aic->pool_gap_size = 0xe0;
  1424. aic->pool_base = 0x1a0;
  1425. aic->bus_pool_base = aspeed_2500_i2c_bus_pool_base;
  1426. aic->has_dma = true;
  1427. aic->mem_size = 0x2000;
  1428. aic->has_dma64 = true;
  1429. }
  1430. static const TypeInfo aspeed_2700_i2c_info = {
  1431. .name = TYPE_ASPEED_2700_I2C,
  1432. .parent = TYPE_ASPEED_I2C,
  1433. .class_init = aspeed_2700_i2c_class_init,
  1434. };
  1435. static void aspeed_i2c_register_types(void)
  1436. {
  1437. type_register_static(&aspeed_i2c_bus_info);
  1438. type_register_static(&aspeed_i2c_bus_slave_info);
  1439. type_register_static(&aspeed_i2c_info);
  1440. type_register_static(&aspeed_2400_i2c_info);
  1441. type_register_static(&aspeed_2500_i2c_info);
  1442. type_register_static(&aspeed_2600_i2c_info);
  1443. type_register_static(&aspeed_1030_i2c_info);
  1444. type_register_static(&aspeed_2700_i2c_info);
  1445. }
  1446. type_init(aspeed_i2c_register_types)
  1447. I2CBus *aspeed_i2c_get_bus(AspeedI2CState *s, int busnr)
  1448. {
  1449. AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(s);
  1450. I2CBus *bus = NULL;
  1451. if (busnr >= 0 && busnr < aic->num_busses) {
  1452. bus = s->busses[busnr].bus;
  1453. }
  1454. return bus;
  1455. }