clk-a20-gmac.c 3.1 KB

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