ti-musb.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * MISC driver for TI MUSB Glue.
  3. *
  4. * (C) Copyright 2016
  5. * Texas Instruments Incorporated, <www.ti.com>
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <common.h>
  10. #include <command.h>
  11. #include <console.h>
  12. #include <dm.h>
  13. #include <linux/usb/otg.h>
  14. #include <dm/device-internal.h>
  15. #include <dm/lists.h>
  16. #include <asm/io.h>
  17. #include <asm/omap_musb.h>
  18. #include "musb_uboot.h"
  19. DECLARE_GLOBAL_DATA_PTR;
  20. #ifdef CONFIG_DM_USB
  21. /* USB 2.0 PHY Control */
  22. #define CM_PHY_PWRDN (1 << 0)
  23. #define CM_PHY_OTG_PWRDN (1 << 1)
  24. #define OTGVDET_EN (1 << 19)
  25. #define OTGSESSENDEN (1 << 20)
  26. #define AM335X_USB1_CTRL 0x8
  27. struct ti_musb_platdata {
  28. void *base;
  29. void *ctrl_mod_base;
  30. struct musb_hdrc_platform_data plat;
  31. struct musb_hdrc_config musb_config;
  32. struct omap_musb_board_data otg_board_data;
  33. };
  34. static int ti_musb_get_usb_index(int node)
  35. {
  36. const void *fdt = gd->fdt_blob;
  37. int i = 0;
  38. char path[64];
  39. const char *alias_path;
  40. char alias[16];
  41. fdt_get_path(fdt, node, path, sizeof(path));
  42. do {
  43. snprintf(alias, sizeof(alias), "usb%d", i);
  44. alias_path = fdt_get_alias(fdt, alias);
  45. if (alias_path == NULL) {
  46. debug("USB index not found\n");
  47. return -ENOENT;
  48. }
  49. if (!strcmp(path, alias_path))
  50. return i;
  51. i++;
  52. } while (alias_path);
  53. return -ENOENT;
  54. }
  55. static void ti_musb_set_phy_power(struct udevice *dev, u8 on)
  56. {
  57. struct ti_musb_platdata *platdata = dev_get_platdata(dev);
  58. if (on) {
  59. clrsetbits_le32(platdata->ctrl_mod_base,
  60. CM_PHY_PWRDN | CM_PHY_OTG_PWRDN,
  61. OTGVDET_EN | OTGSESSENDEN);
  62. } else {
  63. clrsetbits_le32(platdata->ctrl_mod_base, 0,
  64. CM_PHY_PWRDN | CM_PHY_OTG_PWRDN);
  65. }
  66. }
  67. static int ti_musb_ofdata_to_platdata(struct udevice *dev)
  68. {
  69. struct ti_musb_platdata *platdata = dev_get_platdata(dev);
  70. const void *fdt = gd->fdt_blob;
  71. int node = dev->of_offset;
  72. int phys;
  73. int ctrl_mod;
  74. int usb_index;
  75. platdata->base = (void *)dev_get_addr_index(dev, 1);
  76. phys = fdtdec_lookup_phandle(fdt, node, "phys");
  77. ctrl_mod = fdtdec_lookup_phandle(fdt, phys, "ti,ctrl_mod");
  78. platdata->ctrl_mod_base = (void *)fdtdec_get_addr(fdt, ctrl_mod, "reg");
  79. usb_index = ti_musb_get_usb_index(node);
  80. switch (usb_index) {
  81. case 1:
  82. platdata->ctrl_mod_base += AM335X_USB1_CTRL;
  83. case 0:
  84. default:
  85. break;
  86. }
  87. platdata->musb_config.multipoint = fdtdec_get_int(fdt, node,
  88. "mentor,multipoint",
  89. -1);
  90. if (platdata->musb_config.multipoint < 0) {
  91. error("MUSB multipoint DT entry missing\n");
  92. return -ENOENT;
  93. }
  94. platdata->musb_config.dyn_fifo = 1;
  95. platdata->musb_config.num_eps = fdtdec_get_int(fdt, node,
  96. "mentor,num-eps", -1);
  97. if (platdata->musb_config.num_eps < 0) {
  98. error("MUSB num-eps DT entry missing\n");
  99. return -ENOENT;
  100. }
  101. platdata->musb_config.ram_bits = fdtdec_get_int(fdt, node,
  102. "mentor,ram-bits", -1);
  103. if (platdata->musb_config.ram_bits < 0) {
  104. error("MUSB ram-bits DT entry missing\n");
  105. return -ENOENT;
  106. }
  107. platdata->otg_board_data.set_phy_power = ti_musb_set_phy_power;
  108. platdata->otg_board_data.dev = dev;
  109. platdata->plat.config = &platdata->musb_config;
  110. platdata->plat.power = fdtdec_get_int(fdt, node, "mentor,power", -1);
  111. if (platdata->plat.power < 0) {
  112. error("MUSB mentor,power DT entry missing\n");
  113. return -ENOENT;
  114. }
  115. platdata->plat.platform_ops = &musb_dsps_ops;
  116. platdata->plat.board_data = &platdata->otg_board_data;
  117. return 0;
  118. }
  119. static int ti_musb_host_probe(struct udevice *dev)
  120. {
  121. struct musb_host_data *host = dev_get_priv(dev);
  122. struct ti_musb_platdata *platdata = dev_get_platdata(dev);
  123. struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
  124. struct omap_musb_board_data *otg_board_data;
  125. int ret;
  126. priv->desc_before_addr = true;
  127. otg_board_data = &platdata->otg_board_data;
  128. host->host = musb_init_controller(&platdata->plat,
  129. (struct device *)otg_board_data,
  130. platdata->base);
  131. if (!host->host)
  132. return -EIO;
  133. ret = musb_lowlevel_init(host);
  134. return ret;
  135. }
  136. static int ti_musb_host_remove(struct udevice *dev)
  137. {
  138. struct musb_host_data *host = dev_get_priv(dev);
  139. musb_stop(host->host);
  140. return 0;
  141. }
  142. static int ti_musb_host_ofdata_to_platdata(struct udevice *dev)
  143. {
  144. struct ti_musb_platdata *platdata = dev_get_platdata(dev);
  145. const void *fdt = gd->fdt_blob;
  146. int node = dev->of_offset;
  147. int ret;
  148. ret = ti_musb_ofdata_to_platdata(dev);
  149. if (ret) {
  150. error("platdata dt parse error\n");
  151. return ret;
  152. }
  153. platdata->plat.mode = MUSB_HOST;
  154. return 0;
  155. }
  156. U_BOOT_DRIVER(ti_musb_host) = {
  157. .name = "ti-musb-host",
  158. .id = UCLASS_USB,
  159. .ofdata_to_platdata = ti_musb_host_ofdata_to_platdata,
  160. .probe = ti_musb_host_probe,
  161. .remove = ti_musb_host_remove,
  162. .ops = &musb_usb_ops,
  163. .platdata_auto_alloc_size = sizeof(struct ti_musb_platdata),
  164. .priv_auto_alloc_size = sizeof(struct musb_host_data),
  165. };
  166. static int ti_musb_wrapper_bind(struct udevice *parent)
  167. {
  168. const void *fdt = gd->fdt_blob;
  169. int node;
  170. int ret;
  171. for (node = fdt_first_subnode(fdt, parent->of_offset); node > 0;
  172. node = fdt_next_subnode(fdt, node)) {
  173. struct udevice *dev;
  174. const char *name = fdt_get_name(fdt, node, NULL);
  175. enum usb_dr_mode dr_mode;
  176. struct driver *drv;
  177. if (strncmp(name, "usb@", 4))
  178. continue;
  179. dr_mode = usb_get_dr_mode(node);
  180. switch (dr_mode) {
  181. case USB_DR_MODE_PERIPHERAL:
  182. /* Bind MUSB device */
  183. break;
  184. case USB_DR_MODE_HOST:
  185. /* Bind MUSB host */
  186. ret = device_bind_driver_to_node(parent, "ti-musb-host",
  187. name, node, &dev);
  188. if (ret) {
  189. error("musb - not able to bind usb host node\n");
  190. return ret;
  191. }
  192. break;
  193. default:
  194. break;
  195. };
  196. }
  197. return 0;
  198. }
  199. static const struct udevice_id ti_musb_ids[] = {
  200. { .compatible = "ti,am33xx-usb" },
  201. { }
  202. };
  203. U_BOOT_DRIVER(ti_musb_wrapper) = {
  204. .name = "ti-musb-wrapper",
  205. .id = UCLASS_MISC,
  206. .of_match = ti_musb_ids,
  207. .bind = ti_musb_wrapper_bind,
  208. };
  209. #endif /* CONFIG_DM_USB */