acpi_device.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Generation of tables for particular device types
  4. *
  5. * Copyright 2019 Google LLC
  6. * Mostly taken from coreboot file of the same name
  7. */
  8. #include <common.h>
  9. #include <dm.h>
  10. #include <irq.h>
  11. #include <log.h>
  12. #include <usb.h>
  13. #include <acpi/acpigen.h>
  14. #include <acpi/acpi_device.h>
  15. #include <acpi/acpigen.h>
  16. #include <asm-generic/gpio.h>
  17. #include <dm/acpi.h>
  18. /**
  19. * acpi_device_path_fill() - Find the root device and build a path from there
  20. *
  21. * This recursively reaches back to the root device and progressively adds path
  22. * elements until the device is reached.
  23. *
  24. * @dev: Device to return path of
  25. * @buf: Buffer to hold the path
  26. * @buf_len: Length of buffer
  27. * @cur: Current position in the buffer
  28. * @return new position in buffer after adding @dev, or -ve on error
  29. */
  30. static int acpi_device_path_fill(const struct udevice *dev, char *buf,
  31. size_t buf_len, int cur)
  32. {
  33. char name[ACPI_NAME_MAX];
  34. int next = 0;
  35. int ret;
  36. ret = acpi_get_name(dev, name);
  37. if (ret)
  38. return ret;
  39. /*
  40. * Make sure this name segment will fit, including the path segment
  41. * separator and possible NULL terminator, if this is the last segment.
  42. */
  43. if (cur + strlen(name) + 2 > buf_len)
  44. return -ENOSPC;
  45. /* Walk up the tree to the root device */
  46. if (dev_get_parent(dev)) {
  47. next = acpi_device_path_fill(dev_get_parent(dev), buf, buf_len,
  48. cur);
  49. if (next < 0)
  50. return next;
  51. }
  52. /* Fill in the path from the root device */
  53. next += snprintf(buf + next, buf_len - next, "%s%s",
  54. dev_get_parent(dev) && *name ? "." : "", name);
  55. return next;
  56. }
  57. int acpi_device_path(const struct udevice *dev, char *buf, int maxlen)
  58. {
  59. int ret;
  60. ret = acpi_device_path_fill(dev, buf, maxlen, 0);
  61. if (ret < 0)
  62. return ret;
  63. return 0;
  64. }
  65. int acpi_device_scope(const struct udevice *dev, char *scope, int maxlen)
  66. {
  67. int ret;
  68. if (!dev_get_parent(dev))
  69. return log_msg_ret("noparent", -EINVAL);
  70. ret = acpi_device_path_fill(dev_get_parent(dev), scope, maxlen, 0);
  71. if (ret < 0)
  72. return log_msg_ret("fill", ret);
  73. return 0;
  74. }
  75. enum acpi_dev_status acpi_device_status(const struct udevice *dev)
  76. {
  77. return ACPI_DSTATUS_ALL_ON;
  78. }
  79. /**
  80. * largeres_write_len_f() - Write a placeholder word value
  81. *
  82. * Write a forward length for a large resource (2 bytes)
  83. *
  84. * @return pointer to the zero word (for fixing up later)
  85. */
  86. static void *largeres_write_len_f(struct acpi_ctx *ctx)
  87. {
  88. u8 *p = acpigen_get_current(ctx);
  89. acpigen_emit_word(ctx, 0);
  90. return p;
  91. }
  92. /**
  93. * largeres_fill_from_len() - Fill in a length value
  94. *
  95. * This calculated the number of bytes since the provided @start and writes it
  96. * to @ptr, which was previous returned by largeres_write_len_f().
  97. *
  98. * @ptr: Word to update
  99. * @start: Start address to count from to calculated the length
  100. */
  101. static void largeres_fill_from_len(struct acpi_ctx *ctx, char *ptr, u8 *start)
  102. {
  103. u16 len = acpigen_get_current(ctx) - start;
  104. ptr[0] = len & 0xff;
  105. ptr[1] = (len >> 8) & 0xff;
  106. }
  107. /**
  108. * largeres_fill_len() - Fill in a length value, excluding the length itself
  109. *
  110. * Fill in the length field with the value calculated from after the 16bit
  111. * field to acpigen current. This is useful since the length value does not
  112. * include the length field itself.
  113. *
  114. * This calls acpi_device_largeres_fill_len() passing @ptr + 2 as @start
  115. *
  116. * @ptr: Word to update.
  117. */
  118. static void largeres_fill_len(struct acpi_ctx *ctx, void *ptr)
  119. {
  120. largeres_fill_from_len(ctx, ptr, ptr + sizeof(u16));
  121. }
  122. /* ACPI 6.3 section 6.4.3.6: Extended Interrupt Descriptor */
  123. static int acpi_device_write_interrupt(struct acpi_ctx *ctx,
  124. const struct acpi_irq *irq)
  125. {
  126. void *desc_length;
  127. u8 flags;
  128. if (!irq->pin)
  129. return -ENOENT;
  130. /* This is supported by GpioInt() but not Interrupt() */
  131. if (irq->polarity == ACPI_IRQ_ACTIVE_BOTH)
  132. return -EINVAL;
  133. /* Byte 0: Descriptor Type */
  134. acpigen_emit_byte(ctx, ACPI_DESCRIPTOR_INTERRUPT);
  135. /* Byte 1-2: Length (filled in later) */
  136. desc_length = largeres_write_len_f(ctx);
  137. /*
  138. * Byte 3: Flags
  139. * [7:5]: Reserved
  140. * [4]: Wake (0=NO_WAKE 1=WAKE)
  141. * [3]: Sharing (0=EXCLUSIVE 1=SHARED)
  142. * [2]: Polarity (0=HIGH 1=LOW)
  143. * [1]: Mode (0=LEVEL 1=EDGE)
  144. * [0]: Resource (0=PRODUCER 1=CONSUMER)
  145. */
  146. flags = BIT(0); /* ResourceConsumer */
  147. if (irq->mode == ACPI_IRQ_EDGE_TRIGGERED)
  148. flags |= BIT(1);
  149. if (irq->polarity == ACPI_IRQ_ACTIVE_LOW)
  150. flags |= BIT(2);
  151. if (irq->shared == ACPI_IRQ_SHARED)
  152. flags |= BIT(3);
  153. if (irq->wake == ACPI_IRQ_WAKE)
  154. flags |= BIT(4);
  155. acpigen_emit_byte(ctx, flags);
  156. /* Byte 4: Interrupt Table Entry Count */
  157. acpigen_emit_byte(ctx, 1);
  158. /* Byte 5-8: Interrupt Number */
  159. acpigen_emit_dword(ctx, irq->pin);
  160. /* Fill in Descriptor Length (account for len word) */
  161. largeres_fill_len(ctx, desc_length);
  162. return 0;
  163. }
  164. int acpi_device_write_interrupt_irq(struct acpi_ctx *ctx,
  165. const struct irq *req_irq)
  166. {
  167. struct acpi_irq irq;
  168. int ret;
  169. ret = irq_get_acpi(req_irq, &irq);
  170. if (ret)
  171. return log_msg_ret("get", ret);
  172. ret = acpi_device_write_interrupt(ctx, &irq);
  173. if (ret)
  174. return log_msg_ret("write", ret);
  175. return irq.pin;
  176. }
  177. /* ACPI 6.3 section 6.4.3.8.1 - GPIO Interrupt or I/O */
  178. int acpi_device_write_gpio(struct acpi_ctx *ctx, const struct acpi_gpio *gpio)
  179. {
  180. void *start, *desc_length;
  181. void *pin_table_offset, *vendor_data_offset, *resource_offset;
  182. u16 flags = 0;
  183. int pin;
  184. if (gpio->type > ACPI_GPIO_TYPE_IO)
  185. return -EINVAL;
  186. start = acpigen_get_current(ctx);
  187. /* Byte 0: Descriptor Type */
  188. acpigen_emit_byte(ctx, ACPI_DESCRIPTOR_GPIO);
  189. /* Byte 1-2: Length (fill in later) */
  190. desc_length = largeres_write_len_f(ctx);
  191. /* Byte 3: Revision ID */
  192. acpigen_emit_byte(ctx, ACPI_GPIO_REVISION_ID);
  193. /* Byte 4: GpioIo or GpioInt */
  194. acpigen_emit_byte(ctx, gpio->type);
  195. /*
  196. * Byte 5-6: General Flags
  197. * [15:1]: 0 => Reserved
  198. * [0]: 1 => ResourceConsumer
  199. */
  200. acpigen_emit_word(ctx, 1 << 0);
  201. switch (gpio->type) {
  202. case ACPI_GPIO_TYPE_INTERRUPT:
  203. /*
  204. * Byte 7-8: GPIO Interrupt Flags
  205. * [15:5]: 0 => Reserved
  206. * [4]: Wake (0=NO_WAKE 1=WAKE)
  207. * [3]: Sharing (0=EXCLUSIVE 1=SHARED)
  208. * [2:1]: Polarity (0=HIGH 1=LOW 2=BOTH)
  209. * [0]: Mode (0=LEVEL 1=EDGE)
  210. */
  211. if (gpio->irq.mode == ACPI_IRQ_EDGE_TRIGGERED)
  212. flags |= 1 << 0;
  213. if (gpio->irq.shared == ACPI_IRQ_SHARED)
  214. flags |= 1 << 3;
  215. if (gpio->irq.wake == ACPI_IRQ_WAKE)
  216. flags |= 1 << 4;
  217. switch (gpio->irq.polarity) {
  218. case ACPI_IRQ_ACTIVE_HIGH:
  219. flags |= 0 << 1;
  220. break;
  221. case ACPI_IRQ_ACTIVE_LOW:
  222. flags |= 1 << 1;
  223. break;
  224. case ACPI_IRQ_ACTIVE_BOTH:
  225. flags |= 2 << 1;
  226. break;
  227. }
  228. break;
  229. case ACPI_GPIO_TYPE_IO:
  230. /*
  231. * Byte 7-8: GPIO IO Flags
  232. * [15:4]: 0 => Reserved
  233. * [3]: Sharing (0=EXCLUSIVE 1=SHARED)
  234. * [2]: 0 => Reserved
  235. * [1:0]: IO Restriction
  236. * 0 => IoRestrictionNone
  237. * 1 => IoRestrictionInputOnly
  238. * 2 => IoRestrictionOutputOnly
  239. * 3 => IoRestrictionNoneAndPreserve
  240. */
  241. flags |= gpio->io_restrict & 3;
  242. if (gpio->io_shared)
  243. flags |= 1 << 3;
  244. break;
  245. }
  246. acpigen_emit_word(ctx, flags);
  247. /*
  248. * Byte 9: Pin Configuration
  249. * 0x01 => Default (no configuration applied)
  250. * 0x02 => Pull-up
  251. * 0x03 => Pull-down
  252. * 0x04-0x7F => Reserved
  253. * 0x80-0xff => Vendor defined
  254. */
  255. acpigen_emit_byte(ctx, gpio->pull);
  256. /* Byte 10-11: Output Drive Strength in 1/100 mA */
  257. acpigen_emit_word(ctx, gpio->output_drive_strength);
  258. /* Byte 12-13: Debounce Timeout in 1/100 ms */
  259. acpigen_emit_word(ctx, gpio->interrupt_debounce_timeout);
  260. /* Byte 14-15: Pin Table Offset, relative to start */
  261. pin_table_offset = largeres_write_len_f(ctx);
  262. /* Byte 16: Reserved */
  263. acpigen_emit_byte(ctx, 0);
  264. /* Byte 17-18: Resource Source Name Offset, relative to start */
  265. resource_offset = largeres_write_len_f(ctx);
  266. /* Byte 19-20: Vendor Data Offset, relative to start */
  267. vendor_data_offset = largeres_write_len_f(ctx);
  268. /* Byte 21-22: Vendor Data Length */
  269. acpigen_emit_word(ctx, 0);
  270. /* Fill in Pin Table Offset */
  271. largeres_fill_from_len(ctx, pin_table_offset, start);
  272. /* Pin Table, one word for each pin */
  273. for (pin = 0; pin < gpio->pin_count; pin++)
  274. acpigen_emit_word(ctx, gpio->pins[pin]);
  275. /* Fill in Resource Source Name Offset */
  276. largeres_fill_from_len(ctx, resource_offset, start);
  277. /* Resource Source Name String */
  278. acpigen_emit_string(ctx, gpio->resource);
  279. /* Fill in Vendor Data Offset */
  280. largeres_fill_from_len(ctx, vendor_data_offset, start);
  281. /* Fill in GPIO Descriptor Length (account for len word) */
  282. largeres_fill_len(ctx, desc_length);
  283. return gpio->pins[0];
  284. }
  285. int acpi_device_write_gpio_desc(struct acpi_ctx *ctx,
  286. const struct gpio_desc *desc)
  287. {
  288. struct acpi_gpio gpio;
  289. int ret;
  290. ret = gpio_get_acpi(desc, &gpio);
  291. if (ret)
  292. return log_msg_ret("desc", ret);
  293. ret = acpi_device_write_gpio(ctx, &gpio);
  294. if (ret < 0)
  295. return log_msg_ret("gpio", ret);
  296. return ret;
  297. }
  298. int acpi_device_write_interrupt_or_gpio(struct acpi_ctx *ctx,
  299. struct udevice *dev, const char *prop)
  300. {
  301. struct irq req_irq;
  302. int pin;
  303. int ret;
  304. ret = irq_get_by_index(dev, 0, &req_irq);
  305. if (!ret) {
  306. ret = acpi_device_write_interrupt_irq(ctx, &req_irq);
  307. if (ret < 0)
  308. return log_msg_ret("irq", ret);
  309. pin = ret;
  310. } else {
  311. struct gpio_desc req_gpio;
  312. ret = gpio_request_by_name(dev, prop, 0, &req_gpio,
  313. GPIOD_IS_IN);
  314. if (ret)
  315. return log_msg_ret("no gpio", ret);
  316. ret = acpi_device_write_gpio_desc(ctx, &req_gpio);
  317. if (ret < 0)
  318. return log_msg_ret("gpio", ret);
  319. pin = ret;
  320. }
  321. return pin;
  322. }
  323. /* PowerResource() with Enable and/or Reset control */
  324. int acpi_device_add_power_res(struct acpi_ctx *ctx, u32 tx_state_val,
  325. const char *dw0_read, const char *dw0_write,
  326. const struct gpio_desc *reset_gpio,
  327. uint reset_delay_ms, uint reset_off_delay_ms,
  328. const struct gpio_desc *enable_gpio,
  329. uint enable_delay_ms, uint enable_off_delay_ms,
  330. const struct gpio_desc *stop_gpio,
  331. uint stop_delay_ms, uint stop_off_delay_ms)
  332. {
  333. static const char *const power_res_dev_states[] = { "_PR0", "_PR3" };
  334. struct acpi_gpio reset, enable, stop;
  335. bool has_reset, has_enable, has_stop;
  336. int ret;
  337. gpio_get_acpi(reset_gpio, &reset);
  338. gpio_get_acpi(enable_gpio, &enable);
  339. gpio_get_acpi(stop_gpio, &stop);
  340. has_reset = reset.pins[0];
  341. has_enable = enable.pins[0];
  342. has_stop = stop.pins[0];
  343. if (!has_reset && !has_enable && !has_stop)
  344. return -EINVAL;
  345. /* PowerResource (PRIC, 0, 0) */
  346. acpigen_write_power_res(ctx, "PRIC", 0, 0, power_res_dev_states,
  347. ARRAY_SIZE(power_res_dev_states));
  348. /* Method (_STA, 0, NotSerialized) { Return (0x1) } */
  349. acpigen_write_sta(ctx, 0x1);
  350. /* Method (_ON, 0, Serialized) */
  351. acpigen_write_method_serialized(ctx, "_ON", 0);
  352. if (reset_gpio) {
  353. ret = acpigen_set_enable_tx_gpio(ctx, tx_state_val, dw0_read,
  354. dw0_write, &reset, true);
  355. if (ret)
  356. return log_msg_ret("reset1", ret);
  357. }
  358. if (has_enable) {
  359. ret = acpigen_set_enable_tx_gpio(ctx, tx_state_val, dw0_read,
  360. dw0_write, &enable, true);
  361. if (ret)
  362. return log_msg_ret("enable1", ret);
  363. if (enable_delay_ms)
  364. acpigen_write_sleep(ctx, enable_delay_ms);
  365. }
  366. if (has_reset) {
  367. ret = acpigen_set_enable_tx_gpio(ctx, tx_state_val, dw0_read,
  368. dw0_write, &reset, false);
  369. if (ret)
  370. return log_msg_ret("reset2", ret);
  371. if (reset_delay_ms)
  372. acpigen_write_sleep(ctx, reset_delay_ms);
  373. }
  374. if (has_stop) {
  375. ret = acpigen_set_enable_tx_gpio(ctx, tx_state_val, dw0_read,
  376. dw0_write, &stop, false);
  377. if (ret)
  378. return log_msg_ret("stop1", ret);
  379. if (stop_delay_ms)
  380. acpigen_write_sleep(ctx, stop_delay_ms);
  381. }
  382. acpigen_pop_len(ctx); /* _ON method */
  383. /* Method (_OFF, 0, Serialized) */
  384. acpigen_write_method_serialized(ctx, "_OFF", 0);
  385. if (has_stop) {
  386. ret = acpigen_set_enable_tx_gpio(ctx, tx_state_val, dw0_read,
  387. dw0_write, &stop, true);
  388. if (ret)
  389. return log_msg_ret("stop2", ret);
  390. if (stop_off_delay_ms)
  391. acpigen_write_sleep(ctx, stop_off_delay_ms);
  392. }
  393. if (has_reset) {
  394. ret = acpigen_set_enable_tx_gpio(ctx, tx_state_val, dw0_read,
  395. dw0_write, &reset, true);
  396. if (ret)
  397. return log_msg_ret("reset3", ret);
  398. if (reset_off_delay_ms)
  399. acpigen_write_sleep(ctx, reset_off_delay_ms);
  400. }
  401. if (has_enable) {
  402. ret = acpigen_set_enable_tx_gpio(ctx, tx_state_val, dw0_read,
  403. dw0_write, &enable, false);
  404. if (ret)
  405. return log_msg_ret("enable2", ret);
  406. if (enable_off_delay_ms)
  407. acpigen_write_sleep(ctx, enable_off_delay_ms);
  408. }
  409. acpigen_pop_len(ctx); /* _OFF method */
  410. acpigen_pop_len(ctx); /* PowerResource PRIC */
  411. return 0;
  412. }
  413. int acpi_device_write_dsm_i2c_hid(struct acpi_ctx *ctx,
  414. int hid_desc_reg_offset)
  415. {
  416. int ret;
  417. acpigen_write_dsm_start(ctx);
  418. ret = acpigen_write_dsm_uuid_start(ctx, ACPI_DSM_I2C_HID_UUID);
  419. if (ret)
  420. return log_ret(ret);
  421. acpigen_write_dsm_uuid_start_cond(ctx, 0);
  422. /* ToInteger (Arg1, Local2) */
  423. acpigen_write_to_integer(ctx, ARG1_OP, LOCAL2_OP);
  424. /* If (LEqual (Local2, 0x0)) */
  425. acpigen_write_if_lequal_op_int(ctx, LOCAL2_OP, 0x0);
  426. /* Return (Buffer (One) { 0x1f }) */
  427. acpigen_write_return_singleton_buffer(ctx, 0x1f);
  428. acpigen_pop_len(ctx); /* Pop : If */
  429. /* Else */
  430. acpigen_write_else(ctx);
  431. /* If (LEqual (Local2, 0x1)) */
  432. acpigen_write_if_lequal_op_int(ctx, LOCAL2_OP, 0x1);
  433. /* Return (Buffer (One) { 0x3f }) */
  434. acpigen_write_return_singleton_buffer(ctx, 0x3f);
  435. acpigen_pop_len(ctx); /* Pop : If */
  436. /* Else */
  437. acpigen_write_else(ctx);
  438. /* Return (Buffer (One) { 0x0 }) */
  439. acpigen_write_return_singleton_buffer(ctx, 0x0);
  440. acpigen_pop_len(ctx); /* Pop : Else */
  441. acpigen_pop_len(ctx); /* Pop : Else */
  442. acpigen_write_dsm_uuid_end_cond(ctx);
  443. acpigen_write_dsm_uuid_start_cond(ctx, 1);
  444. acpigen_write_return_byte(ctx, hid_desc_reg_offset);
  445. acpigen_write_dsm_uuid_end_cond(ctx);
  446. acpigen_write_dsm_uuid_end(ctx);
  447. acpigen_write_dsm_end(ctx);
  448. return 0;
  449. }
  450. /* ACPI 6.3 section 6.4.3.8.2.1 - I2cSerialBusV2() */
  451. static void acpi_device_write_i2c(struct acpi_ctx *ctx,
  452. const struct acpi_i2c *i2c)
  453. {
  454. void *desc_length, *type_length;
  455. /* Byte 0: Descriptor Type */
  456. acpigen_emit_byte(ctx, ACPI_DESCRIPTOR_SERIAL_BUS);
  457. /* Byte 1+2: Length (filled in later) */
  458. desc_length = largeres_write_len_f(ctx);
  459. /* Byte 3: Revision ID */
  460. acpigen_emit_byte(ctx, ACPI_I2C_SERIAL_BUS_REVISION_ID);
  461. /* Byte 4: Resource Source Index is Reserved */
  462. acpigen_emit_byte(ctx, 0);
  463. /* Byte 5: Serial Bus Type is I2C */
  464. acpigen_emit_byte(ctx, ACPI_SERIAL_BUS_TYPE_I2C);
  465. /*
  466. * Byte 6: Flags
  467. * [7:2]: 0 => Reserved
  468. * [1]: 1 => ResourceConsumer
  469. * [0]: 0 => ControllerInitiated
  470. */
  471. acpigen_emit_byte(ctx, 1 << 1);
  472. /*
  473. * Byte 7-8: Type Specific Flags
  474. * [15:1]: 0 => Reserved
  475. * [0]: 0 => 7bit, 1 => 10bit
  476. */
  477. acpigen_emit_word(ctx, i2c->mode_10bit);
  478. /* Byte 9: Type Specific Revision ID */
  479. acpigen_emit_byte(ctx, ACPI_I2C_TYPE_SPECIFIC_REVISION_ID);
  480. /* Byte 10-11: I2C Type Data Length */
  481. type_length = largeres_write_len_f(ctx);
  482. /* Byte 12-15: I2C Bus Speed */
  483. acpigen_emit_dword(ctx, i2c->speed);
  484. /* Byte 16-17: I2C Slave Address */
  485. acpigen_emit_word(ctx, i2c->address);
  486. /* Fill in Type Data Length */
  487. largeres_fill_len(ctx, type_length);
  488. /* Byte 18+: ResourceSource */
  489. acpigen_emit_string(ctx, i2c->resource);
  490. /* Fill in I2C Descriptor Length */
  491. largeres_fill_len(ctx, desc_length);
  492. }
  493. /**
  494. * acpi_device_set_i2c() - Set up an ACPI I2C struct from a device
  495. *
  496. * The value of @scope is not copied, but only referenced. This implies the
  497. * caller has to ensure it stays valid for the lifetime of @i2c.
  498. *
  499. * @dev: I2C device to convert
  500. * @i2c: Place to put the new structure
  501. * @scope: Scope of the I2C device (this is the controller path)
  502. * @return chip address of device
  503. */
  504. static int acpi_device_set_i2c(const struct udevice *dev, struct acpi_i2c *i2c,
  505. const char *scope)
  506. {
  507. struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
  508. struct udevice *bus = dev_get_parent(dev);
  509. memset(i2c, '\0', sizeof(*i2c));
  510. i2c->address = chip->chip_addr;
  511. i2c->mode_10bit = 0;
  512. /*
  513. * i2c_bus->speed_hz is set if this device is probed, but if not we
  514. * must use the device tree
  515. */
  516. i2c->speed = dev_read_u32_default(bus, "clock-frequency",
  517. I2C_SPEED_STANDARD_RATE);
  518. i2c->resource = scope;
  519. return i2c->address;
  520. }
  521. int acpi_device_write_i2c_dev(struct acpi_ctx *ctx, const struct udevice *dev)
  522. {
  523. char scope[ACPI_PATH_MAX];
  524. struct acpi_i2c i2c;
  525. int ret;
  526. ret = acpi_device_scope(dev, scope, sizeof(scope));
  527. if (ret)
  528. return log_msg_ret("scope", ret);
  529. ret = acpi_device_set_i2c(dev, &i2c, scope);
  530. if (ret < 0)
  531. return log_msg_ret("set", ret);
  532. acpi_device_write_i2c(ctx, &i2c);
  533. return ret;
  534. }
  535. #ifdef CONFIG_SPI
  536. /* ACPI 6.1 section 6.4.3.8.2.2 - SpiSerialBus() */
  537. static void acpi_device_write_spi(struct acpi_ctx *ctx, const struct acpi_spi *spi)
  538. {
  539. void *desc_length, *type_length;
  540. u16 flags = 0;
  541. /* Byte 0: Descriptor Type */
  542. acpigen_emit_byte(ctx, ACPI_DESCRIPTOR_SERIAL_BUS);
  543. /* Byte 1+2: Length (filled in later) */
  544. desc_length = largeres_write_len_f(ctx);
  545. /* Byte 3: Revision ID */
  546. acpigen_emit_byte(ctx, ACPI_SPI_SERIAL_BUS_REVISION_ID);
  547. /* Byte 4: Resource Source Index is Reserved */
  548. acpigen_emit_byte(ctx, 0);
  549. /* Byte 5: Serial Bus Type is SPI */
  550. acpigen_emit_byte(ctx, ACPI_SERIAL_BUS_TYPE_SPI);
  551. /*
  552. * Byte 6: Flags
  553. * [7:2]: 0 => Reserved
  554. * [1]: 1 => ResourceConsumer
  555. * [0]: 0 => ControllerInitiated
  556. */
  557. acpigen_emit_byte(ctx, BIT(1));
  558. /*
  559. * Byte 7-8: Type Specific Flags
  560. * [15:2]: 0 => Reserveda
  561. * [1]: 0 => ActiveLow, 1 => ActiveHigh
  562. * [0]: 0 => FourWire, 1 => ThreeWire
  563. */
  564. if (spi->wire_mode == SPI_3_WIRE_MODE)
  565. flags |= BIT(0);
  566. if (spi->device_select_polarity == SPI_POLARITY_HIGH)
  567. flags |= BIT(1);
  568. acpigen_emit_word(ctx, flags);
  569. /* Byte 9: Type Specific Revision ID */
  570. acpigen_emit_byte(ctx, ACPI_SPI_TYPE_SPECIFIC_REVISION_ID);
  571. /* Byte 10-11: SPI Type Data Length */
  572. type_length = largeres_write_len_f(ctx);
  573. /* Byte 12-15: Connection Speed */
  574. acpigen_emit_dword(ctx, spi->speed);
  575. /* Byte 16: Data Bit Length */
  576. acpigen_emit_byte(ctx, spi->data_bit_length);
  577. /* Byte 17: Clock Phase */
  578. acpigen_emit_byte(ctx, spi->clock_phase);
  579. /* Byte 18: Clock Polarity */
  580. acpigen_emit_byte(ctx, spi->clock_polarity);
  581. /* Byte 19-20: Device Selection */
  582. acpigen_emit_word(ctx, spi->device_select);
  583. /* Fill in Type Data Length */
  584. largeres_fill_len(ctx, type_length);
  585. /* Byte 21+: ResourceSource String */
  586. acpigen_emit_string(ctx, spi->resource);
  587. /* Fill in SPI Descriptor Length */
  588. largeres_fill_len(ctx, desc_length);
  589. }
  590. /**
  591. * acpi_device_set_spi() - Set up an ACPI SPI struct from a device
  592. *
  593. * The value of @scope is not copied, but only referenced. This implies the
  594. * caller has to ensure it stays valid for the lifetime of @spi.
  595. *
  596. * @dev: SPI device to convert
  597. * @spi: Place to put the new structure
  598. * @scope: Scope of the SPI device (this is the controller path)
  599. * @return 0 (always)
  600. */
  601. static int acpi_device_set_spi(const struct udevice *dev, struct acpi_spi *spi,
  602. const char *scope)
  603. {
  604. struct dm_spi_slave_platdata *plat;
  605. struct spi_slave *slave = dev_get_parent_priv(dev);
  606. plat = dev_get_parent_platdata(slave->dev);
  607. memset(spi, '\0', sizeof(*spi));
  608. spi->device_select = plat->cs;
  609. spi->device_select_polarity = SPI_POLARITY_LOW;
  610. spi->wire_mode = SPI_4_WIRE_MODE;
  611. spi->speed = plat->max_hz;
  612. spi->data_bit_length = slave->wordlen;
  613. spi->clock_phase = plat->mode & SPI_CPHA ?
  614. SPI_CLOCK_PHASE_SECOND : SPI_CLOCK_PHASE_FIRST;
  615. spi->clock_polarity = plat->mode & SPI_CPOL ?
  616. SPI_POLARITY_HIGH : SPI_POLARITY_LOW;
  617. spi->resource = scope;
  618. return 0;
  619. }
  620. int acpi_device_write_spi_dev(struct acpi_ctx *ctx, const struct udevice *dev)
  621. {
  622. char scope[ACPI_PATH_MAX];
  623. struct acpi_spi spi;
  624. int ret;
  625. ret = acpi_device_scope(dev, scope, sizeof(scope));
  626. if (ret)
  627. return log_msg_ret("scope", ret);
  628. ret = acpi_device_set_spi(dev, &spi, scope);
  629. if (ret)
  630. return log_msg_ret("set", ret);
  631. acpi_device_write_spi(ctx, &spi);
  632. return 0;
  633. }
  634. #endif /* CONFIG_SPI */
  635. static const char *acpi_name_from_id(enum uclass_id id)
  636. {
  637. switch (id) {
  638. case UCLASS_USB_HUB:
  639. /* Root Hub */
  640. return "RHUB";
  641. /* DSDT: acpi/northbridge.asl */
  642. case UCLASS_NORTHBRIDGE:
  643. return "MCHC";
  644. /* DSDT: acpi/lpc.asl */
  645. case UCLASS_LPC:
  646. return "LPCB";
  647. /* DSDT: acpi/xhci.asl */
  648. case UCLASS_USB:
  649. /* This only supports USB3.0 controllers at present */
  650. return "XHCI";
  651. case UCLASS_PWM:
  652. return "PWM";
  653. default:
  654. return NULL;
  655. }
  656. }
  657. static int acpi_check_seq(const struct udevice *dev)
  658. {
  659. if (dev->req_seq == -1) {
  660. log_warning("Device '%s' has no seq\n", dev->name);
  661. return log_msg_ret("no seq", -ENXIO);
  662. }
  663. return dev->req_seq;
  664. }
  665. /* If you change this function, add test cases to dm_test_acpi_get_name() */
  666. int acpi_device_infer_name(const struct udevice *dev, char *out_name)
  667. {
  668. enum uclass_id parent_id = UCLASS_INVALID;
  669. enum uclass_id id;
  670. const char *name = NULL;
  671. id = device_get_uclass_id(dev);
  672. if (dev_get_parent(dev))
  673. parent_id = device_get_uclass_id(dev_get_parent(dev));
  674. if (id == UCLASS_SOUND)
  675. name = "HDAS";
  676. else if (id == UCLASS_PCI)
  677. name = "PCI0";
  678. else if (device_is_on_pci_bus(dev))
  679. name = acpi_name_from_id(id);
  680. if (!name) {
  681. switch (parent_id) {
  682. case UCLASS_USB: {
  683. struct usb_device *udev = dev_get_parent_priv(dev);
  684. sprintf(out_name, udev->speed >= USB_SPEED_SUPER ?
  685. "HS%02d" : "FS%02d", udev->portnr);
  686. name = out_name;
  687. break;
  688. }
  689. default:
  690. break;
  691. }
  692. }
  693. if (!name) {
  694. int num;
  695. switch (id) {
  696. /* DSDT: acpi/lpss.asl */
  697. case UCLASS_SERIAL:
  698. num = acpi_check_seq(dev);
  699. if (num < 0)
  700. return num;
  701. sprintf(out_name, "URT%d", num);
  702. name = out_name;
  703. break;
  704. case UCLASS_I2C:
  705. num = acpi_check_seq(dev);
  706. if (num < 0)
  707. return num;
  708. sprintf(out_name, "I2C%d", num);
  709. name = out_name;
  710. break;
  711. case UCLASS_SPI:
  712. num = acpi_check_seq(dev);
  713. if (num < 0)
  714. return num;
  715. sprintf(out_name, "SPI%d", num);
  716. name = out_name;
  717. break;
  718. default:
  719. break;
  720. }
  721. }
  722. if (!name) {
  723. log_warning("No name for device '%s'\n", dev->name);
  724. return -ENOENT;
  725. }
  726. if (name != out_name)
  727. acpi_copy_name(out_name, name);
  728. return 0;
  729. }