2
0

aspeed_i2c.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426
  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. }
  105. break;
  106. case A_I2CD_DMA_LEN:
  107. if (!aic->has_dma) {
  108. qemu_log_mask(LOG_GUEST_ERROR, "%s: No DMA support\n", __func__);
  109. value = -1;
  110. }
  111. break;
  112. default:
  113. qemu_log_mask(LOG_GUEST_ERROR,
  114. "%s: Bad offset 0x%" HWADDR_PRIx "\n", __func__, offset);
  115. value = -1;
  116. break;
  117. }
  118. trace_aspeed_i2c_bus_read(bus->id, offset, size, value);
  119. return value;
  120. }
  121. static uint64_t aspeed_i2c_bus_new_read(AspeedI2CBus *bus, hwaddr offset,
  122. unsigned size)
  123. {
  124. uint64_t value = bus->regs[offset / sizeof(*bus->regs)];
  125. switch (offset) {
  126. case A_I2CC_FUN_CTRL:
  127. case A_I2CC_AC_TIMING:
  128. case A_I2CC_POOL_CTRL:
  129. case A_I2CM_INTR_CTRL:
  130. case A_I2CM_INTR_STS:
  131. case A_I2CC_MS_TXRX_BYTE_BUF:
  132. case A_I2CM_DMA_LEN:
  133. case A_I2CM_DMA_TX_ADDR:
  134. case A_I2CM_DMA_RX_ADDR:
  135. case A_I2CM_DMA_LEN_STS:
  136. case A_I2CC_DMA_ADDR:
  137. case A_I2CC_DMA_LEN:
  138. case A_I2CS_DEV_ADDR:
  139. case A_I2CS_DMA_RX_ADDR:
  140. case A_I2CS_DMA_LEN:
  141. case A_I2CS_CMD:
  142. case A_I2CS_INTR_CTRL:
  143. case A_I2CS_DMA_LEN_STS:
  144. /* Value is already set, don't do anything. */
  145. break;
  146. case A_I2CS_INTR_STS:
  147. break;
  148. case A_I2CM_CMD:
  149. value = SHARED_FIELD_DP32(value, BUS_BUSY_STS, i2c_bus_busy(bus->bus));
  150. break;
  151. default:
  152. qemu_log_mask(LOG_GUEST_ERROR,
  153. "%s: Bad offset 0x%" HWADDR_PRIx "\n", __func__, offset);
  154. value = -1;
  155. break;
  156. }
  157. trace_aspeed_i2c_bus_read(bus->id, offset, size, value);
  158. return value;
  159. }
  160. static uint64_t aspeed_i2c_bus_read(void *opaque, hwaddr offset,
  161. unsigned size)
  162. {
  163. AspeedI2CBus *bus = opaque;
  164. if (aspeed_i2c_is_new_mode(bus->controller)) {
  165. return aspeed_i2c_bus_new_read(bus, offset, size);
  166. }
  167. return aspeed_i2c_bus_old_read(bus, offset, size);
  168. }
  169. static void aspeed_i2c_set_state(AspeedI2CBus *bus, uint8_t state)
  170. {
  171. if (aspeed_i2c_is_new_mode(bus->controller)) {
  172. SHARED_ARRAY_FIELD_DP32(bus->regs, R_I2CC_MS_TXRX_BYTE_BUF, TX_STATE,
  173. state);
  174. } else {
  175. SHARED_ARRAY_FIELD_DP32(bus->regs, R_I2CD_CMD, TX_STATE, state);
  176. }
  177. }
  178. static uint8_t aspeed_i2c_get_state(AspeedI2CBus *bus)
  179. {
  180. if (aspeed_i2c_is_new_mode(bus->controller)) {
  181. return SHARED_ARRAY_FIELD_EX32(bus->regs, R_I2CC_MS_TXRX_BYTE_BUF,
  182. TX_STATE);
  183. }
  184. return SHARED_ARRAY_FIELD_EX32(bus->regs, R_I2CD_CMD, TX_STATE);
  185. }
  186. static int aspeed_i2c_dma_read(AspeedI2CBus *bus, uint8_t *data)
  187. {
  188. MemTxResult result;
  189. AspeedI2CState *s = bus->controller;
  190. uint32_t reg_dma_addr = aspeed_i2c_bus_dma_addr_offset(bus);
  191. uint32_t reg_dma_len = aspeed_i2c_bus_dma_len_offset(bus);
  192. result = address_space_read(&s->dram_as, bus->regs[reg_dma_addr],
  193. MEMTXATTRS_UNSPECIFIED, data, 1);
  194. if (result != MEMTX_OK) {
  195. qemu_log_mask(LOG_GUEST_ERROR, "%s: DRAM read failed @%08x\n",
  196. __func__, bus->regs[reg_dma_addr]);
  197. return -1;
  198. }
  199. bus->regs[reg_dma_addr]++;
  200. bus->regs[reg_dma_len]--;
  201. return 0;
  202. }
  203. static int aspeed_i2c_bus_send(AspeedI2CBus *bus, uint8_t pool_start)
  204. {
  205. AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(bus->controller);
  206. int ret = -1;
  207. int i;
  208. uint32_t reg_cmd = aspeed_i2c_bus_cmd_offset(bus);
  209. uint32_t reg_pool_ctrl = aspeed_i2c_bus_pool_ctrl_offset(bus);
  210. uint32_t reg_byte_buf = aspeed_i2c_bus_byte_buf_offset(bus);
  211. uint32_t reg_dma_len = aspeed_i2c_bus_dma_len_offset(bus);
  212. int pool_tx_count = SHARED_ARRAY_FIELD_EX32(bus->regs, reg_pool_ctrl,
  213. TX_COUNT);
  214. if (SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, TX_BUFF_EN)) {
  215. for (i = pool_start; i < pool_tx_count; i++) {
  216. uint8_t *pool_base = aic->bus_pool_base(bus);
  217. trace_aspeed_i2c_bus_send("BUF", i + 1, pool_tx_count,
  218. pool_base[i]);
  219. ret = i2c_send(bus->bus, pool_base[i]);
  220. if (ret) {
  221. break;
  222. }
  223. }
  224. SHARED_ARRAY_FIELD_DP32(bus->regs, reg_cmd, TX_BUFF_EN, 0);
  225. } else if (SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, TX_DMA_EN)) {
  226. /* In new mode, clear how many bytes we TXed */
  227. if (aspeed_i2c_is_new_mode(bus->controller)) {
  228. ARRAY_FIELD_DP32(bus->regs, I2CM_DMA_LEN_STS, TX_LEN, 0);
  229. }
  230. while (bus->regs[reg_dma_len]) {
  231. uint8_t data;
  232. aspeed_i2c_dma_read(bus, &data);
  233. trace_aspeed_i2c_bus_send("DMA", bus->regs[reg_dma_len],
  234. bus->regs[reg_dma_len], data);
  235. ret = i2c_send(bus->bus, data);
  236. if (ret) {
  237. break;
  238. }
  239. /* In new mode, keep track of how many bytes we TXed */
  240. if (aspeed_i2c_is_new_mode(bus->controller)) {
  241. ARRAY_FIELD_DP32(bus->regs, I2CM_DMA_LEN_STS, TX_LEN,
  242. ARRAY_FIELD_EX32(bus->regs, I2CM_DMA_LEN_STS,
  243. TX_LEN) + 1);
  244. }
  245. }
  246. SHARED_ARRAY_FIELD_DP32(bus->regs, reg_cmd, TX_DMA_EN, 0);
  247. } else {
  248. trace_aspeed_i2c_bus_send("BYTE", pool_start, 1,
  249. bus->regs[reg_byte_buf]);
  250. ret = i2c_send(bus->bus, bus->regs[reg_byte_buf]);
  251. }
  252. return ret;
  253. }
  254. static void aspeed_i2c_bus_recv(AspeedI2CBus *bus)
  255. {
  256. AspeedI2CState *s = bus->controller;
  257. AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(s);
  258. uint8_t data;
  259. int i;
  260. uint32_t reg_cmd = aspeed_i2c_bus_cmd_offset(bus);
  261. uint32_t reg_pool_ctrl = aspeed_i2c_bus_pool_ctrl_offset(bus);
  262. uint32_t reg_byte_buf = aspeed_i2c_bus_byte_buf_offset(bus);
  263. uint32_t reg_dma_len = aspeed_i2c_bus_dma_len_offset(bus);
  264. uint32_t reg_dma_addr = aspeed_i2c_bus_dma_addr_offset(bus);
  265. int pool_rx_count = SHARED_ARRAY_FIELD_EX32(bus->regs, reg_pool_ctrl,
  266. RX_COUNT);
  267. if (SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, RX_BUFF_EN)) {
  268. uint8_t *pool_base = aic->bus_pool_base(bus);
  269. for (i = 0; i < pool_rx_count; i++) {
  270. pool_base[i] = i2c_recv(bus->bus);
  271. trace_aspeed_i2c_bus_recv("BUF", i + 1, pool_rx_count,
  272. pool_base[i]);
  273. }
  274. /* Update RX count */
  275. SHARED_ARRAY_FIELD_DP32(bus->regs, reg_pool_ctrl, RX_COUNT, i & 0xff);
  276. SHARED_ARRAY_FIELD_DP32(bus->regs, reg_cmd, RX_BUFF_EN, 0);
  277. } else if (SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, RX_DMA_EN)) {
  278. uint8_t data;
  279. /* In new mode, clear how many bytes we RXed */
  280. if (aspeed_i2c_is_new_mode(bus->controller)) {
  281. ARRAY_FIELD_DP32(bus->regs, I2CM_DMA_LEN_STS, RX_LEN, 0);
  282. }
  283. while (bus->regs[reg_dma_len]) {
  284. MemTxResult result;
  285. data = i2c_recv(bus->bus);
  286. trace_aspeed_i2c_bus_recv("DMA", bus->regs[reg_dma_len],
  287. bus->regs[reg_dma_len], data);
  288. result = address_space_write(&s->dram_as, bus->regs[reg_dma_addr],
  289. MEMTXATTRS_UNSPECIFIED, &data, 1);
  290. if (result != MEMTX_OK) {
  291. qemu_log_mask(LOG_GUEST_ERROR, "%s: DRAM write failed @%08x\n",
  292. __func__, bus->regs[reg_dma_addr]);
  293. return;
  294. }
  295. bus->regs[reg_dma_addr]++;
  296. bus->regs[reg_dma_len]--;
  297. /* In new mode, keep track of how many bytes we RXed */
  298. if (aspeed_i2c_is_new_mode(bus->controller)) {
  299. ARRAY_FIELD_DP32(bus->regs, I2CM_DMA_LEN_STS, RX_LEN,
  300. ARRAY_FIELD_EX32(bus->regs, I2CM_DMA_LEN_STS,
  301. RX_LEN) + 1);
  302. }
  303. }
  304. SHARED_ARRAY_FIELD_DP32(bus->regs, reg_cmd, RX_DMA_EN, 0);
  305. } else {
  306. data = i2c_recv(bus->bus);
  307. trace_aspeed_i2c_bus_recv("BYTE", 1, 1, bus->regs[reg_byte_buf]);
  308. SHARED_ARRAY_FIELD_DP32(bus->regs, reg_byte_buf, RX_BUF, data);
  309. }
  310. }
  311. static void aspeed_i2c_handle_rx_cmd(AspeedI2CBus *bus)
  312. {
  313. uint32_t reg_cmd = aspeed_i2c_bus_cmd_offset(bus);
  314. uint32_t reg_intr_sts = aspeed_i2c_bus_intr_sts_offset(bus);
  315. aspeed_i2c_set_state(bus, I2CD_MRXD);
  316. aspeed_i2c_bus_recv(bus);
  317. SHARED_ARRAY_FIELD_DP32(bus->regs, reg_intr_sts, RX_DONE, 1);
  318. if (SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, M_S_RX_CMD_LAST)) {
  319. i2c_nack(bus->bus);
  320. }
  321. SHARED_ARRAY_FIELD_DP32(bus->regs, reg_cmd, M_RX_CMD, 0);
  322. SHARED_ARRAY_FIELD_DP32(bus->regs, reg_cmd, M_S_RX_CMD_LAST, 0);
  323. aspeed_i2c_set_state(bus, I2CD_MACTIVE);
  324. }
  325. static uint8_t aspeed_i2c_get_addr(AspeedI2CBus *bus)
  326. {
  327. AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(bus->controller);
  328. uint32_t reg_byte_buf = aspeed_i2c_bus_byte_buf_offset(bus);
  329. uint32_t reg_cmd = aspeed_i2c_bus_cmd_offset(bus);
  330. if (aspeed_i2c_bus_pkt_mode_en(bus)) {
  331. return (ARRAY_FIELD_EX32(bus->regs, I2CM_CMD, PKT_DEV_ADDR) << 1) |
  332. SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, M_RX_CMD);
  333. }
  334. if (SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, TX_BUFF_EN)) {
  335. uint8_t *pool_base = aic->bus_pool_base(bus);
  336. return pool_base[0];
  337. } else if (SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, TX_DMA_EN)) {
  338. uint8_t data;
  339. aspeed_i2c_dma_read(bus, &data);
  340. return data;
  341. } else {
  342. return bus->regs[reg_byte_buf];
  343. }
  344. }
  345. static bool aspeed_i2c_check_sram(AspeedI2CBus *bus)
  346. {
  347. AspeedI2CState *s = bus->controller;
  348. AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(s);
  349. uint32_t reg_cmd = aspeed_i2c_bus_cmd_offset(bus);
  350. bool dma_en = SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, RX_DMA_EN) ||
  351. SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, TX_DMA_EN) ||
  352. SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, RX_BUFF_EN) ||
  353. SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, TX_BUFF_EN);
  354. if (!aic->check_sram) {
  355. return true;
  356. }
  357. /*
  358. * AST2500: SRAM must be enabled before using the Buffer Pool or
  359. * DMA mode.
  360. */
  361. if (!FIELD_EX32(s->ctrl_global, I2C_CTRL_GLOBAL, SRAM_EN) && dma_en) {
  362. qemu_log_mask(LOG_GUEST_ERROR, "%s: SRAM is not enabled\n", __func__);
  363. return false;
  364. }
  365. return true;
  366. }
  367. static void aspeed_i2c_bus_cmd_dump(AspeedI2CBus *bus)
  368. {
  369. g_autofree char *cmd_flags = NULL;
  370. uint32_t count;
  371. uint32_t reg_cmd = aspeed_i2c_bus_cmd_offset(bus);
  372. uint32_t reg_pool_ctrl = aspeed_i2c_bus_pool_ctrl_offset(bus);
  373. uint32_t reg_intr_sts = aspeed_i2c_bus_intr_sts_offset(bus);
  374. uint32_t reg_dma_len = aspeed_i2c_bus_dma_len_offset(bus);
  375. if (SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, RX_BUFF_EN)) {
  376. count = SHARED_ARRAY_FIELD_EX32(bus->regs, reg_pool_ctrl, TX_COUNT);
  377. } else if (SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, RX_DMA_EN)) {
  378. count = bus->regs[reg_dma_len];
  379. } else { /* BYTE mode */
  380. count = 1;
  381. }
  382. cmd_flags = g_strdup_printf("%s%s%s%s%s%s%s%s%s",
  383. SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, M_START_CMD) ? "start|" : "",
  384. SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, RX_DMA_EN) ? "rxdma|" : "",
  385. SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, TX_DMA_EN) ? "txdma|" : "",
  386. SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, RX_BUFF_EN) ? "rxbuf|" : "",
  387. SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, TX_BUFF_EN) ? "txbuf|" : "",
  388. SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, M_TX_CMD) ? "tx|" : "",
  389. SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, M_RX_CMD) ? "rx|" : "",
  390. SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, M_S_RX_CMD_LAST) ? "last|" : "",
  391. SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, M_STOP_CMD) ? "stop|" : "");
  392. trace_aspeed_i2c_bus_cmd(bus->regs[reg_cmd], cmd_flags, count,
  393. bus->regs[reg_intr_sts]);
  394. }
  395. /*
  396. * The state machine needs some refinement. It is only used to track
  397. * invalid STOP commands for the moment.
  398. */
  399. static void aspeed_i2c_bus_handle_cmd(AspeedI2CBus *bus, uint64_t value)
  400. {
  401. uint8_t pool_start = 0;
  402. uint32_t reg_intr_sts = aspeed_i2c_bus_intr_sts_offset(bus);
  403. uint32_t reg_cmd = aspeed_i2c_bus_cmd_offset(bus);
  404. uint32_t reg_pool_ctrl = aspeed_i2c_bus_pool_ctrl_offset(bus);
  405. uint32_t reg_dma_len = aspeed_i2c_bus_dma_len_offset(bus);
  406. if (!aspeed_i2c_check_sram(bus)) {
  407. return;
  408. }
  409. if (trace_event_get_state_backends(TRACE_ASPEED_I2C_BUS_CMD)) {
  410. aspeed_i2c_bus_cmd_dump(bus);
  411. }
  412. if (SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, M_START_CMD)) {
  413. uint8_t state = aspeed_i2c_get_state(bus) & I2CD_MACTIVE ?
  414. I2CD_MSTARTR : I2CD_MSTART;
  415. uint8_t addr;
  416. aspeed_i2c_set_state(bus, state);
  417. addr = aspeed_i2c_get_addr(bus);
  418. if (i2c_start_transfer(bus->bus, extract32(addr, 1, 7),
  419. extract32(addr, 0, 1))) {
  420. SHARED_ARRAY_FIELD_DP32(bus->regs, reg_intr_sts, TX_NAK, 1);
  421. if (aspeed_i2c_bus_pkt_mode_en(bus)) {
  422. ARRAY_FIELD_DP32(bus->regs, I2CM_INTR_STS, PKT_CMD_FAIL, 1);
  423. }
  424. } else {
  425. /* START doesn't set TX_ACK in packet mode */
  426. if (!aspeed_i2c_bus_pkt_mode_en(bus)) {
  427. SHARED_ARRAY_FIELD_DP32(bus->regs, reg_intr_sts, TX_ACK, 1);
  428. }
  429. }
  430. SHARED_ARRAY_FIELD_DP32(bus->regs, reg_cmd, M_START_CMD, 0);
  431. /*
  432. * The START command is also a TX command, as the slave
  433. * address is sent on the bus. Drop the TX flag if nothing
  434. * else needs to be sent in this sequence.
  435. */
  436. if (SHARED_ARRAY_FIELD_EX32(bus->regs, reg_cmd, TX_BUFF_EN)) {
  437. if (SHARED_ARRAY_FIELD_EX32(bus->regs, reg_pool_ctrl, TX_COUNT)
  438. == 1) {
  439. SHARED_ARRAY_FIELD_DP32(bus->regs, reg_cmd, M_TX_CMD, 0);
  440. } else {
  441. /*
  442. * Increase the start index in the TX pool buffer to
  443. * skip the address byte.
  444. */
  445. pool_start++;
  446. }
  447. } else 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 {
  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, pool_start)) {
  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->regs[R_I2CC_DMA_ADDR] = FIELD_EX32(value, I2CM_DMA_TX_ADDR, ADDR);
  585. bus->regs[R_I2CC_DMA_LEN] = ARRAY_FIELD_EX32(bus->regs, I2CM_DMA_LEN,
  586. TX_BUF_LEN) + 1;
  587. break;
  588. case A_I2CM_DMA_RX_ADDR:
  589. bus->regs[R_I2CM_DMA_RX_ADDR] = FIELD_EX32(value, I2CM_DMA_RX_ADDR,
  590. ADDR);
  591. bus->regs[R_I2CC_DMA_ADDR] = FIELD_EX32(value, I2CM_DMA_RX_ADDR, ADDR);
  592. bus->regs[R_I2CC_DMA_LEN] = ARRAY_FIELD_EX32(bus->regs, I2CM_DMA_LEN,
  593. RX_BUF_LEN) + 1;
  594. break;
  595. case A_I2CM_DMA_LEN:
  596. w1t = FIELD_EX32(value, I2CM_DMA_LEN, RX_BUF_LEN_W1T) ||
  597. FIELD_EX32(value, I2CM_DMA_LEN, TX_BUF_LEN_W1T);
  598. /* If none of the w1t bits are set, just write to the reg as normal. */
  599. if (!w1t) {
  600. bus->regs[R_I2CM_DMA_LEN] = value;
  601. break;
  602. }
  603. if (FIELD_EX32(value, I2CM_DMA_LEN, RX_BUF_LEN_W1T)) {
  604. ARRAY_FIELD_DP32(bus->regs, I2CM_DMA_LEN, RX_BUF_LEN,
  605. FIELD_EX32(value, I2CM_DMA_LEN, RX_BUF_LEN));
  606. }
  607. if (FIELD_EX32(value, I2CM_DMA_LEN, TX_BUF_LEN_W1T)) {
  608. ARRAY_FIELD_DP32(bus->regs, I2CM_DMA_LEN, TX_BUF_LEN,
  609. FIELD_EX32(value, I2CM_DMA_LEN, TX_BUF_LEN));
  610. }
  611. break;
  612. case A_I2CM_DMA_LEN_STS:
  613. /* Writes clear to 0 */
  614. bus->regs[R_I2CM_DMA_LEN_STS] = 0;
  615. break;
  616. case A_I2CC_DMA_ADDR:
  617. case A_I2CC_DMA_LEN:
  618. /* RO */
  619. break;
  620. case A_I2CS_DEV_ADDR:
  621. bus->regs[R_I2CS_DEV_ADDR] = value;
  622. break;
  623. case A_I2CS_DMA_RX_ADDR:
  624. bus->regs[R_I2CS_DMA_RX_ADDR] = value;
  625. break;
  626. case A_I2CS_DMA_LEN:
  627. assert(FIELD_EX32(value, I2CS_DMA_LEN, TX_BUF_LEN) == 0);
  628. if (FIELD_EX32(value, I2CS_DMA_LEN, RX_BUF_LEN_W1T)) {
  629. ARRAY_FIELD_DP32(bus->regs, I2CS_DMA_LEN, RX_BUF_LEN,
  630. FIELD_EX32(value, I2CS_DMA_LEN, RX_BUF_LEN));
  631. } else {
  632. bus->regs[R_I2CS_DMA_LEN] = value;
  633. }
  634. break;
  635. case A_I2CS_CMD:
  636. if (FIELD_EX32(value, I2CS_CMD, W1_CTRL)) {
  637. bus->regs[R_I2CS_CMD] |= value;
  638. } else {
  639. bus->regs[R_I2CS_CMD] = value;
  640. }
  641. i2c_slave_set_address(bus->slave, bus->regs[R_I2CS_DEV_ADDR]);
  642. break;
  643. case A_I2CS_INTR_CTRL:
  644. bus->regs[R_I2CS_INTR_CTRL] = value;
  645. break;
  646. case A_I2CS_INTR_STS:
  647. if (ARRAY_FIELD_EX32(bus->regs, I2CS_INTR_CTRL, PKT_CMD_DONE)) {
  648. if (ARRAY_FIELD_EX32(bus->regs, I2CS_INTR_STS, PKT_CMD_DONE) &&
  649. FIELD_EX32(value, I2CS_INTR_STS, PKT_CMD_DONE)) {
  650. bus->regs[R_I2CS_INTR_STS] &= 0xfffc0000;
  651. }
  652. } else {
  653. bus->regs[R_I2CS_INTR_STS] &= ~value;
  654. }
  655. if (!bus->regs[R_I2CS_INTR_STS]) {
  656. bus->controller->intr_status &= ~(1 << bus->id);
  657. qemu_irq_lower(aic->bus_get_irq(bus));
  658. }
  659. aspeed_i2c_bus_raise_interrupt(bus);
  660. break;
  661. case A_I2CS_DMA_LEN_STS:
  662. bus->regs[R_I2CS_DMA_LEN_STS] = 0;
  663. break;
  664. case A_I2CS_DMA_TX_ADDR:
  665. qemu_log_mask(LOG_UNIMP, "%s: Slave mode DMA TX is not implemented\n",
  666. __func__);
  667. break;
  668. default:
  669. qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n",
  670. __func__, offset);
  671. }
  672. }
  673. static void aspeed_i2c_bus_old_write(AspeedI2CBus *bus, hwaddr offset,
  674. uint64_t value, unsigned size)
  675. {
  676. AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(bus->controller);
  677. bool handle_rx;
  678. trace_aspeed_i2c_bus_write(bus->id, offset, size, value);
  679. switch (offset) {
  680. case A_I2CD_FUN_CTRL:
  681. if (SHARED_FIELD_EX32(value, SLAVE_EN)) {
  682. i2c_slave_set_address(bus->slave, bus->regs[R_I2CD_DEV_ADDR]);
  683. }
  684. bus->regs[R_I2CD_FUN_CTRL] = value & 0x0071C3FF;
  685. break;
  686. case A_I2CD_AC_TIMING1:
  687. bus->regs[R_I2CD_AC_TIMING1] = value & 0xFFFFF0F;
  688. break;
  689. case A_I2CD_AC_TIMING2:
  690. bus->regs[R_I2CD_AC_TIMING2] = value & 0x7;
  691. break;
  692. case A_I2CD_INTR_CTRL:
  693. bus->regs[R_I2CD_INTR_CTRL] = value & 0x7FFF;
  694. break;
  695. case A_I2CD_INTR_STS:
  696. handle_rx = SHARED_ARRAY_FIELD_EX32(bus->regs, R_I2CD_INTR_STS, RX_DONE)
  697. && SHARED_FIELD_EX32(value, RX_DONE);
  698. bus->regs[R_I2CD_INTR_STS] &= ~(value & 0x7FFF);
  699. if (!bus->regs[R_I2CD_INTR_STS]) {
  700. bus->controller->intr_status &= ~(1 << bus->id);
  701. qemu_irq_lower(aic->bus_get_irq(bus));
  702. }
  703. if (handle_rx) {
  704. if (SHARED_ARRAY_FIELD_EX32(bus->regs, R_I2CD_CMD, M_RX_CMD) ||
  705. SHARED_ARRAY_FIELD_EX32(bus->regs, R_I2CD_CMD,
  706. M_S_RX_CMD_LAST)) {
  707. aspeed_i2c_handle_rx_cmd(bus);
  708. aspeed_i2c_bus_raise_interrupt(bus);
  709. } else if (aspeed_i2c_get_state(bus) == I2CD_STXD) {
  710. i2c_ack(bus->bus);
  711. }
  712. }
  713. break;
  714. case A_I2CD_DEV_ADDR:
  715. bus->regs[R_I2CD_DEV_ADDR] = value;
  716. break;
  717. case A_I2CD_POOL_CTRL:
  718. bus->regs[R_I2CD_POOL_CTRL] &= ~0xffffff;
  719. bus->regs[R_I2CD_POOL_CTRL] |= (value & 0xffffff);
  720. break;
  721. case A_I2CD_BYTE_BUF:
  722. SHARED_ARRAY_FIELD_DP32(bus->regs, R_I2CD_BYTE_BUF, TX_BUF, value);
  723. break;
  724. case A_I2CD_CMD:
  725. if (!aspeed_i2c_bus_is_enabled(bus)) {
  726. break;
  727. }
  728. if (!aspeed_i2c_bus_is_master(bus)) {
  729. qemu_log_mask(LOG_GUEST_ERROR, "%s: Master mode is not enabled\n",
  730. __func__);
  731. break;
  732. }
  733. if (!aic->has_dma &&
  734. (SHARED_FIELD_EX32(value, RX_DMA_EN) ||
  735. SHARED_FIELD_EX32(value, TX_DMA_EN))) {
  736. qemu_log_mask(LOG_GUEST_ERROR, "%s: No DMA support\n", __func__);
  737. break;
  738. }
  739. bus->regs[R_I2CD_CMD] &= ~0xFFFF;
  740. bus->regs[R_I2CD_CMD] |= value & 0xFFFF;
  741. aspeed_i2c_bus_handle_cmd(bus, value);
  742. aspeed_i2c_bus_raise_interrupt(bus);
  743. break;
  744. case A_I2CD_DMA_ADDR:
  745. if (!aic->has_dma) {
  746. qemu_log_mask(LOG_GUEST_ERROR, "%s: No DMA support\n", __func__);
  747. break;
  748. }
  749. bus->regs[R_I2CD_DMA_ADDR] = value & 0x3ffffffc;
  750. break;
  751. case A_I2CD_DMA_LEN:
  752. if (!aic->has_dma) {
  753. qemu_log_mask(LOG_GUEST_ERROR, "%s: No DMA support\n", __func__);
  754. break;
  755. }
  756. bus->regs[R_I2CD_DMA_LEN] = value & 0xfff;
  757. if (!bus->regs[R_I2CD_DMA_LEN]) {
  758. qemu_log_mask(LOG_UNIMP, "%s: invalid DMA length\n", __func__);
  759. }
  760. break;
  761. default:
  762. qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n",
  763. __func__, offset);
  764. }
  765. }
  766. static void aspeed_i2c_bus_write(void *opaque, hwaddr offset,
  767. uint64_t value, unsigned size)
  768. {
  769. AspeedI2CBus *bus = opaque;
  770. if (aspeed_i2c_is_new_mode(bus->controller)) {
  771. aspeed_i2c_bus_new_write(bus, offset, value, size);
  772. } else {
  773. aspeed_i2c_bus_old_write(bus, offset, value, size);
  774. }
  775. }
  776. static uint64_t aspeed_i2c_ctrl_read(void *opaque, hwaddr offset,
  777. unsigned size)
  778. {
  779. AspeedI2CState *s = opaque;
  780. switch (offset) {
  781. case A_I2C_CTRL_STATUS:
  782. return s->intr_status;
  783. case A_I2C_CTRL_GLOBAL:
  784. return s->ctrl_global;
  785. case A_I2C_CTRL_NEW_CLK_DIVIDER:
  786. if (aspeed_i2c_is_new_mode(s)) {
  787. return s->new_clk_divider;
  788. }
  789. qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n",
  790. __func__, offset);
  791. break;
  792. default:
  793. qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n",
  794. __func__, offset);
  795. break;
  796. }
  797. return -1;
  798. }
  799. static void aspeed_i2c_ctrl_write(void *opaque, hwaddr offset,
  800. uint64_t value, unsigned size)
  801. {
  802. AspeedI2CState *s = opaque;
  803. switch (offset) {
  804. case A_I2C_CTRL_GLOBAL:
  805. s->ctrl_global = value;
  806. break;
  807. case A_I2C_CTRL_NEW_CLK_DIVIDER:
  808. if (aspeed_i2c_is_new_mode(s)) {
  809. s->new_clk_divider = value;
  810. } else {
  811. qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx
  812. "\n", __func__, offset);
  813. }
  814. break;
  815. case A_I2C_CTRL_STATUS:
  816. default:
  817. qemu_log_mask(LOG_GUEST_ERROR, "%s: Bad offset 0x%" HWADDR_PRIx "\n",
  818. __func__, offset);
  819. break;
  820. }
  821. }
  822. static const MemoryRegionOps aspeed_i2c_bus_ops = {
  823. .read = aspeed_i2c_bus_read,
  824. .write = aspeed_i2c_bus_write,
  825. .endianness = DEVICE_LITTLE_ENDIAN,
  826. };
  827. static const MemoryRegionOps aspeed_i2c_ctrl_ops = {
  828. .read = aspeed_i2c_ctrl_read,
  829. .write = aspeed_i2c_ctrl_write,
  830. .endianness = DEVICE_LITTLE_ENDIAN,
  831. };
  832. static uint64_t aspeed_i2c_pool_read(void *opaque, hwaddr offset,
  833. unsigned size)
  834. {
  835. AspeedI2CState *s = opaque;
  836. uint64_t ret = 0;
  837. int i;
  838. for (i = 0; i < size; i++) {
  839. ret |= (uint64_t) s->pool[offset + i] << (8 * i);
  840. }
  841. return ret;
  842. }
  843. static void aspeed_i2c_pool_write(void *opaque, hwaddr offset,
  844. uint64_t value, unsigned size)
  845. {
  846. AspeedI2CState *s = opaque;
  847. int i;
  848. for (i = 0; i < size; i++) {
  849. s->pool[offset + i] = (value >> (8 * i)) & 0xFF;
  850. }
  851. }
  852. static const MemoryRegionOps aspeed_i2c_pool_ops = {
  853. .read = aspeed_i2c_pool_read,
  854. .write = aspeed_i2c_pool_write,
  855. .endianness = DEVICE_LITTLE_ENDIAN,
  856. .valid = {
  857. .min_access_size = 1,
  858. .max_access_size = 4,
  859. },
  860. };
  861. static const VMStateDescription aspeed_i2c_bus_vmstate = {
  862. .name = TYPE_ASPEED_I2C,
  863. .version_id = 5,
  864. .minimum_version_id = 5,
  865. .fields = (VMStateField[]) {
  866. VMSTATE_UINT32_ARRAY(regs, AspeedI2CBus, ASPEED_I2C_NEW_NUM_REG),
  867. VMSTATE_END_OF_LIST()
  868. }
  869. };
  870. static const VMStateDescription aspeed_i2c_vmstate = {
  871. .name = TYPE_ASPEED_I2C,
  872. .version_id = 2,
  873. .minimum_version_id = 2,
  874. .fields = (VMStateField[]) {
  875. VMSTATE_UINT32(intr_status, AspeedI2CState),
  876. VMSTATE_STRUCT_ARRAY(busses, AspeedI2CState,
  877. ASPEED_I2C_NR_BUSSES, 1, aspeed_i2c_bus_vmstate,
  878. AspeedI2CBus),
  879. VMSTATE_UINT8_ARRAY(pool, AspeedI2CState, ASPEED_I2C_MAX_POOL_SIZE),
  880. VMSTATE_END_OF_LIST()
  881. }
  882. };
  883. static void aspeed_i2c_reset(DeviceState *dev)
  884. {
  885. AspeedI2CState *s = ASPEED_I2C(dev);
  886. s->intr_status = 0;
  887. }
  888. static void aspeed_i2c_instance_init(Object *obj)
  889. {
  890. AspeedI2CState *s = ASPEED_I2C(obj);
  891. AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(s);
  892. int i;
  893. for (i = 0; i < aic->num_busses; i++) {
  894. object_initialize_child(obj, "bus[*]", &s->busses[i],
  895. TYPE_ASPEED_I2C_BUS);
  896. }
  897. }
  898. /*
  899. * Address Definitions (AST2400 and AST2500)
  900. *
  901. * 0x000 ... 0x03F: Global Register
  902. * 0x040 ... 0x07F: Device 1
  903. * 0x080 ... 0x0BF: Device 2
  904. * 0x0C0 ... 0x0FF: Device 3
  905. * 0x100 ... 0x13F: Device 4
  906. * 0x140 ... 0x17F: Device 5
  907. * 0x180 ... 0x1BF: Device 6
  908. * 0x1C0 ... 0x1FF: Device 7
  909. * 0x200 ... 0x2FF: Buffer Pool (unused in linux driver)
  910. * 0x300 ... 0x33F: Device 8
  911. * 0x340 ... 0x37F: Device 9
  912. * 0x380 ... 0x3BF: Device 10
  913. * 0x3C0 ... 0x3FF: Device 11
  914. * 0x400 ... 0x43F: Device 12
  915. * 0x440 ... 0x47F: Device 13
  916. * 0x480 ... 0x4BF: Device 14
  917. * 0x800 ... 0xFFF: Buffer Pool (unused in linux driver)
  918. */
  919. static void aspeed_i2c_realize(DeviceState *dev, Error **errp)
  920. {
  921. int i;
  922. SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
  923. AspeedI2CState *s = ASPEED_I2C(dev);
  924. AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(s);
  925. sysbus_init_irq(sbd, &s->irq);
  926. memory_region_init_io(&s->iomem, OBJECT(s), &aspeed_i2c_ctrl_ops, s,
  927. "aspeed.i2c", 0x1000);
  928. sysbus_init_mmio(sbd, &s->iomem);
  929. for (i = 0; i < aic->num_busses; i++) {
  930. Object *bus = OBJECT(&s->busses[i]);
  931. int offset = i < aic->gap ? 1 : 5;
  932. if (!object_property_set_link(bus, "controller", OBJECT(s), errp)) {
  933. return;
  934. }
  935. if (!object_property_set_uint(bus, "bus-id", i, errp)) {
  936. return;
  937. }
  938. if (!sysbus_realize(SYS_BUS_DEVICE(bus), errp)) {
  939. return;
  940. }
  941. memory_region_add_subregion(&s->iomem, aic->reg_size * (i + offset),
  942. &s->busses[i].mr);
  943. }
  944. memory_region_init_io(&s->pool_iomem, OBJECT(s), &aspeed_i2c_pool_ops, s,
  945. "aspeed.i2c-pool", aic->pool_size);
  946. memory_region_add_subregion(&s->iomem, aic->pool_base, &s->pool_iomem);
  947. if (aic->has_dma) {
  948. if (!s->dram_mr) {
  949. error_setg(errp, TYPE_ASPEED_I2C ": 'dram' link not set");
  950. return;
  951. }
  952. address_space_init(&s->dram_as, s->dram_mr,
  953. TYPE_ASPEED_I2C "-dma-dram");
  954. }
  955. }
  956. static Property aspeed_i2c_properties[] = {
  957. DEFINE_PROP_LINK("dram", AspeedI2CState, dram_mr,
  958. TYPE_MEMORY_REGION, MemoryRegion *),
  959. DEFINE_PROP_END_OF_LIST(),
  960. };
  961. static void aspeed_i2c_class_init(ObjectClass *klass, void *data)
  962. {
  963. DeviceClass *dc = DEVICE_CLASS(klass);
  964. dc->vmsd = &aspeed_i2c_vmstate;
  965. dc->reset = aspeed_i2c_reset;
  966. device_class_set_props(dc, aspeed_i2c_properties);
  967. dc->realize = aspeed_i2c_realize;
  968. dc->desc = "Aspeed I2C Controller";
  969. }
  970. static const TypeInfo aspeed_i2c_info = {
  971. .name = TYPE_ASPEED_I2C,
  972. .parent = TYPE_SYS_BUS_DEVICE,
  973. .instance_init = aspeed_i2c_instance_init,
  974. .instance_size = sizeof(AspeedI2CState),
  975. .class_init = aspeed_i2c_class_init,
  976. .class_size = sizeof(AspeedI2CClass),
  977. .abstract = true,
  978. };
  979. static int aspeed_i2c_bus_new_slave_event(AspeedI2CBus *bus,
  980. enum i2c_event event)
  981. {
  982. switch (event) {
  983. case I2C_START_SEND_ASYNC:
  984. if (!SHARED_ARRAY_FIELD_EX32(bus->regs, R_I2CS_CMD, RX_DMA_EN)) {
  985. qemu_log_mask(LOG_GUEST_ERROR,
  986. "%s: Slave mode RX DMA is not enabled\n", __func__);
  987. return -1;
  988. }
  989. ARRAY_FIELD_DP32(bus->regs, I2CS_DMA_LEN_STS, RX_LEN, 0);
  990. bus->regs[R_I2CC_DMA_ADDR] =
  991. ARRAY_FIELD_EX32(bus->regs, I2CS_DMA_RX_ADDR, ADDR);
  992. bus->regs[R_I2CC_DMA_LEN] =
  993. ARRAY_FIELD_EX32(bus->regs, I2CS_DMA_LEN, RX_BUF_LEN) + 1;
  994. i2c_ack(bus->bus);
  995. break;
  996. case I2C_FINISH:
  997. ARRAY_FIELD_DP32(bus->regs, I2CS_INTR_STS, PKT_CMD_DONE, 1);
  998. ARRAY_FIELD_DP32(bus->regs, I2CS_INTR_STS, SLAVE_ADDR_RX_MATCH, 1);
  999. SHARED_ARRAY_FIELD_DP32(bus->regs, R_I2CS_INTR_STS, NORMAL_STOP, 1);
  1000. SHARED_ARRAY_FIELD_DP32(bus->regs, R_I2CS_INTR_STS, RX_DONE, 1);
  1001. aspeed_i2c_bus_raise_slave_interrupt(bus);
  1002. break;
  1003. default:
  1004. qemu_log_mask(LOG_UNIMP, "%s: i2c event %d unimplemented\n",
  1005. __func__, event);
  1006. return -1;
  1007. }
  1008. return 0;
  1009. }
  1010. static int aspeed_i2c_bus_slave_event(I2CSlave *slave, enum i2c_event event)
  1011. {
  1012. BusState *qbus = qdev_get_parent_bus(DEVICE(slave));
  1013. AspeedI2CBus *bus = ASPEED_I2C_BUS(qbus->parent);
  1014. uint32_t reg_intr_sts = aspeed_i2c_bus_intr_sts_offset(bus);
  1015. uint32_t reg_byte_buf = aspeed_i2c_bus_byte_buf_offset(bus);
  1016. uint32_t reg_dev_addr = aspeed_i2c_bus_dev_addr_offset(bus);
  1017. uint32_t dev_addr = SHARED_ARRAY_FIELD_EX32(bus->regs, reg_dev_addr,
  1018. SLAVE_DEV_ADDR1);
  1019. if (aspeed_i2c_is_new_mode(bus->controller)) {
  1020. return aspeed_i2c_bus_new_slave_event(bus, event);
  1021. }
  1022. switch (event) {
  1023. case I2C_START_SEND_ASYNC:
  1024. /* Bit[0] == 0 indicates "send". */
  1025. SHARED_ARRAY_FIELD_DP32(bus->regs, reg_byte_buf, RX_BUF, dev_addr << 1);
  1026. ARRAY_FIELD_DP32(bus->regs, I2CD_INTR_STS, SLAVE_ADDR_RX_MATCH, 1);
  1027. SHARED_ARRAY_FIELD_DP32(bus->regs, reg_intr_sts, RX_DONE, 1);
  1028. aspeed_i2c_set_state(bus, I2CD_STXD);
  1029. break;
  1030. case I2C_FINISH:
  1031. SHARED_ARRAY_FIELD_DP32(bus->regs, reg_intr_sts, NORMAL_STOP, 1);
  1032. aspeed_i2c_set_state(bus, I2CD_IDLE);
  1033. break;
  1034. default:
  1035. return -1;
  1036. }
  1037. aspeed_i2c_bus_raise_interrupt(bus);
  1038. return 0;
  1039. }
  1040. static void aspeed_i2c_bus_new_slave_send_async(AspeedI2CBus *bus, uint8_t data)
  1041. {
  1042. assert(address_space_write(&bus->controller->dram_as,
  1043. bus->regs[R_I2CC_DMA_ADDR],
  1044. MEMTXATTRS_UNSPECIFIED, &data, 1) == MEMTX_OK);
  1045. bus->regs[R_I2CC_DMA_ADDR]++;
  1046. bus->regs[R_I2CC_DMA_LEN]--;
  1047. ARRAY_FIELD_DP32(bus->regs, I2CS_DMA_LEN_STS, RX_LEN,
  1048. ARRAY_FIELD_EX32(bus->regs, I2CS_DMA_LEN_STS, RX_LEN) + 1);
  1049. i2c_ack(bus->bus);
  1050. }
  1051. static void aspeed_i2c_bus_slave_send_async(I2CSlave *slave, uint8_t data)
  1052. {
  1053. BusState *qbus = qdev_get_parent_bus(DEVICE(slave));
  1054. AspeedI2CBus *bus = ASPEED_I2C_BUS(qbus->parent);
  1055. uint32_t reg_intr_sts = aspeed_i2c_bus_intr_sts_offset(bus);
  1056. uint32_t reg_byte_buf = aspeed_i2c_bus_byte_buf_offset(bus);
  1057. if (aspeed_i2c_is_new_mode(bus->controller)) {
  1058. return aspeed_i2c_bus_new_slave_send_async(bus, data);
  1059. }
  1060. SHARED_ARRAY_FIELD_DP32(bus->regs, reg_byte_buf, RX_BUF, data);
  1061. SHARED_ARRAY_FIELD_DP32(bus->regs, reg_intr_sts, RX_DONE, 1);
  1062. aspeed_i2c_bus_raise_interrupt(bus);
  1063. }
  1064. static void aspeed_i2c_bus_slave_class_init(ObjectClass *klass, void *data)
  1065. {
  1066. DeviceClass *dc = DEVICE_CLASS(klass);
  1067. I2CSlaveClass *sc = I2C_SLAVE_CLASS(klass);
  1068. dc->desc = "Aspeed I2C Bus Slave";
  1069. sc->event = aspeed_i2c_bus_slave_event;
  1070. sc->send_async = aspeed_i2c_bus_slave_send_async;
  1071. }
  1072. static const TypeInfo aspeed_i2c_bus_slave_info = {
  1073. .name = TYPE_ASPEED_I2C_BUS_SLAVE,
  1074. .parent = TYPE_I2C_SLAVE,
  1075. .instance_size = sizeof(AspeedI2CBusSlave),
  1076. .class_init = aspeed_i2c_bus_slave_class_init,
  1077. };
  1078. static void aspeed_i2c_bus_reset(DeviceState *dev)
  1079. {
  1080. AspeedI2CBus *s = ASPEED_I2C_BUS(dev);
  1081. memset(s->regs, 0, sizeof(s->regs));
  1082. i2c_end_transfer(s->bus);
  1083. }
  1084. static void aspeed_i2c_bus_realize(DeviceState *dev, Error **errp)
  1085. {
  1086. AspeedI2CBus *s = ASPEED_I2C_BUS(dev);
  1087. AspeedI2CClass *aic;
  1088. g_autofree char *name = g_strdup_printf(TYPE_ASPEED_I2C_BUS ".%d", s->id);
  1089. if (!s->controller) {
  1090. error_setg(errp, TYPE_ASPEED_I2C_BUS ": 'controller' link not set");
  1091. return;
  1092. }
  1093. aic = ASPEED_I2C_GET_CLASS(s->controller);
  1094. sysbus_init_irq(SYS_BUS_DEVICE(dev), &s->irq);
  1095. s->bus = i2c_init_bus(dev, name);
  1096. s->slave = i2c_slave_create_simple(s->bus, TYPE_ASPEED_I2C_BUS_SLAVE,
  1097. 0xff);
  1098. memory_region_init_io(&s->mr, OBJECT(s), &aspeed_i2c_bus_ops,
  1099. s, name, aic->reg_size);
  1100. sysbus_init_mmio(SYS_BUS_DEVICE(dev), &s->mr);
  1101. }
  1102. static Property aspeed_i2c_bus_properties[] = {
  1103. DEFINE_PROP_UINT8("bus-id", AspeedI2CBus, id, 0),
  1104. DEFINE_PROP_LINK("controller", AspeedI2CBus, controller, TYPE_ASPEED_I2C,
  1105. AspeedI2CState *),
  1106. DEFINE_PROP_END_OF_LIST(),
  1107. };
  1108. static void aspeed_i2c_bus_class_init(ObjectClass *klass, void *data)
  1109. {
  1110. DeviceClass *dc = DEVICE_CLASS(klass);
  1111. dc->desc = "Aspeed I2C Bus";
  1112. dc->realize = aspeed_i2c_bus_realize;
  1113. dc->reset = aspeed_i2c_bus_reset;
  1114. device_class_set_props(dc, aspeed_i2c_bus_properties);
  1115. }
  1116. static const TypeInfo aspeed_i2c_bus_info = {
  1117. .name = TYPE_ASPEED_I2C_BUS,
  1118. .parent = TYPE_SYS_BUS_DEVICE,
  1119. .instance_size = sizeof(AspeedI2CBus),
  1120. .class_init = aspeed_i2c_bus_class_init,
  1121. };
  1122. static qemu_irq aspeed_2400_i2c_bus_get_irq(AspeedI2CBus *bus)
  1123. {
  1124. return bus->controller->irq;
  1125. }
  1126. static uint8_t *aspeed_2400_i2c_bus_pool_base(AspeedI2CBus *bus)
  1127. {
  1128. uint8_t *pool_page =
  1129. &bus->controller->pool[ARRAY_FIELD_EX32(bus->regs, I2CD_FUN_CTRL,
  1130. POOL_PAGE_SEL) * 0x100];
  1131. return &pool_page[ARRAY_FIELD_EX32(bus->regs, I2CD_POOL_CTRL, OFFSET)];
  1132. }
  1133. static void aspeed_2400_i2c_class_init(ObjectClass *klass, void *data)
  1134. {
  1135. DeviceClass *dc = DEVICE_CLASS(klass);
  1136. AspeedI2CClass *aic = ASPEED_I2C_CLASS(klass);
  1137. dc->desc = "ASPEED 2400 I2C Controller";
  1138. aic->num_busses = 14;
  1139. aic->reg_size = 0x40;
  1140. aic->gap = 7;
  1141. aic->bus_get_irq = aspeed_2400_i2c_bus_get_irq;
  1142. aic->pool_size = 0x800;
  1143. aic->pool_base = 0x800;
  1144. aic->bus_pool_base = aspeed_2400_i2c_bus_pool_base;
  1145. }
  1146. static const TypeInfo aspeed_2400_i2c_info = {
  1147. .name = TYPE_ASPEED_2400_I2C,
  1148. .parent = TYPE_ASPEED_I2C,
  1149. .class_init = aspeed_2400_i2c_class_init,
  1150. };
  1151. static qemu_irq aspeed_2500_i2c_bus_get_irq(AspeedI2CBus *bus)
  1152. {
  1153. return bus->controller->irq;
  1154. }
  1155. static uint8_t *aspeed_2500_i2c_bus_pool_base(AspeedI2CBus *bus)
  1156. {
  1157. return &bus->controller->pool[bus->id * 0x10];
  1158. }
  1159. static void aspeed_2500_i2c_class_init(ObjectClass *klass, void *data)
  1160. {
  1161. DeviceClass *dc = DEVICE_CLASS(klass);
  1162. AspeedI2CClass *aic = ASPEED_I2C_CLASS(klass);
  1163. dc->desc = "ASPEED 2500 I2C Controller";
  1164. aic->num_busses = 14;
  1165. aic->reg_size = 0x40;
  1166. aic->gap = 7;
  1167. aic->bus_get_irq = aspeed_2500_i2c_bus_get_irq;
  1168. aic->pool_size = 0x100;
  1169. aic->pool_base = 0x200;
  1170. aic->bus_pool_base = aspeed_2500_i2c_bus_pool_base;
  1171. aic->check_sram = true;
  1172. aic->has_dma = true;
  1173. }
  1174. static const TypeInfo aspeed_2500_i2c_info = {
  1175. .name = TYPE_ASPEED_2500_I2C,
  1176. .parent = TYPE_ASPEED_I2C,
  1177. .class_init = aspeed_2500_i2c_class_init,
  1178. };
  1179. static qemu_irq aspeed_2600_i2c_bus_get_irq(AspeedI2CBus *bus)
  1180. {
  1181. return bus->irq;
  1182. }
  1183. static uint8_t *aspeed_2600_i2c_bus_pool_base(AspeedI2CBus *bus)
  1184. {
  1185. return &bus->controller->pool[bus->id * 0x20];
  1186. }
  1187. static void aspeed_2600_i2c_class_init(ObjectClass *klass, void *data)
  1188. {
  1189. DeviceClass *dc = DEVICE_CLASS(klass);
  1190. AspeedI2CClass *aic = ASPEED_I2C_CLASS(klass);
  1191. dc->desc = "ASPEED 2600 I2C Controller";
  1192. aic->num_busses = 16;
  1193. aic->reg_size = 0x80;
  1194. aic->gap = -1; /* no gap */
  1195. aic->bus_get_irq = aspeed_2600_i2c_bus_get_irq;
  1196. aic->pool_size = 0x200;
  1197. aic->pool_base = 0xC00;
  1198. aic->bus_pool_base = aspeed_2600_i2c_bus_pool_base;
  1199. aic->has_dma = true;
  1200. }
  1201. static const TypeInfo aspeed_2600_i2c_info = {
  1202. .name = TYPE_ASPEED_2600_I2C,
  1203. .parent = TYPE_ASPEED_I2C,
  1204. .class_init = aspeed_2600_i2c_class_init,
  1205. };
  1206. static void aspeed_1030_i2c_class_init(ObjectClass *klass, void *data)
  1207. {
  1208. DeviceClass *dc = DEVICE_CLASS(klass);
  1209. AspeedI2CClass *aic = ASPEED_I2C_CLASS(klass);
  1210. dc->desc = "ASPEED 1030 I2C Controller";
  1211. aic->num_busses = 14;
  1212. aic->reg_size = 0x80;
  1213. aic->gap = -1; /* no gap */
  1214. aic->bus_get_irq = aspeed_2600_i2c_bus_get_irq;
  1215. aic->pool_size = 0x200;
  1216. aic->pool_base = 0xC00;
  1217. aic->bus_pool_base = aspeed_2600_i2c_bus_pool_base;
  1218. aic->has_dma = true;
  1219. }
  1220. static const TypeInfo aspeed_1030_i2c_info = {
  1221. .name = TYPE_ASPEED_1030_I2C,
  1222. .parent = TYPE_ASPEED_I2C,
  1223. .class_init = aspeed_1030_i2c_class_init,
  1224. };
  1225. static void aspeed_i2c_register_types(void)
  1226. {
  1227. type_register_static(&aspeed_i2c_bus_info);
  1228. type_register_static(&aspeed_i2c_bus_slave_info);
  1229. type_register_static(&aspeed_i2c_info);
  1230. type_register_static(&aspeed_2400_i2c_info);
  1231. type_register_static(&aspeed_2500_i2c_info);
  1232. type_register_static(&aspeed_2600_i2c_info);
  1233. type_register_static(&aspeed_1030_i2c_info);
  1234. }
  1235. type_init(aspeed_i2c_register_types)
  1236. I2CBus *aspeed_i2c_get_bus(AspeedI2CState *s, int busnr)
  1237. {
  1238. AspeedI2CClass *aic = ASPEED_I2C_GET_CLASS(s);
  1239. I2CBus *bus = NULL;
  1240. if (busnr >= 0 && busnr < aic->num_busses) {
  1241. bus = s->busses[busnr].bus;
  1242. }
  1243. return bus;
  1244. }