xrx200_phy_fw.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Lantiq XRX200 PHY Firmware Loader
  3. * Author: John Crispin
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published
  7. * by the Free Software Foundation.
  8. *
  9. * Copyright (C) 2012 John Crispin <john@phrozen.org>
  10. */
  11. #include <linux/delay.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/firmware.h>
  14. #include <linux/of_platform.h>
  15. #include <lantiq_soc.h>
  16. #define XRX200_GPHY_FW_ALIGN (16 * 1024)
  17. static dma_addr_t xway_gphy_load(struct platform_device *pdev)
  18. {
  19. const struct firmware *fw;
  20. dma_addr_t dev_addr = 0;
  21. const char *fw_name;
  22. void *fw_addr;
  23. size_t size;
  24. if (of_get_property(pdev->dev.of_node, "firmware1", NULL) ||
  25. of_get_property(pdev->dev.of_node, "firmware2", NULL)) {
  26. switch (ltq_soc_type()) {
  27. case SOC_TYPE_VR9:
  28. if (of_property_read_string(pdev->dev.of_node,
  29. "firmware1", &fw_name)) {
  30. dev_err(&pdev->dev,
  31. "failed to load firmware filename\n");
  32. return 0;
  33. }
  34. break;
  35. case SOC_TYPE_VR9_2:
  36. if (of_property_read_string(pdev->dev.of_node,
  37. "firmware2", &fw_name)) {
  38. dev_err(&pdev->dev,
  39. "failed to load firmware filename\n");
  40. return 0;
  41. }
  42. break;
  43. }
  44. } else if (of_property_read_string(pdev->dev.of_node,
  45. "firmware", &fw_name)) {
  46. dev_err(&pdev->dev, "failed to load firmware filename\n");
  47. return 0;
  48. }
  49. dev_info(&pdev->dev, "requesting %s\n", fw_name);
  50. if (reject_firmware(&fw, fw_name, &pdev->dev)) {
  51. dev_err(&pdev->dev, "failed to load firmware: %s\n", fw_name);
  52. return 0;
  53. }
  54. /*
  55. * GPHY cores need the firmware code in a persistent and contiguous
  56. * memory area with a 16 kB boundary aligned start address
  57. */
  58. size = fw->size + XRX200_GPHY_FW_ALIGN;
  59. fw_addr = dma_alloc_coherent(&pdev->dev, size, &dev_addr, GFP_KERNEL);
  60. if (fw_addr) {
  61. fw_addr = PTR_ALIGN(fw_addr, XRX200_GPHY_FW_ALIGN);
  62. dev_addr = ALIGN(dev_addr, XRX200_GPHY_FW_ALIGN);
  63. memcpy(fw_addr, fw->data, fw->size);
  64. } else {
  65. dev_err(&pdev->dev, "failed to alloc firmware memory\n");
  66. }
  67. release_firmware(fw);
  68. return dev_addr;
  69. }
  70. static int xway_phy_fw_probe(struct platform_device *pdev)
  71. {
  72. dma_addr_t fw_addr;
  73. struct property *pp;
  74. unsigned char *phyids;
  75. int i, ret = 0;
  76. fw_addr = xway_gphy_load(pdev);
  77. if (!fw_addr)
  78. return -EINVAL;
  79. pp = of_find_property(pdev->dev.of_node, "phys", NULL);
  80. if (!pp)
  81. return -ENOENT;
  82. phyids = pp->value;
  83. for (i = 0; i < pp->length && !ret; i++)
  84. ret = xrx200_gphy_boot(&pdev->dev, phyids[i], fw_addr);
  85. if (!ret)
  86. mdelay(100);
  87. return ret;
  88. }
  89. static const struct of_device_id xway_phy_match[] = {
  90. { .compatible = "lantiq,phy-xrx200" },
  91. {},
  92. };
  93. static struct platform_driver xway_phy_driver = {
  94. .probe = xway_phy_fw_probe,
  95. .driver = {
  96. .name = "phy-xrx200",
  97. .of_match_table = xway_phy_match,
  98. },
  99. };
  100. builtin_platform_driver(xway_phy_driver);