fsl-mph-dr-of.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Setup platform devices needed by the Freescale multi-port host
  4. * and/or dual-role USB controller modules based on the description
  5. * in flat device tree.
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/platform_device.h>
  9. #include <linux/fsl_devices.h>
  10. #include <linux/err.h>
  11. #include <linux/io.h>
  12. #include <linux/of_platform.h>
  13. #include <linux/clk.h>
  14. #include <linux/module.h>
  15. #include <linux/dma-mapping.h>
  16. struct fsl_usb2_dev_data {
  17. char *dr_mode; /* controller mode */
  18. char *drivers[3]; /* drivers to instantiate for this mode */
  19. enum fsl_usb2_operating_modes op_mode; /* operating mode */
  20. };
  21. static struct fsl_usb2_dev_data dr_mode_data[] = {
  22. {
  23. .dr_mode = "host",
  24. .drivers = { "fsl-ehci", NULL, NULL, },
  25. .op_mode = FSL_USB2_DR_HOST,
  26. },
  27. {
  28. .dr_mode = "otg",
  29. .drivers = { "fsl-usb2-otg", "fsl-ehci", "fsl-usb2-udc", },
  30. .op_mode = FSL_USB2_DR_OTG,
  31. },
  32. {
  33. .dr_mode = "peripheral",
  34. .drivers = { "fsl-usb2-udc", NULL, NULL, },
  35. .op_mode = FSL_USB2_DR_DEVICE,
  36. },
  37. };
  38. static struct fsl_usb2_dev_data *get_dr_mode_data(struct device_node *np)
  39. {
  40. const unsigned char *prop;
  41. int i;
  42. prop = of_get_property(np, "dr_mode", NULL);
  43. if (prop) {
  44. for (i = 0; i < ARRAY_SIZE(dr_mode_data); i++) {
  45. if (!strcmp(prop, dr_mode_data[i].dr_mode))
  46. return &dr_mode_data[i];
  47. }
  48. }
  49. pr_warn("%pOF: Invalid 'dr_mode' property, fallback to host mode\n",
  50. np);
  51. return &dr_mode_data[0]; /* mode not specified, use host */
  52. }
  53. static enum fsl_usb2_phy_modes determine_usb_phy(const char *phy_type)
  54. {
  55. if (!phy_type)
  56. return FSL_USB2_PHY_NONE;
  57. if (!strcasecmp(phy_type, "ulpi"))
  58. return FSL_USB2_PHY_ULPI;
  59. if (!strcasecmp(phy_type, "utmi"))
  60. return FSL_USB2_PHY_UTMI;
  61. if (!strcasecmp(phy_type, "utmi_wide"))
  62. return FSL_USB2_PHY_UTMI_WIDE;
  63. if (!strcasecmp(phy_type, "utmi_dual"))
  64. return FSL_USB2_PHY_UTMI_DUAL;
  65. if (!strcasecmp(phy_type, "serial"))
  66. return FSL_USB2_PHY_SERIAL;
  67. return FSL_USB2_PHY_NONE;
  68. }
  69. static struct platform_device *fsl_usb2_device_register(
  70. struct platform_device *ofdev,
  71. struct fsl_usb2_platform_data *pdata,
  72. const char *name, int id)
  73. {
  74. struct platform_device *pdev;
  75. const struct resource *res = ofdev->resource;
  76. unsigned int num = ofdev->num_resources;
  77. int retval;
  78. pdev = platform_device_alloc(name, id);
  79. if (!pdev) {
  80. retval = -ENOMEM;
  81. goto error;
  82. }
  83. pdev->dev.parent = &ofdev->dev;
  84. pdev->dev.coherent_dma_mask = ofdev->dev.coherent_dma_mask;
  85. if (!pdev->dev.dma_mask)
  86. pdev->dev.dma_mask = &ofdev->dev.coherent_dma_mask;
  87. else
  88. dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
  89. retval = platform_device_add_data(pdev, pdata, sizeof(*pdata));
  90. if (retval)
  91. goto error;
  92. if (num) {
  93. retval = platform_device_add_resources(pdev, res, num);
  94. if (retval)
  95. goto error;
  96. }
  97. retval = platform_device_add(pdev);
  98. if (retval)
  99. goto error;
  100. return pdev;
  101. error:
  102. platform_device_put(pdev);
  103. return ERR_PTR(retval);
  104. }
  105. static const struct of_device_id fsl_usb2_mph_dr_of_match[];
  106. static enum fsl_usb2_controller_ver usb_get_ver_info(struct device_node *np)
  107. {
  108. enum fsl_usb2_controller_ver ver = FSL_USB_VER_NONE;
  109. /*
  110. * returns 1 for usb controller version 1.6
  111. * returns 2 for usb controller version 2.2
  112. * returns 3 for usb controller version 2.4
  113. * returns 4 for usb controller version 2.5
  114. * returns 0 otherwise
  115. */
  116. if (of_device_is_compatible(np, "fsl-usb2-dr")) {
  117. if (of_device_is_compatible(np, "fsl-usb2-dr-v1.6"))
  118. ver = FSL_USB_VER_1_6;
  119. else if (of_device_is_compatible(np, "fsl-usb2-dr-v2.2"))
  120. ver = FSL_USB_VER_2_2;
  121. else if (of_device_is_compatible(np, "fsl-usb2-dr-v2.4"))
  122. ver = FSL_USB_VER_2_4;
  123. else if (of_device_is_compatible(np, "fsl-usb2-dr-v2.5"))
  124. ver = FSL_USB_VER_2_5;
  125. else /* for previous controller versions */
  126. ver = FSL_USB_VER_OLD;
  127. if (ver > FSL_USB_VER_NONE)
  128. return ver;
  129. }
  130. if (of_device_is_compatible(np, "fsl,mpc5121-usb2-dr"))
  131. return FSL_USB_VER_OLD;
  132. if (of_device_is_compatible(np, "fsl-usb2-mph")) {
  133. if (of_device_is_compatible(np, "fsl-usb2-mph-v1.6"))
  134. ver = FSL_USB_VER_1_6;
  135. else if (of_device_is_compatible(np, "fsl-usb2-mph-v2.2"))
  136. ver = FSL_USB_VER_2_2;
  137. else if (of_device_is_compatible(np, "fsl-usb2-mph-v2.4"))
  138. ver = FSL_USB_VER_2_4;
  139. else if (of_device_is_compatible(np, "fsl-usb2-mph-v2.5"))
  140. ver = FSL_USB_VER_2_5;
  141. else /* for previous controller versions */
  142. ver = FSL_USB_VER_OLD;
  143. }
  144. return ver;
  145. }
  146. static int fsl_usb2_mph_dr_of_probe(struct platform_device *ofdev)
  147. {
  148. struct device_node *np = ofdev->dev.of_node;
  149. struct platform_device *usb_dev;
  150. struct fsl_usb2_platform_data data, *pdata;
  151. struct fsl_usb2_dev_data *dev_data;
  152. const struct of_device_id *match;
  153. const unsigned char *prop;
  154. static unsigned int idx;
  155. int i;
  156. if (!of_device_is_available(np))
  157. return -ENODEV;
  158. match = of_match_device(fsl_usb2_mph_dr_of_match, &ofdev->dev);
  159. if (!match)
  160. return -ENODEV;
  161. pdata = &data;
  162. if (match->data)
  163. memcpy(pdata, match->data, sizeof(data));
  164. else
  165. memset(pdata, 0, sizeof(data));
  166. dev_data = get_dr_mode_data(np);
  167. if (of_device_is_compatible(np, "fsl-usb2-mph")) {
  168. if (of_get_property(np, "port0", NULL))
  169. pdata->port_enables |= FSL_USB2_PORT0_ENABLED;
  170. if (of_get_property(np, "port1", NULL))
  171. pdata->port_enables |= FSL_USB2_PORT1_ENABLED;
  172. pdata->operating_mode = FSL_USB2_MPH_HOST;
  173. } else {
  174. if (of_get_property(np, "fsl,invert-drvvbus", NULL))
  175. pdata->invert_drvvbus = 1;
  176. if (of_get_property(np, "fsl,invert-pwr-fault", NULL))
  177. pdata->invert_pwr_fault = 1;
  178. /* setup mode selected in the device tree */
  179. pdata->operating_mode = dev_data->op_mode;
  180. }
  181. prop = of_get_property(np, "phy_type", NULL);
  182. pdata->phy_mode = determine_usb_phy(prop);
  183. pdata->controller_ver = usb_get_ver_info(np);
  184. /* Activate Erratum by reading property in device tree */
  185. pdata->has_fsl_erratum_a007792 =
  186. of_property_read_bool(np, "fsl,usb-erratum-a007792");
  187. pdata->has_fsl_erratum_a005275 =
  188. of_property_read_bool(np, "fsl,usb-erratum-a005275");
  189. pdata->has_fsl_erratum_a005697 =
  190. of_property_read_bool(np, "fsl,usb_erratum-a005697");
  191. /*
  192. * Determine whether phy_clk_valid needs to be checked
  193. * by reading property in device tree
  194. */
  195. pdata->check_phy_clk_valid =
  196. of_property_read_bool(np, "phy-clk-valid");
  197. if (pdata->have_sysif_regs) {
  198. if (pdata->controller_ver == FSL_USB_VER_NONE) {
  199. dev_warn(&ofdev->dev, "Could not get controller version\n");
  200. return -ENODEV;
  201. }
  202. }
  203. for (i = 0; i < ARRAY_SIZE(dev_data->drivers); i++) {
  204. if (!dev_data->drivers[i])
  205. continue;
  206. usb_dev = fsl_usb2_device_register(ofdev, pdata,
  207. dev_data->drivers[i], idx);
  208. if (IS_ERR(usb_dev)) {
  209. dev_err(&ofdev->dev, "Can't register usb device\n");
  210. return PTR_ERR(usb_dev);
  211. }
  212. }
  213. idx++;
  214. return 0;
  215. }
  216. static int __unregister_subdev(struct device *dev, void *d)
  217. {
  218. platform_device_unregister(to_platform_device(dev));
  219. return 0;
  220. }
  221. static int fsl_usb2_mph_dr_of_remove(struct platform_device *ofdev)
  222. {
  223. device_for_each_child(&ofdev->dev, NULL, __unregister_subdev);
  224. return 0;
  225. }
  226. #ifdef CONFIG_PPC_MPC512x
  227. #define USBGENCTRL 0x200 /* NOTE: big endian */
  228. #define GC_WU_INT_CLR (1 << 5) /* Wakeup int clear */
  229. #define GC_ULPI_SEL (1 << 4) /* ULPI i/f select (usb0 only)*/
  230. #define GC_PPP (1 << 3) /* Inv. Port Power Polarity */
  231. #define GC_PFP (1 << 2) /* Inv. Power Fault Polarity */
  232. #define GC_WU_ULPI_EN (1 << 1) /* Wakeup on ULPI event */
  233. #define GC_WU_IE (1 << 1) /* Wakeup interrupt enable */
  234. #define ISIPHYCTRL 0x204 /* NOTE: big endian */
  235. #define PHYCTRL_PHYE (1 << 4) /* On-chip UTMI PHY enable */
  236. #define PHYCTRL_BSENH (1 << 3) /* Bit Stuff Enable High */
  237. #define PHYCTRL_BSEN (1 << 2) /* Bit Stuff Enable */
  238. #define PHYCTRL_LSFE (1 << 1) /* Line State Filter Enable */
  239. #define PHYCTRL_PXE (1 << 0) /* PHY oscillator enable */
  240. int fsl_usb2_mpc5121_init(struct platform_device *pdev)
  241. {
  242. struct fsl_usb2_platform_data *pdata = dev_get_platdata(&pdev->dev);
  243. struct clk *clk;
  244. int err;
  245. clk = devm_clk_get(pdev->dev.parent, "ipg");
  246. if (IS_ERR(clk)) {
  247. dev_err(&pdev->dev, "failed to get clk\n");
  248. return PTR_ERR(clk);
  249. }
  250. err = clk_prepare_enable(clk);
  251. if (err) {
  252. dev_err(&pdev->dev, "failed to enable clk\n");
  253. return err;
  254. }
  255. pdata->clk = clk;
  256. if (pdata->phy_mode == FSL_USB2_PHY_UTMI_WIDE) {
  257. u32 reg = 0;
  258. if (pdata->invert_drvvbus)
  259. reg |= GC_PPP;
  260. if (pdata->invert_pwr_fault)
  261. reg |= GC_PFP;
  262. out_be32(pdata->regs + ISIPHYCTRL, PHYCTRL_PHYE | PHYCTRL_PXE);
  263. out_be32(pdata->regs + USBGENCTRL, reg);
  264. }
  265. return 0;
  266. }
  267. static void fsl_usb2_mpc5121_exit(struct platform_device *pdev)
  268. {
  269. struct fsl_usb2_platform_data *pdata = dev_get_platdata(&pdev->dev);
  270. pdata->regs = NULL;
  271. if (pdata->clk)
  272. clk_disable_unprepare(pdata->clk);
  273. }
  274. static struct fsl_usb2_platform_data fsl_usb2_mpc5121_pd = {
  275. .big_endian_desc = 1,
  276. .big_endian_mmio = 1,
  277. .es = 1,
  278. .have_sysif_regs = 0,
  279. .le_setup_buf = 1,
  280. .init = fsl_usb2_mpc5121_init,
  281. .exit = fsl_usb2_mpc5121_exit,
  282. };
  283. #endif /* CONFIG_PPC_MPC512x */
  284. static struct fsl_usb2_platform_data fsl_usb2_mpc8xxx_pd = {
  285. .have_sysif_regs = 1,
  286. };
  287. static const struct of_device_id fsl_usb2_mph_dr_of_match[] = {
  288. { .compatible = "fsl-usb2-mph", .data = &fsl_usb2_mpc8xxx_pd, },
  289. { .compatible = "fsl-usb2-dr", .data = &fsl_usb2_mpc8xxx_pd, },
  290. #ifdef CONFIG_PPC_MPC512x
  291. { .compatible = "fsl,mpc5121-usb2-dr", .data = &fsl_usb2_mpc5121_pd, },
  292. #endif
  293. {},
  294. };
  295. MODULE_DEVICE_TABLE(of, fsl_usb2_mph_dr_of_match);
  296. static struct platform_driver fsl_usb2_mph_dr_driver = {
  297. .driver = {
  298. .name = "fsl-usb2-mph-dr",
  299. .of_match_table = fsl_usb2_mph_dr_of_match,
  300. },
  301. .probe = fsl_usb2_mph_dr_of_probe,
  302. .remove = fsl_usb2_mph_dr_of_remove,
  303. };
  304. module_platform_driver(fsl_usb2_mph_dr_driver);
  305. MODULE_DESCRIPTION("FSL MPH DR OF devices driver");
  306. MODULE_AUTHOR("Anatolij Gustschin <agust@denx.de>");
  307. MODULE_LICENSE("GPL");