fsl-mph-dr-of.c 9.9 KB

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