clk-a20-gmac.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2013 Emilio López
  4. * Emilio López <emilio@elopez.com.ar>
  5. *
  6. * Copyright 2013 Chen-Yu Tsai
  7. * Chen-Yu Tsai <wens@csie.org>
  8. */
  9. #include <linux/clk-provider.h>
  10. #include <linux/io.h>
  11. #include <linux/of.h>
  12. #include <linux/of_address.h>
  13. #include <linux/slab.h>
  14. static DEFINE_SPINLOCK(gmac_lock);
  15. #define SUN7I_A20_GMAC_GPIT 2
  16. #define SUN7I_A20_GMAC_MASK 0x3
  17. #define SUN7I_A20_GMAC_PARENTS 2
  18. static u32 sun7i_a20_gmac_mux_table[SUN7I_A20_GMAC_PARENTS] = {
  19. 0x00, /* Select mii_phy_tx_clk */
  20. 0x02, /* Select gmac_int_tx_clk */
  21. };
  22. /**
  23. * sun7i_a20_gmac_clk_setup - Setup function for A20/A31 GMAC clock module
  24. * @node: &struct device_node for the clock
  25. *
  26. * This clock looks something like this
  27. * ________________________
  28. * MII TX clock from PHY >-----|___________ _________|----> to GMAC core
  29. * GMAC Int. RGMII TX clk >----|___________\__/__gate---|----> to PHY
  30. * Ext. 125MHz RGMII TX clk >--|__divider__/ |
  31. * |________________________|
  32. *
  33. * The external 125 MHz reference is optional, i.e. GMAC can use its
  34. * internal TX clock just fine. The A31 GMAC clock module does not have
  35. * the divider controls for the external reference.
  36. *
  37. * To keep it simple, let the GMAC use either the MII TX clock for MII mode,
  38. * and its internal TX clock for GMII and RGMII modes. The GMAC driver should
  39. * select the appropriate source and gate/ungate the output to the PHY.
  40. *
  41. * Only the GMAC should use this clock. Altering the clock so that it doesn't
  42. * match the GMAC's operation parameters will result in the GMAC not being
  43. * able to send traffic out. The GMAC driver should set the clock rate and
  44. * enable/disable this clock to configure the required state. The clock
  45. * driver then responds by auto-reparenting the clock.
  46. */
  47. static void __init sun7i_a20_gmac_clk_setup(struct device_node *node)
  48. {
  49. struct clk *clk;
  50. struct clk_mux *mux;
  51. struct clk_gate *gate;
  52. const char *clk_name = node->name;
  53. const char *parents[SUN7I_A20_GMAC_PARENTS];
  54. void __iomem *reg;
  55. if (of_property_read_string(node, "clock-output-names", &clk_name))
  56. return;
  57. /* allocate mux and gate clock structs */
  58. mux = kzalloc_obj(struct clk_mux);
  59. if (!mux)
  60. return;
  61. gate = kzalloc_obj(struct clk_gate);
  62. if (!gate)
  63. goto free_mux;
  64. /* gmac clock requires exactly 2 parents */
  65. if (of_clk_parent_fill(node, parents, 2) != 2)
  66. goto free_gate;
  67. reg = of_iomap(node, 0);
  68. if (!reg)
  69. goto free_gate;
  70. /* set up gate and fixed rate properties */
  71. gate->reg = reg;
  72. gate->bit_idx = SUN7I_A20_GMAC_GPIT;
  73. gate->lock = &gmac_lock;
  74. mux->reg = reg;
  75. mux->mask = SUN7I_A20_GMAC_MASK;
  76. mux->table = sun7i_a20_gmac_mux_table;
  77. mux->lock = &gmac_lock;
  78. clk = clk_register_composite(NULL, clk_name,
  79. parents, SUN7I_A20_GMAC_PARENTS,
  80. &mux->hw, &clk_mux_ops,
  81. NULL, NULL,
  82. &gate->hw, &clk_gate_ops,
  83. 0);
  84. if (IS_ERR(clk))
  85. goto iounmap_reg;
  86. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  87. return;
  88. iounmap_reg:
  89. iounmap(reg);
  90. free_gate:
  91. kfree(gate);
  92. free_mux:
  93. kfree(mux);
  94. }
  95. CLK_OF_DECLARE(sun7i_a20_gmac, "allwinner,sun7i-a20-gmac-clk",
  96. sun7i_a20_gmac_clk_setup);