clk-a20-gmac.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright 2013 Emilio López
  3. * Emilio López <emilio@elopez.com.ar>
  4. *
  5. * Copyright 2013 Chen-Yu Tsai
  6. * Chen-Yu Tsai <wens@csie.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  18. #include <linux/clk-provider.h>
  19. #include <linux/of.h>
  20. #include <linux/of_address.h>
  21. #include <linux/slab.h>
  22. static DEFINE_SPINLOCK(gmac_lock);
  23. /**
  24. * sun7i_a20_gmac_clk_setup - Setup function for A20/A31 GMAC clock module
  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. #define SUN7I_A20_GMAC_GPIT 2
  48. #define SUN7I_A20_GMAC_MASK 0x3
  49. #define SUN7I_A20_GMAC_PARENTS 2
  50. static u32 sun7i_a20_gmac_mux_table[SUN7I_A20_GMAC_PARENTS] = {
  51. 0x00, /* Select mii_phy_tx_clk */
  52. 0x02, /* Select gmac_int_tx_clk */
  53. };
  54. static void __init sun7i_a20_gmac_clk_setup(struct device_node *node)
  55. {
  56. struct clk *clk;
  57. struct clk_mux *mux;
  58. struct clk_gate *gate;
  59. const char *clk_name = node->name;
  60. const char *parents[SUN7I_A20_GMAC_PARENTS];
  61. void __iomem *reg;
  62. if (of_property_read_string(node, "clock-output-names", &clk_name))
  63. return;
  64. /* allocate mux and gate clock structs */
  65. mux = kzalloc(sizeof(struct clk_mux), GFP_KERNEL);
  66. if (!mux)
  67. return;
  68. gate = kzalloc(sizeof(struct clk_gate), GFP_KERNEL);
  69. if (!gate)
  70. goto free_mux;
  71. /* gmac clock requires exactly 2 parents */
  72. if (of_clk_parent_fill(node, parents, 2) != 2)
  73. goto free_gate;
  74. reg = of_iomap(node, 0);
  75. if (!reg)
  76. goto free_gate;
  77. /* set up gate and fixed rate properties */
  78. gate->reg = reg;
  79. gate->bit_idx = SUN7I_A20_GMAC_GPIT;
  80. gate->lock = &gmac_lock;
  81. mux->reg = reg;
  82. mux->mask = SUN7I_A20_GMAC_MASK;
  83. mux->table = sun7i_a20_gmac_mux_table;
  84. mux->lock = &gmac_lock;
  85. clk = clk_register_composite(NULL, clk_name,
  86. parents, SUN7I_A20_GMAC_PARENTS,
  87. &mux->hw, &clk_mux_ops,
  88. NULL, NULL,
  89. &gate->hw, &clk_gate_ops,
  90. 0);
  91. if (IS_ERR(clk))
  92. goto iounmap_reg;
  93. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  94. return;
  95. iounmap_reg:
  96. iounmap(reg);
  97. free_gate:
  98. kfree(gate);
  99. free_mux:
  100. kfree(mux);
  101. }
  102. CLK_OF_DECLARE(sun7i_a20_gmac, "allwinner,sun7i-a20-gmac-clk",
  103. sun7i_a20_gmac_clk_setup);