xrx200_phy_fw.c 2.4 KB

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