r9a06g032-clocks.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * R9A06G032 clock driver
  4. *
  5. * Copyright (C) 2018 Renesas Electronics Europe Limited
  6. *
  7. * Michel Pollet <[email protected]>, <[email protected]>
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/clk-provider.h>
  11. #include <linux/delay.h>
  12. #include <linux/init.h>
  13. #include <linux/io.h>
  14. #include <linux/kernel.h>
  15. #include <linux/math64.h>
  16. #include <linux/of.h>
  17. #include <linux/of_address.h>
  18. #include <linux/of_platform.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/pm_clock.h>
  21. #include <linux/pm_domain.h>
  22. #include <linux/slab.h>
  23. #include <linux/soc/renesas/r9a06g032-sysctrl.h>
  24. #include <linux/spinlock.h>
  25. #include <dt-bindings/clock/r9a06g032-sysctrl.h>
  26. #define R9A06G032_SYSCTRL_DMAMUX 0xA0
  27. struct r9a06g032_gate {
  28. u16 gate, reset, ready, midle,
  29. scon, mirack, mistat;
  30. };
  31. /* This is used to describe a clock for instantiation */
  32. struct r9a06g032_clkdesc {
  33. const char *name;
  34. uint32_t managed: 1;
  35. uint32_t type: 3;
  36. uint32_t index: 8;
  37. uint32_t source : 8; /* source index + 1 (0 == none) */
  38. /* these are used to populate the bitsel struct */
  39. union {
  40. struct r9a06g032_gate gate;
  41. /* for dividers */
  42. struct {
  43. unsigned int div_min : 10, div_max : 10, reg: 10;
  44. u16 div_table[4];
  45. };
  46. /* For fixed-factor ones */
  47. struct {
  48. u16 div, mul;
  49. };
  50. /* for dual gate */
  51. struct {
  52. uint16_t group : 1;
  53. u16 sel, g1, r1, g2, r2;
  54. } dual;
  55. };
  56. };
  57. #define I_GATE(_clk, _rst, _rdy, _midle, _scon, _mirack, _mistat) \
  58. { .gate = _clk, .reset = _rst, \
  59. .ready = _rdy, .midle = _midle, \
  60. .scon = _scon, .mirack = _mirack, .mistat = _mistat }
  61. #define D_GATE(_idx, _n, _src, ...) \
  62. { .type = K_GATE, .index = R9A06G032_##_idx, \
  63. .source = 1 + R9A06G032_##_src, .name = _n, \
  64. .gate = I_GATE(__VA_ARGS__) }
  65. #define D_MODULE(_idx, _n, _src, ...) \
  66. { .type = K_GATE, .index = R9A06G032_##_idx, \
  67. .source = 1 + R9A06G032_##_src, .name = _n, \
  68. .managed = 1, .gate = I_GATE(__VA_ARGS__) }
  69. #define D_ROOT(_idx, _n, _mul, _div) \
  70. { .type = K_FFC, .index = R9A06G032_##_idx, .name = _n, \
  71. .div = _div, .mul = _mul }
  72. #define D_FFC(_idx, _n, _src, _div) \
  73. { .type = K_FFC, .index = R9A06G032_##_idx, \
  74. .source = 1 + R9A06G032_##_src, .name = _n, \
  75. .div = _div, .mul = 1}
  76. #define D_DIV(_idx, _n, _src, _reg, _min, _max, ...) \
  77. { .type = K_DIV, .index = R9A06G032_##_idx, \
  78. .source = 1 + R9A06G032_##_src, .name = _n, \
  79. .reg = _reg, .div_min = _min, .div_max = _max, \
  80. .div_table = { __VA_ARGS__ } }
  81. #define D_UGATE(_idx, _n, _src, _g, _g1, _r1, _g2, _r2) \
  82. { .type = K_DUALGATE, .index = R9A06G032_##_idx, \
  83. .source = 1 + R9A06G032_##_src, .name = _n, \
  84. .dual = { .group = _g, \
  85. .g1 = _g1, .r1 = _r1, .g2 = _g2, .r2 = _r2 }, }
  86. enum { K_GATE = 0, K_FFC, K_DIV, K_BITSEL, K_DUALGATE };
  87. /* Internal clock IDs */
  88. #define R9A06G032_CLKOUT 0
  89. #define R9A06G032_CLKOUT_D10 2
  90. #define R9A06G032_CLKOUT_D16 3
  91. #define R9A06G032_CLKOUT_D160 4
  92. #define R9A06G032_CLKOUT_D1OR2 5
  93. #define R9A06G032_CLKOUT_D20 6
  94. #define R9A06G032_CLKOUT_D40 7
  95. #define R9A06G032_CLKOUT_D5 8
  96. #define R9A06G032_CLKOUT_D8 9
  97. #define R9A06G032_DIV_ADC 10
  98. #define R9A06G032_DIV_I2C 11
  99. #define R9A06G032_DIV_NAND 12
  100. #define R9A06G032_DIV_P1_PG 13
  101. #define R9A06G032_DIV_P2_PG 14
  102. #define R9A06G032_DIV_P3_PG 15
  103. #define R9A06G032_DIV_P4_PG 16
  104. #define R9A06G032_DIV_P5_PG 17
  105. #define R9A06G032_DIV_P6_PG 18
  106. #define R9A06G032_DIV_QSPI0 19
  107. #define R9A06G032_DIV_QSPI1 20
  108. #define R9A06G032_DIV_REF_SYNC 21
  109. #define R9A06G032_DIV_SDIO0 22
  110. #define R9A06G032_DIV_SDIO1 23
  111. #define R9A06G032_DIV_SWITCH 24
  112. #define R9A06G032_DIV_UART 25
  113. #define R9A06G032_DIV_MOTOR 64
  114. #define R9A06G032_CLK_DDRPHY_PLLCLK_D4 78
  115. #define R9A06G032_CLK_ECAT100_D4 79
  116. #define R9A06G032_CLK_HSR100_D2 80
  117. #define R9A06G032_CLK_REF_SYNC_D4 81
  118. #define R9A06G032_CLK_REF_SYNC_D8 82
  119. #define R9A06G032_CLK_SERCOS100_D2 83
  120. #define R9A06G032_DIV_CA7 84
  121. #define R9A06G032_UART_GROUP_012 154
  122. #define R9A06G032_UART_GROUP_34567 155
  123. #define R9A06G032_CLOCK_COUNT (R9A06G032_UART_GROUP_34567 + 1)
  124. static const struct r9a06g032_clkdesc r9a06g032_clocks[] = {
  125. D_ROOT(CLKOUT, "clkout", 25, 1),
  126. D_ROOT(CLK_PLL_USB, "clk_pll_usb", 12, 10),
  127. D_FFC(CLKOUT_D10, "clkout_d10", CLKOUT, 10),
  128. D_FFC(CLKOUT_D16, "clkout_d16", CLKOUT, 16),
  129. D_FFC(CLKOUT_D160, "clkout_d160", CLKOUT, 160),
  130. D_DIV(CLKOUT_D1OR2, "clkout_d1or2", CLKOUT, 0, 1, 2),
  131. D_FFC(CLKOUT_D20, "clkout_d20", CLKOUT, 20),
  132. D_FFC(CLKOUT_D40, "clkout_d40", CLKOUT, 40),
  133. D_FFC(CLKOUT_D5, "clkout_d5", CLKOUT, 5),
  134. D_FFC(CLKOUT_D8, "clkout_d8", CLKOUT, 8),
  135. D_DIV(DIV_ADC, "div_adc", CLKOUT, 77, 50, 250),
  136. D_DIV(DIV_I2C, "div_i2c", CLKOUT, 78, 12, 16),
  137. D_DIV(DIV_NAND, "div_nand", CLKOUT, 82, 12, 32),
  138. D_DIV(DIV_P1_PG, "div_p1_pg", CLKOUT, 68, 12, 200),
  139. D_DIV(DIV_P2_PG, "div_p2_pg", CLKOUT, 62, 12, 128),
  140. D_DIV(DIV_P3_PG, "div_p3_pg", CLKOUT, 64, 8, 128),
  141. D_DIV(DIV_P4_PG, "div_p4_pg", CLKOUT, 66, 8, 128),
  142. D_DIV(DIV_P5_PG, "div_p5_pg", CLKOUT, 71, 10, 40),
  143. D_DIV(DIV_P6_PG, "div_p6_pg", CLKOUT, 18, 12, 64),
  144. D_DIV(DIV_QSPI0, "div_qspi0", CLKOUT, 73, 3, 7),
  145. D_DIV(DIV_QSPI1, "div_qspi1", CLKOUT, 25, 3, 7),
  146. D_DIV(DIV_REF_SYNC, "div_ref_sync", CLKOUT, 56, 2, 16, 2, 4, 8, 16),
  147. D_DIV(DIV_SDIO0, "div_sdio0", CLKOUT, 74, 20, 128),
  148. D_DIV(DIV_SDIO1, "div_sdio1", CLKOUT, 75, 20, 128),
  149. D_DIV(DIV_SWITCH, "div_switch", CLKOUT, 37, 5, 40),
  150. D_DIV(DIV_UART, "div_uart", CLKOUT, 79, 12, 128),
  151. D_GATE(CLK_25_PG4, "clk_25_pg4", CLKOUT_D40, 0x749, 0x74a, 0x74b, 0, 0xae3, 0, 0),
  152. D_GATE(CLK_25_PG5, "clk_25_pg5", CLKOUT_D40, 0x74c, 0x74d, 0x74e, 0, 0xae4, 0, 0),
  153. D_GATE(CLK_25_PG6, "clk_25_pg6", CLKOUT_D40, 0x74f, 0x750, 0x751, 0, 0xae5, 0, 0),
  154. D_GATE(CLK_25_PG7, "clk_25_pg7", CLKOUT_D40, 0x752, 0x753, 0x754, 0, 0xae6, 0, 0),
  155. D_GATE(CLK_25_PG8, "clk_25_pg8", CLKOUT_D40, 0x755, 0x756, 0x757, 0, 0xae7, 0, 0),
  156. D_GATE(CLK_ADC, "clk_adc", DIV_ADC, 0x1ea, 0x1eb, 0, 0, 0, 0, 0),
  157. D_GATE(CLK_ECAT100, "clk_ecat100", CLKOUT_D10, 0x405, 0, 0, 0, 0, 0, 0),
  158. D_GATE(CLK_HSR100, "clk_hsr100", CLKOUT_D10, 0x483, 0, 0, 0, 0, 0, 0),
  159. D_GATE(CLK_I2C0, "clk_i2c0", DIV_I2C, 0x1e6, 0x1e7, 0, 0, 0, 0, 0),
  160. D_GATE(CLK_I2C1, "clk_i2c1", DIV_I2C, 0x1e8, 0x1e9, 0, 0, 0, 0, 0),
  161. D_GATE(CLK_MII_REF, "clk_mii_ref", CLKOUT_D40, 0x342, 0, 0, 0, 0, 0, 0),
  162. D_GATE(CLK_NAND, "clk_nand", DIV_NAND, 0x284, 0x285, 0, 0, 0, 0, 0),
  163. D_GATE(CLK_NOUSBP2_PG6, "clk_nousbp2_pg6", DIV_P2_PG, 0x774, 0x775, 0, 0, 0, 0, 0),
  164. D_GATE(CLK_P1_PG2, "clk_p1_pg2", DIV_P1_PG, 0x862, 0x863, 0, 0, 0, 0, 0),
  165. D_GATE(CLK_P1_PG3, "clk_p1_pg3", DIV_P1_PG, 0x864, 0x865, 0, 0, 0, 0, 0),
  166. D_GATE(CLK_P1_PG4, "clk_p1_pg4", DIV_P1_PG, 0x866, 0x867, 0, 0, 0, 0, 0),
  167. D_GATE(CLK_P4_PG3, "clk_p4_pg3", DIV_P4_PG, 0x824, 0x825, 0, 0, 0, 0, 0),
  168. D_GATE(CLK_P4_PG4, "clk_p4_pg4", DIV_P4_PG, 0x826, 0x827, 0, 0, 0, 0, 0),
  169. D_GATE(CLK_P6_PG1, "clk_p6_pg1", DIV_P6_PG, 0x8a0, 0x8a1, 0x8a2, 0, 0xb60, 0, 0),
  170. D_GATE(CLK_P6_PG2, "clk_p6_pg2", DIV_P6_PG, 0x8a3, 0x8a4, 0x8a5, 0, 0xb61, 0, 0),
  171. D_GATE(CLK_P6_PG3, "clk_p6_pg3", DIV_P6_PG, 0x8a6, 0x8a7, 0x8a8, 0, 0xb62, 0, 0),
  172. D_GATE(CLK_P6_PG4, "clk_p6_pg4", DIV_P6_PG, 0x8a9, 0x8aa, 0x8ab, 0, 0xb63, 0, 0),
  173. D_MODULE(CLK_PCI_USB, "clk_pci_usb", CLKOUT_D40, 0xe6, 0, 0, 0, 0, 0, 0),
  174. D_GATE(CLK_QSPI0, "clk_qspi0", DIV_QSPI0, 0x2a4, 0x2a5, 0, 0, 0, 0, 0),
  175. D_GATE(CLK_QSPI1, "clk_qspi1", DIV_QSPI1, 0x484, 0x485, 0, 0, 0, 0, 0),
  176. D_GATE(CLK_RGMII_REF, "clk_rgmii_ref", CLKOUT_D8, 0x340, 0, 0, 0, 0, 0, 0),
  177. D_GATE(CLK_RMII_REF, "clk_rmii_ref", CLKOUT_D20, 0x341, 0, 0, 0, 0, 0, 0),
  178. D_GATE(CLK_SDIO0, "clk_sdio0", DIV_SDIO0, 0x64, 0, 0, 0, 0, 0, 0),
  179. D_GATE(CLK_SDIO1, "clk_sdio1", DIV_SDIO1, 0x644, 0, 0, 0, 0, 0, 0),
  180. D_GATE(CLK_SERCOS100, "clk_sercos100", CLKOUT_D10, 0x425, 0, 0, 0, 0, 0, 0),
  181. D_GATE(CLK_SLCD, "clk_slcd", DIV_P1_PG, 0x860, 0x861, 0, 0, 0, 0, 0),
  182. D_GATE(CLK_SPI0, "clk_spi0", DIV_P3_PG, 0x7e0, 0x7e1, 0, 0, 0, 0, 0),
  183. D_GATE(CLK_SPI1, "clk_spi1", DIV_P3_PG, 0x7e2, 0x7e3, 0, 0, 0, 0, 0),
  184. D_GATE(CLK_SPI2, "clk_spi2", DIV_P3_PG, 0x7e4, 0x7e5, 0, 0, 0, 0, 0),
  185. D_GATE(CLK_SPI3, "clk_spi3", DIV_P3_PG, 0x7e6, 0x7e7, 0, 0, 0, 0, 0),
  186. D_GATE(CLK_SPI4, "clk_spi4", DIV_P4_PG, 0x820, 0x821, 0, 0, 0, 0, 0),
  187. D_GATE(CLK_SPI5, "clk_spi5", DIV_P4_PG, 0x822, 0x823, 0, 0, 0, 0, 0),
  188. D_GATE(CLK_SWITCH, "clk_switch", DIV_SWITCH, 0x982, 0x983, 0, 0, 0, 0, 0),
  189. D_DIV(DIV_MOTOR, "div_motor", CLKOUT_D5, 84, 2, 8),
  190. D_MODULE(HCLK_ECAT125, "hclk_ecat125", CLKOUT_D8, 0x400, 0x401, 0, 0x402, 0, 0x440, 0x441),
  191. D_MODULE(HCLK_PINCONFIG, "hclk_pinconfig", CLKOUT_D40, 0x740, 0x741, 0x742, 0, 0xae0, 0, 0),
  192. D_MODULE(HCLK_SERCOS, "hclk_sercos", CLKOUT_D10, 0x420, 0x422, 0, 0x421, 0, 0x460, 0x461),
  193. D_MODULE(HCLK_SGPIO2, "hclk_sgpio2", DIV_P5_PG, 0x8c3, 0x8c4, 0x8c5, 0, 0xb41, 0, 0),
  194. D_MODULE(HCLK_SGPIO3, "hclk_sgpio3", DIV_P5_PG, 0x8c6, 0x8c7, 0x8c8, 0, 0xb42, 0, 0),
  195. D_MODULE(HCLK_SGPIO4, "hclk_sgpio4", DIV_P5_PG, 0x8c9, 0x8ca, 0x8cb, 0, 0xb43, 0, 0),
  196. D_MODULE(HCLK_TIMER0, "hclk_timer0", CLKOUT_D40, 0x743, 0x744, 0x745, 0, 0xae1, 0, 0),
  197. D_MODULE(HCLK_TIMER1, "hclk_timer1", CLKOUT_D40, 0x746, 0x747, 0x748, 0, 0xae2, 0, 0),
  198. D_MODULE(HCLK_USBF, "hclk_usbf", CLKOUT_D8, 0xe3, 0, 0, 0xe4, 0, 0x102, 0x103),
  199. D_MODULE(HCLK_USBH, "hclk_usbh", CLKOUT_D8, 0xe0, 0xe1, 0, 0xe2, 0, 0x100, 0x101),
  200. D_MODULE(HCLK_USBPM, "hclk_usbpm", CLKOUT_D8, 0xe5, 0, 0, 0, 0, 0, 0),
  201. D_GATE(CLK_48_PG_F, "clk_48_pg_f", CLK_48, 0x78c, 0x78d, 0, 0x78e, 0, 0xb04, 0xb05),
  202. D_GATE(CLK_48_PG4, "clk_48_pg4", CLK_48, 0x789, 0x78a, 0x78b, 0, 0xb03, 0, 0),
  203. D_FFC(CLK_DDRPHY_PLLCLK_D4, "clk_ddrphy_pllclk_d4", CLK_DDRPHY_PLLCLK, 4),
  204. D_FFC(CLK_ECAT100_D4, "clk_ecat100_d4", CLK_ECAT100, 4),
  205. D_FFC(CLK_HSR100_D2, "clk_hsr100_d2", CLK_HSR100, 2),
  206. D_FFC(CLK_REF_SYNC_D4, "clk_ref_sync_d4", CLK_REF_SYNC, 4),
  207. D_FFC(CLK_REF_SYNC_D8, "clk_ref_sync_d8", CLK_REF_SYNC, 8),
  208. D_FFC(CLK_SERCOS100_D2, "clk_sercos100_d2", CLK_SERCOS100, 2),
  209. D_DIV(DIV_CA7, "div_ca7", CLK_REF_SYNC, 57, 1, 4, 1, 2, 4),
  210. D_MODULE(HCLK_CAN0, "hclk_can0", CLK_48, 0x783, 0x784, 0x785, 0, 0xb01, 0, 0),
  211. D_MODULE(HCLK_CAN1, "hclk_can1", CLK_48, 0x786, 0x787, 0x788, 0, 0xb02, 0, 0),
  212. D_MODULE(HCLK_DELTASIGMA, "hclk_deltasigma", DIV_MOTOR, 0x1ef, 0x1f0, 0x1f1, 0, 0, 0, 0),
  213. D_MODULE(HCLK_PWMPTO, "hclk_pwmpto", DIV_MOTOR, 0x1ec, 0x1ed, 0x1ee, 0, 0, 0, 0),
  214. D_MODULE(HCLK_RSV, "hclk_rsv", CLK_48, 0x780, 0x781, 0x782, 0, 0xb00, 0, 0),
  215. D_MODULE(HCLK_SGPIO0, "hclk_sgpio0", DIV_MOTOR, 0x1e0, 0x1e1, 0x1e2, 0, 0, 0, 0),
  216. D_MODULE(HCLK_SGPIO1, "hclk_sgpio1", DIV_MOTOR, 0x1e3, 0x1e4, 0x1e5, 0, 0, 0, 0),
  217. D_DIV(RTOS_MDC, "rtos_mdc", CLK_REF_SYNC, 100, 80, 640, 80, 160, 320, 640),
  218. D_GATE(CLK_CM3, "clk_cm3", CLK_REF_SYNC_D4, 0xba0, 0xba1, 0, 0xba2, 0, 0xbc0, 0xbc1),
  219. D_GATE(CLK_DDRC, "clk_ddrc", CLK_DDRPHY_PLLCLK_D4, 0x323, 0x324, 0, 0, 0, 0, 0),
  220. D_GATE(CLK_ECAT25, "clk_ecat25", CLK_ECAT100_D4, 0x403, 0x404, 0, 0, 0, 0, 0),
  221. D_GATE(CLK_HSR50, "clk_hsr50", CLK_HSR100_D2, 0x484, 0x485, 0, 0, 0, 0, 0),
  222. D_GATE(CLK_HW_RTOS, "clk_hw_rtos", CLK_REF_SYNC_D4, 0xc60, 0xc61, 0, 0, 0, 0, 0),
  223. D_GATE(CLK_SERCOS50, "clk_sercos50", CLK_SERCOS100_D2, 0x424, 0x423, 0, 0, 0, 0, 0),
  224. D_MODULE(HCLK_ADC, "hclk_adc", CLK_REF_SYNC_D8, 0x1af, 0x1b0, 0x1b1, 0, 0, 0, 0),
  225. D_MODULE(HCLK_CM3, "hclk_cm3", CLK_REF_SYNC_D4, 0xc20, 0xc21, 0xc22, 0, 0, 0, 0),
  226. D_MODULE(HCLK_CRYPTO_EIP150, "hclk_crypto_eip150", CLK_REF_SYNC_D4, 0x123, 0x124, 0x125, 0, 0x142, 0, 0),
  227. D_MODULE(HCLK_CRYPTO_EIP93, "hclk_crypto_eip93", CLK_REF_SYNC_D4, 0x120, 0x121, 0, 0x122, 0, 0x140, 0x141),
  228. D_MODULE(HCLK_DDRC, "hclk_ddrc", CLK_REF_SYNC_D4, 0x320, 0x322, 0, 0x321, 0, 0x3a0, 0x3a1),
  229. D_MODULE(HCLK_DMA0, "hclk_dma0", CLK_REF_SYNC_D4, 0x260, 0x261, 0x262, 0x263, 0x2c0, 0x2c1, 0x2c2),
  230. D_MODULE(HCLK_DMA1, "hclk_dma1", CLK_REF_SYNC_D4, 0x264, 0x265, 0x266, 0x267, 0x2c3, 0x2c4, 0x2c5),
  231. D_MODULE(HCLK_GMAC0, "hclk_gmac0", CLK_REF_SYNC_D4, 0x360, 0x361, 0x362, 0x363, 0x3c0, 0x3c1, 0x3c2),
  232. D_MODULE(HCLK_GMAC1, "hclk_gmac1", CLK_REF_SYNC_D4, 0x380, 0x381, 0x382, 0x383, 0x3e0, 0x3e1, 0x3e2),
  233. D_MODULE(HCLK_GPIO0, "hclk_gpio0", CLK_REF_SYNC_D4, 0x212, 0x213, 0x214, 0, 0, 0, 0),
  234. D_MODULE(HCLK_GPIO1, "hclk_gpio1", CLK_REF_SYNC_D4, 0x215, 0x216, 0x217, 0, 0, 0, 0),
  235. D_MODULE(HCLK_GPIO2, "hclk_gpio2", CLK_REF_SYNC_D4, 0x229, 0x22a, 0x22b, 0, 0, 0, 0),
  236. D_MODULE(HCLK_HSR, "hclk_hsr", CLK_HSR100_D2, 0x480, 0x482, 0, 0x481, 0, 0x4c0, 0x4c1),
  237. D_MODULE(HCLK_I2C0, "hclk_i2c0", CLK_REF_SYNC_D8, 0x1a9, 0x1aa, 0x1ab, 0, 0, 0, 0),
  238. D_MODULE(HCLK_I2C1, "hclk_i2c1", CLK_REF_SYNC_D8, 0x1ac, 0x1ad, 0x1ae, 0, 0, 0, 0),
  239. D_MODULE(HCLK_LCD, "hclk_lcd", CLK_REF_SYNC_D4, 0x7a0, 0x7a1, 0x7a2, 0, 0xb20, 0, 0),
  240. D_MODULE(HCLK_MSEBI_M, "hclk_msebi_m", CLK_REF_SYNC_D4, 0x164, 0x165, 0x166, 0, 0x183, 0, 0),
  241. D_MODULE(HCLK_MSEBI_S, "hclk_msebi_s", CLK_REF_SYNC_D4, 0x160, 0x161, 0x162, 0x163, 0x180, 0x181, 0x182),
  242. D_MODULE(HCLK_NAND, "hclk_nand", CLK_REF_SYNC_D4, 0x280, 0x281, 0x282, 0x283, 0x2e0, 0x2e1, 0x2e2),
  243. D_MODULE(HCLK_PG_I, "hclk_pg_i", CLK_REF_SYNC_D4, 0x7ac, 0x7ad, 0, 0x7ae, 0, 0xb24, 0xb25),
  244. D_MODULE(HCLK_PG19, "hclk_pg19", CLK_REF_SYNC_D4, 0x22c, 0x22d, 0x22e, 0, 0, 0, 0),
  245. D_MODULE(HCLK_PG20, "hclk_pg20", CLK_REF_SYNC_D4, 0x22f, 0x230, 0x231, 0, 0, 0, 0),
  246. D_MODULE(HCLK_PG3, "hclk_pg3", CLK_REF_SYNC_D4, 0x7a6, 0x7a7, 0x7a8, 0, 0xb22, 0, 0),
  247. D_MODULE(HCLK_PG4, "hclk_pg4", CLK_REF_SYNC_D4, 0x7a9, 0x7aa, 0x7ab, 0, 0xb23, 0, 0),
  248. D_MODULE(HCLK_QSPI0, "hclk_qspi0", CLK_REF_SYNC_D4, 0x2a0, 0x2a1, 0x2a2, 0x2a3, 0x300, 0x301, 0x302),
  249. D_MODULE(HCLK_QSPI1, "hclk_qspi1", CLK_REF_SYNC_D4, 0x480, 0x481, 0x482, 0x483, 0x4c0, 0x4c1, 0x4c2),
  250. D_MODULE(HCLK_ROM, "hclk_rom", CLK_REF_SYNC_D4, 0xaa0, 0xaa1, 0xaa2, 0, 0xb80, 0, 0),
  251. D_MODULE(HCLK_RTC, "hclk_rtc", CLK_REF_SYNC_D8, 0xa00, 0xa03, 0, 0xa02, 0, 0, 0),
  252. D_MODULE(HCLK_SDIO0, "hclk_sdio0", CLK_REF_SYNC_D4, 0x60, 0x61, 0x62, 0x63, 0x80, 0x81, 0x82),
  253. D_MODULE(HCLK_SDIO1, "hclk_sdio1", CLK_REF_SYNC_D4, 0x640, 0x641, 0x642, 0x643, 0x660, 0x661, 0x662),
  254. D_MODULE(HCLK_SEMAP, "hclk_semap", CLK_REF_SYNC_D4, 0x7a3, 0x7a4, 0x7a5, 0, 0xb21, 0, 0),
  255. D_MODULE(HCLK_SPI0, "hclk_spi0", CLK_REF_SYNC_D4, 0x200, 0x201, 0x202, 0, 0, 0, 0),
  256. D_MODULE(HCLK_SPI1, "hclk_spi1", CLK_REF_SYNC_D4, 0x203, 0x204, 0x205, 0, 0, 0, 0),
  257. D_MODULE(HCLK_SPI2, "hclk_spi2", CLK_REF_SYNC_D4, 0x206, 0x207, 0x208, 0, 0, 0, 0),
  258. D_MODULE(HCLK_SPI3, "hclk_spi3", CLK_REF_SYNC_D4, 0x209, 0x20a, 0x20b, 0, 0, 0, 0),
  259. D_MODULE(HCLK_SPI4, "hclk_spi4", CLK_REF_SYNC_D4, 0x20c, 0x20d, 0x20e, 0, 0, 0, 0),
  260. D_MODULE(HCLK_SPI5, "hclk_spi5", CLK_REF_SYNC_D4, 0x20f, 0x210, 0x211, 0, 0, 0, 0),
  261. D_MODULE(HCLK_SWITCH, "hclk_switch", CLK_REF_SYNC_D4, 0x980, 0, 0x981, 0, 0, 0, 0),
  262. D_MODULE(HCLK_SWITCH_RG, "hclk_switch_rg", CLK_REF_SYNC_D4, 0xc40, 0xc41, 0xc42, 0, 0, 0, 0),
  263. D_MODULE(HCLK_UART0, "hclk_uart0", CLK_REF_SYNC_D8, 0x1a0, 0x1a1, 0x1a2, 0, 0, 0, 0),
  264. D_MODULE(HCLK_UART1, "hclk_uart1", CLK_REF_SYNC_D8, 0x1a3, 0x1a4, 0x1a5, 0, 0, 0, 0),
  265. D_MODULE(HCLK_UART2, "hclk_uart2", CLK_REF_SYNC_D8, 0x1a6, 0x1a7, 0x1a8, 0, 0, 0, 0),
  266. D_MODULE(HCLK_UART3, "hclk_uart3", CLK_REF_SYNC_D4, 0x218, 0x219, 0x21a, 0, 0, 0, 0),
  267. D_MODULE(HCLK_UART4, "hclk_uart4", CLK_REF_SYNC_D4, 0x21b, 0x21c, 0x21d, 0, 0, 0, 0),
  268. D_MODULE(HCLK_UART5, "hclk_uart5", CLK_REF_SYNC_D4, 0x220, 0x221, 0x222, 0, 0, 0, 0),
  269. D_MODULE(HCLK_UART6, "hclk_uart6", CLK_REF_SYNC_D4, 0x223, 0x224, 0x225, 0, 0, 0, 0),
  270. D_MODULE(HCLK_UART7, "hclk_uart7", CLK_REF_SYNC_D4, 0x226, 0x227, 0x228, 0, 0, 0, 0),
  271. /*
  272. * These are not hardware clocks, but are needed to handle the special
  273. * case where we have a 'selector bit' that doesn't just change the
  274. * parent for a clock, but also the gate it's supposed to use.
  275. */
  276. {
  277. .index = R9A06G032_UART_GROUP_012,
  278. .name = "uart_group_012",
  279. .type = K_BITSEL,
  280. .source = 1 + R9A06G032_DIV_UART,
  281. /* R9A06G032_SYSCTRL_REG_PWRCTRL_PG0_0 */
  282. .dual.sel = ((0x34 / 4) << 5) | 30,
  283. .dual.group = 0,
  284. },
  285. {
  286. .index = R9A06G032_UART_GROUP_34567,
  287. .name = "uart_group_34567",
  288. .type = K_BITSEL,
  289. .source = 1 + R9A06G032_DIV_P2_PG,
  290. /* R9A06G032_SYSCTRL_REG_PWRCTRL_PG1_PR2 */
  291. .dual.sel = ((0xec / 4) << 5) | 24,
  292. .dual.group = 1,
  293. },
  294. D_UGATE(CLK_UART0, "clk_uart0", UART_GROUP_012, 0, 0x1b2, 0x1b3, 0x1b4, 0x1b5),
  295. D_UGATE(CLK_UART1, "clk_uart1", UART_GROUP_012, 0, 0x1b6, 0x1b7, 0x1b8, 0x1b9),
  296. D_UGATE(CLK_UART2, "clk_uart2", UART_GROUP_012, 0, 0x1ba, 0x1bb, 0x1bc, 0x1bd),
  297. D_UGATE(CLK_UART3, "clk_uart3", UART_GROUP_34567, 1, 0x760, 0x761, 0x762, 0x763),
  298. D_UGATE(CLK_UART4, "clk_uart4", UART_GROUP_34567, 1, 0x764, 0x765, 0x766, 0x767),
  299. D_UGATE(CLK_UART5, "clk_uart5", UART_GROUP_34567, 1, 0x768, 0x769, 0x76a, 0x76b),
  300. D_UGATE(CLK_UART6, "clk_uart6", UART_GROUP_34567, 1, 0x76c, 0x76d, 0x76e, 0x76f),
  301. D_UGATE(CLK_UART7, "clk_uart7", UART_GROUP_34567, 1, 0x770, 0x771, 0x772, 0x773),
  302. };
  303. struct r9a06g032_priv {
  304. struct clk_onecell_data data;
  305. spinlock_t lock; /* protects concurrent access to gates */
  306. void __iomem *reg;
  307. };
  308. static struct r9a06g032_priv *sysctrl_priv;
  309. /* Exported helper to access the DMAMUX register */
  310. int r9a06g032_sysctrl_set_dmamux(u32 mask, u32 val)
  311. {
  312. unsigned long flags;
  313. u32 dmamux;
  314. if (!sysctrl_priv)
  315. return -EPROBE_DEFER;
  316. spin_lock_irqsave(&sysctrl_priv->lock, flags);
  317. dmamux = readl(sysctrl_priv->reg + R9A06G032_SYSCTRL_DMAMUX);
  318. dmamux &= ~mask;
  319. dmamux |= val & mask;
  320. writel(dmamux, sysctrl_priv->reg + R9A06G032_SYSCTRL_DMAMUX);
  321. spin_unlock_irqrestore(&sysctrl_priv->lock, flags);
  322. return 0;
  323. }
  324. EXPORT_SYMBOL_GPL(r9a06g032_sysctrl_set_dmamux);
  325. /* register/bit pairs are encoded as an uint16_t */
  326. static void
  327. clk_rdesc_set(struct r9a06g032_priv *clocks,
  328. u16 one, unsigned int on)
  329. {
  330. u32 __iomem *reg = clocks->reg + (4 * (one >> 5));
  331. u32 val = readl(reg);
  332. val = (val & ~(1U << (one & 0x1f))) | ((!!on) << (one & 0x1f));
  333. writel(val, reg);
  334. }
  335. static int
  336. clk_rdesc_get(struct r9a06g032_priv *clocks,
  337. uint16_t one)
  338. {
  339. u32 __iomem *reg = clocks->reg + (4 * (one >> 5));
  340. u32 val = readl(reg);
  341. return !!(val & (1U << (one & 0x1f)));
  342. }
  343. /*
  344. * This implements the R9A06G032 clock gate 'driver'. We cannot use the system's
  345. * clock gate framework as the gates on the R9A06G032 have a special enabling
  346. * sequence, therefore we use this little proxy.
  347. */
  348. struct r9a06g032_clk_gate {
  349. struct clk_hw hw;
  350. struct r9a06g032_priv *clocks;
  351. u16 index;
  352. struct r9a06g032_gate gate;
  353. };
  354. #define to_r9a06g032_gate(_hw) container_of(_hw, struct r9a06g032_clk_gate, hw)
  355. static int create_add_module_clock(struct of_phandle_args *clkspec,
  356. struct device *dev)
  357. {
  358. struct clk *clk;
  359. int error;
  360. clk = of_clk_get_from_provider(clkspec);
  361. if (IS_ERR(clk))
  362. return PTR_ERR(clk);
  363. error = pm_clk_create(dev);
  364. if (error) {
  365. clk_put(clk);
  366. return error;
  367. }
  368. error = pm_clk_add_clk(dev, clk);
  369. if (error) {
  370. pm_clk_destroy(dev);
  371. clk_put(clk);
  372. }
  373. return error;
  374. }
  375. static int r9a06g032_attach_dev(struct generic_pm_domain *pd,
  376. struct device *dev)
  377. {
  378. struct device_node *np = dev->of_node;
  379. struct of_phandle_args clkspec;
  380. int i = 0;
  381. int error;
  382. int index;
  383. while (!of_parse_phandle_with_args(np, "clocks", "#clock-cells", i++,
  384. &clkspec)) {
  385. if (clkspec.np != pd->dev.of_node)
  386. continue;
  387. index = clkspec.args[0];
  388. if (index < R9A06G032_CLOCK_COUNT &&
  389. r9a06g032_clocks[index].managed) {
  390. error = create_add_module_clock(&clkspec, dev);
  391. of_node_put(clkspec.np);
  392. if (error)
  393. return error;
  394. }
  395. }
  396. return 0;
  397. }
  398. static void r9a06g032_detach_dev(struct generic_pm_domain *unused, struct device *dev)
  399. {
  400. if (!pm_clk_no_clocks(dev))
  401. pm_clk_destroy(dev);
  402. }
  403. static int r9a06g032_add_clk_domain(struct device *dev)
  404. {
  405. struct device_node *np = dev->of_node;
  406. struct generic_pm_domain *pd;
  407. pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
  408. if (!pd)
  409. return -ENOMEM;
  410. pd->name = np->name;
  411. pd->flags = GENPD_FLAG_PM_CLK | GENPD_FLAG_ALWAYS_ON |
  412. GENPD_FLAG_ACTIVE_WAKEUP;
  413. pd->attach_dev = r9a06g032_attach_dev;
  414. pd->detach_dev = r9a06g032_detach_dev;
  415. pm_genpd_init(pd, &pm_domain_always_on_gov, false);
  416. of_genpd_add_provider_simple(np, pd);
  417. return 0;
  418. }
  419. static void
  420. r9a06g032_clk_gate_set(struct r9a06g032_priv *clocks,
  421. struct r9a06g032_gate *g, int on)
  422. {
  423. unsigned long flags;
  424. WARN_ON(!g->gate);
  425. spin_lock_irqsave(&clocks->lock, flags);
  426. clk_rdesc_set(clocks, g->gate, on);
  427. /* De-assert reset */
  428. if (g->reset)
  429. clk_rdesc_set(clocks, g->reset, 1);
  430. spin_unlock_irqrestore(&clocks->lock, flags);
  431. /* Hardware manual recommends 5us delay after enabling clock & reset */
  432. udelay(5);
  433. /* If the peripheral is memory mapped (i.e. an AXI slave), there is an
  434. * associated SLVRDY bit in the System Controller that needs to be set
  435. * so that the FlexWAY bus fabric passes on the read/write requests.
  436. */
  437. if (g->ready || g->midle) {
  438. spin_lock_irqsave(&clocks->lock, flags);
  439. if (g->ready)
  440. clk_rdesc_set(clocks, g->ready, on);
  441. /* Clear 'Master Idle Request' bit */
  442. if (g->midle)
  443. clk_rdesc_set(clocks, g->midle, !on);
  444. spin_unlock_irqrestore(&clocks->lock, flags);
  445. }
  446. /* Note: We don't wait for FlexWAY Socket Connection signal */
  447. }
  448. static int r9a06g032_clk_gate_enable(struct clk_hw *hw)
  449. {
  450. struct r9a06g032_clk_gate *g = to_r9a06g032_gate(hw);
  451. r9a06g032_clk_gate_set(g->clocks, &g->gate, 1);
  452. return 0;
  453. }
  454. static void r9a06g032_clk_gate_disable(struct clk_hw *hw)
  455. {
  456. struct r9a06g032_clk_gate *g = to_r9a06g032_gate(hw);
  457. r9a06g032_clk_gate_set(g->clocks, &g->gate, 0);
  458. }
  459. static int r9a06g032_clk_gate_is_enabled(struct clk_hw *hw)
  460. {
  461. struct r9a06g032_clk_gate *g = to_r9a06g032_gate(hw);
  462. /* if clock is in reset, the gate might be on, and still not 'be' on */
  463. if (g->gate.reset && !clk_rdesc_get(g->clocks, g->gate.reset))
  464. return 0;
  465. return clk_rdesc_get(g->clocks, g->gate.gate);
  466. }
  467. static const struct clk_ops r9a06g032_clk_gate_ops = {
  468. .enable = r9a06g032_clk_gate_enable,
  469. .disable = r9a06g032_clk_gate_disable,
  470. .is_enabled = r9a06g032_clk_gate_is_enabled,
  471. };
  472. static struct clk *
  473. r9a06g032_register_gate(struct r9a06g032_priv *clocks,
  474. const char *parent_name,
  475. const struct r9a06g032_clkdesc *desc)
  476. {
  477. struct clk *clk;
  478. struct r9a06g032_clk_gate *g;
  479. struct clk_init_data init = {};
  480. g = kzalloc(sizeof(*g), GFP_KERNEL);
  481. if (!g)
  482. return NULL;
  483. init.name = desc->name;
  484. init.ops = &r9a06g032_clk_gate_ops;
  485. init.flags = CLK_SET_RATE_PARENT;
  486. init.parent_names = parent_name ? &parent_name : NULL;
  487. init.num_parents = parent_name ? 1 : 0;
  488. g->clocks = clocks;
  489. g->index = desc->index;
  490. g->gate = desc->gate;
  491. g->hw.init = &init;
  492. /*
  493. * important here, some clocks are already in use by the CM3, we
  494. * have to assume they are not Linux's to play with and try to disable
  495. * at the end of the boot!
  496. */
  497. if (r9a06g032_clk_gate_is_enabled(&g->hw)) {
  498. init.flags |= CLK_IS_CRITICAL;
  499. pr_debug("%s was enabled, making read-only\n", desc->name);
  500. }
  501. clk = clk_register(NULL, &g->hw);
  502. if (IS_ERR(clk)) {
  503. kfree(g);
  504. return NULL;
  505. }
  506. return clk;
  507. }
  508. struct r9a06g032_clk_div {
  509. struct clk_hw hw;
  510. struct r9a06g032_priv *clocks;
  511. u16 index;
  512. u16 reg;
  513. u16 min, max;
  514. u8 table_size;
  515. u16 table[8]; /* we know there are no more than 8 */
  516. };
  517. #define to_r9a06g032_div(_hw) \
  518. container_of(_hw, struct r9a06g032_clk_div, hw)
  519. static unsigned long
  520. r9a06g032_div_recalc_rate(struct clk_hw *hw,
  521. unsigned long parent_rate)
  522. {
  523. struct r9a06g032_clk_div *clk = to_r9a06g032_div(hw);
  524. u32 __iomem *reg = clk->clocks->reg + (4 * clk->reg);
  525. u32 div = readl(reg);
  526. if (div < clk->min)
  527. div = clk->min;
  528. else if (div > clk->max)
  529. div = clk->max;
  530. return DIV_ROUND_UP(parent_rate, div);
  531. }
  532. /*
  533. * Attempts to find a value that is in range of min,max,
  534. * and if a table of set dividers was specified for this
  535. * register, try to find the fixed divider that is the closest
  536. * to the target frequency
  537. */
  538. static long
  539. r9a06g032_div_clamp_div(struct r9a06g032_clk_div *clk,
  540. unsigned long rate, unsigned long prate)
  541. {
  542. /* + 1 to cope with rates that have the remainder dropped */
  543. u32 div = DIV_ROUND_UP(prate, rate + 1);
  544. int i;
  545. if (div <= clk->min)
  546. return clk->min;
  547. if (div >= clk->max)
  548. return clk->max;
  549. for (i = 0; clk->table_size && i < clk->table_size - 1; i++) {
  550. if (div >= clk->table[i] && div <= clk->table[i + 1]) {
  551. unsigned long m = rate -
  552. DIV_ROUND_UP(prate, clk->table[i]);
  553. unsigned long p =
  554. DIV_ROUND_UP(prate, clk->table[i + 1]) -
  555. rate;
  556. /*
  557. * select the divider that generates
  558. * the value closest to the ideal frequency
  559. */
  560. div = p >= m ? clk->table[i] : clk->table[i + 1];
  561. return div;
  562. }
  563. }
  564. return div;
  565. }
  566. static int
  567. r9a06g032_div_determine_rate(struct clk_hw *hw, struct clk_rate_request *req)
  568. {
  569. struct r9a06g032_clk_div *clk = to_r9a06g032_div(hw);
  570. u32 div = DIV_ROUND_UP(req->best_parent_rate, req->rate);
  571. pr_devel("%s %pC %ld (prate %ld) (wanted div %u)\n", __func__,
  572. hw->clk, req->rate, req->best_parent_rate, div);
  573. pr_devel(" min %d (%ld) max %d (%ld)\n",
  574. clk->min, DIV_ROUND_UP(req->best_parent_rate, clk->min),
  575. clk->max, DIV_ROUND_UP(req->best_parent_rate, clk->max));
  576. div = r9a06g032_div_clamp_div(clk, req->rate, req->best_parent_rate);
  577. /*
  578. * this is a hack. Currently the serial driver asks for a clock rate
  579. * that is 16 times the baud rate -- and that is wildly outside the
  580. * range of the UART divider, somehow there is no provision for that
  581. * case of 'let the divider as is if outside range'.
  582. * The serial driver *shouldn't* play with these clocks anyway, there's
  583. * several uarts attached to this divider, and changing this impacts
  584. * everyone.
  585. */
  586. if (clk->index == R9A06G032_DIV_UART ||
  587. clk->index == R9A06G032_DIV_P2_PG) {
  588. pr_devel("%s div uart hack!\n", __func__);
  589. req->rate = clk_get_rate(hw->clk);
  590. return 0;
  591. }
  592. req->rate = DIV_ROUND_UP(req->best_parent_rate, div);
  593. pr_devel("%s %pC %ld / %u = %ld\n", __func__, hw->clk,
  594. req->best_parent_rate, div, req->rate);
  595. return 0;
  596. }
  597. static int
  598. r9a06g032_div_set_rate(struct clk_hw *hw,
  599. unsigned long rate, unsigned long parent_rate)
  600. {
  601. struct r9a06g032_clk_div *clk = to_r9a06g032_div(hw);
  602. /* + 1 to cope with rates that have the remainder dropped */
  603. u32 div = DIV_ROUND_UP(parent_rate, rate + 1);
  604. u32 __iomem *reg = clk->clocks->reg + (4 * clk->reg);
  605. pr_devel("%s %pC rate %ld parent %ld div %d\n", __func__, hw->clk,
  606. rate, parent_rate, div);
  607. /*
  608. * Need to write the bit 31 with the divider value to
  609. * latch it. Technically we should wait until it has been
  610. * cleared too.
  611. * TODO: Find whether this callback is sleepable, in case
  612. * the hardware /does/ require some sort of spinloop here.
  613. */
  614. writel(div | BIT(31), reg);
  615. return 0;
  616. }
  617. static const struct clk_ops r9a06g032_clk_div_ops = {
  618. .recalc_rate = r9a06g032_div_recalc_rate,
  619. .determine_rate = r9a06g032_div_determine_rate,
  620. .set_rate = r9a06g032_div_set_rate,
  621. };
  622. static struct clk *
  623. r9a06g032_register_div(struct r9a06g032_priv *clocks,
  624. const char *parent_name,
  625. const struct r9a06g032_clkdesc *desc)
  626. {
  627. struct r9a06g032_clk_div *div;
  628. struct clk *clk;
  629. struct clk_init_data init = {};
  630. unsigned int i;
  631. div = kzalloc(sizeof(*div), GFP_KERNEL);
  632. if (!div)
  633. return NULL;
  634. init.name = desc->name;
  635. init.ops = &r9a06g032_clk_div_ops;
  636. init.flags = CLK_SET_RATE_PARENT;
  637. init.parent_names = parent_name ? &parent_name : NULL;
  638. init.num_parents = parent_name ? 1 : 0;
  639. div->clocks = clocks;
  640. div->index = desc->index;
  641. div->reg = desc->reg;
  642. div->hw.init = &init;
  643. div->min = desc->div_min;
  644. div->max = desc->div_max;
  645. /* populate (optional) divider table fixed values */
  646. for (i = 0; i < ARRAY_SIZE(div->table) &&
  647. i < ARRAY_SIZE(desc->div_table) && desc->div_table[i]; i++) {
  648. div->table[div->table_size++] = desc->div_table[i];
  649. }
  650. clk = clk_register(NULL, &div->hw);
  651. if (IS_ERR(clk)) {
  652. kfree(div);
  653. return NULL;
  654. }
  655. return clk;
  656. }
  657. /*
  658. * This clock provider handles the case of the R9A06G032 where you have
  659. * peripherals that have two potential clock source and two gates, one for
  660. * each of the clock source - the used clock source (for all sub clocks)
  661. * is selected by a single bit.
  662. * That single bit affects all sub-clocks, and therefore needs to change the
  663. * active gate (and turn the others off) and force a recalculation of the rates.
  664. *
  665. * This implements two clock providers, one 'bitselect' that
  666. * handles the switch between both parents, and another 'dualgate'
  667. * that knows which gate to poke at, depending on the parent's bit position.
  668. */
  669. struct r9a06g032_clk_bitsel {
  670. struct clk_hw hw;
  671. struct r9a06g032_priv *clocks;
  672. u16 index;
  673. u16 selector; /* selector register + bit */
  674. };
  675. #define to_clk_bitselect(_hw) \
  676. container_of(_hw, struct r9a06g032_clk_bitsel, hw)
  677. static u8 r9a06g032_clk_mux_get_parent(struct clk_hw *hw)
  678. {
  679. struct r9a06g032_clk_bitsel *set = to_clk_bitselect(hw);
  680. return clk_rdesc_get(set->clocks, set->selector);
  681. }
  682. static int r9a06g032_clk_mux_set_parent(struct clk_hw *hw, u8 index)
  683. {
  684. struct r9a06g032_clk_bitsel *set = to_clk_bitselect(hw);
  685. /* a single bit in the register selects one of two parent clocks */
  686. clk_rdesc_set(set->clocks, set->selector, !!index);
  687. return 0;
  688. }
  689. static const struct clk_ops clk_bitselect_ops = {
  690. .get_parent = r9a06g032_clk_mux_get_parent,
  691. .set_parent = r9a06g032_clk_mux_set_parent,
  692. };
  693. static struct clk *
  694. r9a06g032_register_bitsel(struct r9a06g032_priv *clocks,
  695. const char *parent_name,
  696. const struct r9a06g032_clkdesc *desc)
  697. {
  698. struct clk *clk;
  699. struct r9a06g032_clk_bitsel *g;
  700. struct clk_init_data init = {};
  701. const char *names[2];
  702. /* allocate the gate */
  703. g = kzalloc(sizeof(*g), GFP_KERNEL);
  704. if (!g)
  705. return NULL;
  706. names[0] = parent_name;
  707. names[1] = "clk_pll_usb";
  708. init.name = desc->name;
  709. init.ops = &clk_bitselect_ops;
  710. init.flags = CLK_SET_RATE_PARENT;
  711. init.parent_names = names;
  712. init.num_parents = 2;
  713. g->clocks = clocks;
  714. g->index = desc->index;
  715. g->selector = desc->dual.sel;
  716. g->hw.init = &init;
  717. clk = clk_register(NULL, &g->hw);
  718. if (IS_ERR(clk)) {
  719. kfree(g);
  720. return NULL;
  721. }
  722. return clk;
  723. }
  724. struct r9a06g032_clk_dualgate {
  725. struct clk_hw hw;
  726. struct r9a06g032_priv *clocks;
  727. u16 index;
  728. u16 selector; /* selector register + bit */
  729. struct r9a06g032_gate gate[2];
  730. };
  731. #define to_clk_dualgate(_hw) \
  732. container_of(_hw, struct r9a06g032_clk_dualgate, hw)
  733. static int
  734. r9a06g032_clk_dualgate_setenable(struct r9a06g032_clk_dualgate *g, int enable)
  735. {
  736. u8 sel_bit = clk_rdesc_get(g->clocks, g->selector);
  737. /* we always turn off the 'other' gate, regardless */
  738. r9a06g032_clk_gate_set(g->clocks, &g->gate[!sel_bit], 0);
  739. r9a06g032_clk_gate_set(g->clocks, &g->gate[sel_bit], enable);
  740. return 0;
  741. }
  742. static int r9a06g032_clk_dualgate_enable(struct clk_hw *hw)
  743. {
  744. struct r9a06g032_clk_dualgate *gate = to_clk_dualgate(hw);
  745. r9a06g032_clk_dualgate_setenable(gate, 1);
  746. return 0;
  747. }
  748. static void r9a06g032_clk_dualgate_disable(struct clk_hw *hw)
  749. {
  750. struct r9a06g032_clk_dualgate *gate = to_clk_dualgate(hw);
  751. r9a06g032_clk_dualgate_setenable(gate, 0);
  752. }
  753. static int r9a06g032_clk_dualgate_is_enabled(struct clk_hw *hw)
  754. {
  755. struct r9a06g032_clk_dualgate *g = to_clk_dualgate(hw);
  756. u8 sel_bit = clk_rdesc_get(g->clocks, g->selector);
  757. return clk_rdesc_get(g->clocks, g->gate[sel_bit].gate);
  758. }
  759. static const struct clk_ops r9a06g032_clk_dualgate_ops = {
  760. .enable = r9a06g032_clk_dualgate_enable,
  761. .disable = r9a06g032_clk_dualgate_disable,
  762. .is_enabled = r9a06g032_clk_dualgate_is_enabled,
  763. };
  764. static struct clk *
  765. r9a06g032_register_dualgate(struct r9a06g032_priv *clocks,
  766. const char *parent_name,
  767. const struct r9a06g032_clkdesc *desc,
  768. uint16_t sel)
  769. {
  770. struct r9a06g032_clk_dualgate *g;
  771. struct clk *clk;
  772. struct clk_init_data init = {};
  773. /* allocate the gate */
  774. g = kzalloc(sizeof(*g), GFP_KERNEL);
  775. if (!g)
  776. return NULL;
  777. g->clocks = clocks;
  778. g->index = desc->index;
  779. g->selector = sel;
  780. g->gate[0].gate = desc->dual.g1;
  781. g->gate[0].reset = desc->dual.r1;
  782. g->gate[1].gate = desc->dual.g2;
  783. g->gate[1].reset = desc->dual.r2;
  784. init.name = desc->name;
  785. init.ops = &r9a06g032_clk_dualgate_ops;
  786. init.flags = CLK_SET_RATE_PARENT;
  787. init.parent_names = &parent_name;
  788. init.num_parents = 1;
  789. g->hw.init = &init;
  790. /*
  791. * important here, some clocks are already in use by the CM3, we
  792. * have to assume they are not Linux's to play with and try to disable
  793. * at the end of the boot!
  794. */
  795. if (r9a06g032_clk_dualgate_is_enabled(&g->hw)) {
  796. init.flags |= CLK_IS_CRITICAL;
  797. pr_debug("%s was enabled, making read-only\n", desc->name);
  798. }
  799. clk = clk_register(NULL, &g->hw);
  800. if (IS_ERR(clk)) {
  801. kfree(g);
  802. return NULL;
  803. }
  804. return clk;
  805. }
  806. static void r9a06g032_clocks_del_clk_provider(void *data)
  807. {
  808. of_clk_del_provider(data);
  809. }
  810. static int __init r9a06g032_clocks_probe(struct platform_device *pdev)
  811. {
  812. struct device *dev = &pdev->dev;
  813. struct device_node *np = dev->of_node;
  814. struct r9a06g032_priv *clocks;
  815. struct clk **clks;
  816. struct clk *mclk;
  817. unsigned int i;
  818. u16 uart_group_sel[2];
  819. int error;
  820. clocks = devm_kzalloc(dev, sizeof(*clocks), GFP_KERNEL);
  821. clks = devm_kcalloc(dev, R9A06G032_CLOCK_COUNT, sizeof(struct clk *),
  822. GFP_KERNEL);
  823. if (!clocks || !clks)
  824. return -ENOMEM;
  825. spin_lock_init(&clocks->lock);
  826. clocks->data.clks = clks;
  827. clocks->data.clk_num = R9A06G032_CLOCK_COUNT;
  828. mclk = devm_clk_get(dev, "mclk");
  829. if (IS_ERR(mclk))
  830. return PTR_ERR(mclk);
  831. clocks->reg = of_iomap(np, 0);
  832. if (WARN_ON(!clocks->reg))
  833. return -ENOMEM;
  834. for (i = 0; i < ARRAY_SIZE(r9a06g032_clocks); ++i) {
  835. const struct r9a06g032_clkdesc *d = &r9a06g032_clocks[i];
  836. const char *parent_name = d->source ?
  837. __clk_get_name(clocks->data.clks[d->source - 1]) :
  838. __clk_get_name(mclk);
  839. struct clk *clk = NULL;
  840. switch (d->type) {
  841. case K_FFC:
  842. clk = clk_register_fixed_factor(NULL, d->name,
  843. parent_name, 0,
  844. d->mul, d->div);
  845. break;
  846. case K_GATE:
  847. clk = r9a06g032_register_gate(clocks, parent_name, d);
  848. break;
  849. case K_DIV:
  850. clk = r9a06g032_register_div(clocks, parent_name, d);
  851. break;
  852. case K_BITSEL:
  853. /* keep that selector register around */
  854. uart_group_sel[d->dual.group] = d->dual.sel;
  855. clk = r9a06g032_register_bitsel(clocks, parent_name, d);
  856. break;
  857. case K_DUALGATE:
  858. clk = r9a06g032_register_dualgate(clocks, parent_name,
  859. d,
  860. uart_group_sel[d->dual.group]);
  861. break;
  862. }
  863. clocks->data.clks[d->index] = clk;
  864. }
  865. error = of_clk_add_provider(np, of_clk_src_onecell_get, &clocks->data);
  866. if (error)
  867. return error;
  868. error = devm_add_action_or_reset(dev,
  869. r9a06g032_clocks_del_clk_provider, np);
  870. if (error)
  871. return error;
  872. error = r9a06g032_add_clk_domain(dev);
  873. if (error)
  874. return error;
  875. sysctrl_priv = clocks;
  876. error = of_platform_populate(np, NULL, NULL, dev);
  877. if (error)
  878. dev_err(dev, "Failed to populate children (%d)\n", error);
  879. return 0;
  880. }
  881. static const struct of_device_id r9a06g032_match[] = {
  882. { .compatible = "renesas,r9a06g032-sysctrl" },
  883. { }
  884. };
  885. static struct platform_driver r9a06g032_clock_driver = {
  886. .driver = {
  887. .name = "renesas,r9a06g032-sysctrl",
  888. .of_match_table = r9a06g032_match,
  889. },
  890. };
  891. static int __init r9a06g032_clocks_init(void)
  892. {
  893. return platform_driver_probe(&r9a06g032_clock_driver,
  894. r9a06g032_clocks_probe);
  895. }
  896. subsys_initcall(r9a06g032_clocks_init);