clk-en7523.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <linux/delay.h>
  3. #include <linux/clk-provider.h>
  4. #include <linux/io.h>
  5. #include <linux/mfd/syscon.h>
  6. #include <linux/platform_device.h>
  7. #include <linux/property.h>
  8. #include <linux/regmap.h>
  9. #include <linux/reset-controller.h>
  10. #include <dt-bindings/clock/en7523-clk.h>
  11. #include <dt-bindings/reset/airoha,en7523-reset.h>
  12. #include <dt-bindings/reset/airoha,en7581-reset.h>
  13. #define RST_NR_PER_BANK 32
  14. #define REG_PCI_CONTROL 0x88
  15. #define REG_PCI_CONTROL_PERSTOUT BIT(29)
  16. #define REG_PCI_CONTROL_PERSTOUT1 BIT(26)
  17. #define REG_PCI_CONTROL_REFCLK_EN0 BIT(23)
  18. #define REG_PCI_CONTROL_REFCLK_EN1 BIT(22)
  19. #define REG_PCI_CONTROL_PERSTOUT2 BIT(16)
  20. #define REG_GSW_CLK_DIV_SEL 0x1b4
  21. #define REG_EMI_CLK_DIV_SEL 0x1b8
  22. #define REG_BUS_CLK_DIV_SEL 0x1bc
  23. #define REG_SPI_CLK_DIV_SEL 0x1c4
  24. #define REG_SPI_CLK_FREQ_SEL 0x1c8
  25. #define REG_NPU_CLK_DIV_SEL 0x1fc
  26. #define REG_CRYPTO_CLKSRC 0x200
  27. #define REG_RESET_CONTROL2 0x830
  28. #define REG_RESET2_CONTROL_PCIE2 BIT(27)
  29. #define REG_RESET_CONTROL1 0x834
  30. #define REG_RESET_CONTROL_PCIEHB BIT(29)
  31. #define REG_RESET_CONTROL_PCIE1 BIT(27)
  32. #define REG_RESET_CONTROL_PCIE2 BIT(26)
  33. /* EN7581 */
  34. #define REG_NP_SCU_PCIC 0x88
  35. #define REG_NP_SCU_SSTR 0x9c
  36. #define REG_PCIE_XSI0_SEL_MASK GENMASK(14, 13)
  37. #define REG_PCIE_XSI1_SEL_MASK GENMASK(12, 11)
  38. #define REG_CRYPTO_CLKSRC2 0x20c
  39. #define REG_RST_CTRL2 0x830
  40. #define REG_RST_CTRL1 0x834
  41. struct en_clk_desc {
  42. int id;
  43. const char *name;
  44. u32 base_reg;
  45. u8 base_bits;
  46. u8 base_shift;
  47. union {
  48. const unsigned int *base_values;
  49. unsigned int base_value;
  50. };
  51. size_t n_base_values;
  52. u16 div_reg;
  53. u8 div_bits;
  54. u8 div_shift;
  55. u16 div_val0;
  56. u8 div_step;
  57. u8 div_offset;
  58. };
  59. struct en_clk_gate {
  60. void __iomem *base;
  61. struct clk_hw hw;
  62. };
  63. struct en_rst_data {
  64. const u16 *bank_ofs;
  65. const u16 *idx_map;
  66. void __iomem *base;
  67. struct reset_controller_dev rcdev;
  68. };
  69. struct en_clk_soc_data {
  70. u32 num_clocks;
  71. const struct clk_ops pcie_ops;
  72. int (*hw_init)(struct platform_device *pdev,
  73. struct clk_hw_onecell_data *clk_data);
  74. };
  75. static const u32 gsw_base[] = { 400000000, 500000000 };
  76. static const u32 emi_base[] = { 333000000, 400000000 };
  77. static const u32 bus_base[] = { 500000000, 540000000 };
  78. static const u32 slic_base[] = { 100000000, 3125000 };
  79. static const u32 npu_base[] = { 333000000, 400000000, 500000000 };
  80. /* EN7581 */
  81. static const u32 emi7581_base[] = { 540000000, 480000000, 400000000, 300000000 };
  82. static const u32 bus7581_base[] = { 600000000, 540000000 };
  83. static const u32 npu7581_base[] = { 800000000, 750000000, 720000000, 600000000 };
  84. static const u32 crypto_base[] = { 540000000, 480000000 };
  85. static const u32 emmc7581_base[] = { 200000000, 150000000 };
  86. static const struct en_clk_desc en7523_base_clks[] = {
  87. {
  88. .id = EN7523_CLK_GSW,
  89. .name = "gsw",
  90. .base_reg = REG_GSW_CLK_DIV_SEL,
  91. .base_bits = 1,
  92. .base_shift = 8,
  93. .base_values = gsw_base,
  94. .n_base_values = ARRAY_SIZE(gsw_base),
  95. .div_bits = 3,
  96. .div_shift = 0,
  97. .div_step = 1,
  98. .div_offset = 1,
  99. }, {
  100. .id = EN7523_CLK_EMI,
  101. .name = "emi",
  102. .base_reg = REG_EMI_CLK_DIV_SEL,
  103. .base_bits = 1,
  104. .base_shift = 8,
  105. .base_values = emi_base,
  106. .n_base_values = ARRAY_SIZE(emi_base),
  107. .div_bits = 3,
  108. .div_shift = 0,
  109. .div_step = 1,
  110. .div_offset = 1,
  111. }, {
  112. .id = EN7523_CLK_BUS,
  113. .name = "bus",
  114. .base_reg = REG_BUS_CLK_DIV_SEL,
  115. .base_bits = 1,
  116. .base_shift = 8,
  117. .base_values = bus_base,
  118. .n_base_values = ARRAY_SIZE(bus_base),
  119. .div_bits = 3,
  120. .div_shift = 0,
  121. .div_step = 1,
  122. .div_offset = 1,
  123. }, {
  124. .id = EN7523_CLK_SLIC,
  125. .name = "slic",
  126. .base_reg = REG_SPI_CLK_FREQ_SEL,
  127. .base_bits = 1,
  128. .base_shift = 0,
  129. .base_values = slic_base,
  130. .n_base_values = ARRAY_SIZE(slic_base),
  131. .div_reg = REG_SPI_CLK_DIV_SEL,
  132. .div_bits = 5,
  133. .div_shift = 24,
  134. .div_val0 = 20,
  135. .div_step = 2,
  136. }, {
  137. .id = EN7523_CLK_SPI,
  138. .name = "spi",
  139. .base_reg = REG_SPI_CLK_DIV_SEL,
  140. .base_value = 400000000,
  141. .div_bits = 5,
  142. .div_shift = 8,
  143. .div_val0 = 40,
  144. .div_step = 2,
  145. }, {
  146. .id = EN7523_CLK_NPU,
  147. .name = "npu",
  148. .base_reg = REG_NPU_CLK_DIV_SEL,
  149. .base_bits = 2,
  150. .base_shift = 8,
  151. .base_values = npu_base,
  152. .n_base_values = ARRAY_SIZE(npu_base),
  153. .div_bits = 3,
  154. .div_shift = 0,
  155. .div_step = 1,
  156. .div_offset = 1,
  157. }, {
  158. .id = EN7523_CLK_CRYPTO,
  159. .name = "crypto",
  160. .base_reg = REG_CRYPTO_CLKSRC,
  161. .base_bits = 1,
  162. .base_shift = 0,
  163. .base_values = emi_base,
  164. .n_base_values = ARRAY_SIZE(emi_base),
  165. }
  166. };
  167. static const struct en_clk_desc en7581_base_clks[] = {
  168. {
  169. .id = EN7523_CLK_GSW,
  170. .name = "gsw",
  171. .base_reg = REG_GSW_CLK_DIV_SEL,
  172. .base_bits = 1,
  173. .base_shift = 8,
  174. .base_values = gsw_base,
  175. .n_base_values = ARRAY_SIZE(gsw_base),
  176. .div_bits = 3,
  177. .div_shift = 0,
  178. .div_step = 1,
  179. .div_offset = 1,
  180. }, {
  181. .id = EN7523_CLK_EMI,
  182. .name = "emi",
  183. .base_reg = REG_EMI_CLK_DIV_SEL,
  184. .base_bits = 2,
  185. .base_shift = 8,
  186. .base_values = emi7581_base,
  187. .n_base_values = ARRAY_SIZE(emi7581_base),
  188. .div_bits = 3,
  189. .div_shift = 0,
  190. .div_step = 1,
  191. .div_offset = 1,
  192. }, {
  193. .id = EN7523_CLK_BUS,
  194. .name = "bus",
  195. .base_reg = REG_BUS_CLK_DIV_SEL,
  196. .base_bits = 1,
  197. .base_shift = 8,
  198. .base_values = bus7581_base,
  199. .n_base_values = ARRAY_SIZE(bus7581_base),
  200. .div_bits = 3,
  201. .div_shift = 0,
  202. .div_step = 1,
  203. .div_offset = 1,
  204. }, {
  205. .id = EN7523_CLK_SLIC,
  206. .name = "slic",
  207. .base_reg = REG_SPI_CLK_FREQ_SEL,
  208. .base_bits = 1,
  209. .base_shift = 0,
  210. .base_values = slic_base,
  211. .n_base_values = ARRAY_SIZE(slic_base),
  212. .div_reg = REG_SPI_CLK_DIV_SEL,
  213. .div_bits = 5,
  214. .div_shift = 24,
  215. .div_val0 = 20,
  216. .div_step = 2,
  217. }, {
  218. .id = EN7523_CLK_SPI,
  219. .name = "spi",
  220. .base_reg = REG_SPI_CLK_DIV_SEL,
  221. .base_value = 400000000,
  222. .div_bits = 5,
  223. .div_shift = 8,
  224. .div_val0 = 40,
  225. .div_step = 2,
  226. }, {
  227. .id = EN7523_CLK_NPU,
  228. .name = "npu",
  229. .base_reg = REG_NPU_CLK_DIV_SEL,
  230. .base_bits = 2,
  231. .base_shift = 8,
  232. .base_values = npu7581_base,
  233. .n_base_values = ARRAY_SIZE(npu7581_base),
  234. .div_bits = 3,
  235. .div_shift = 0,
  236. .div_step = 1,
  237. .div_offset = 1,
  238. }, {
  239. .id = EN7523_CLK_CRYPTO,
  240. .name = "crypto",
  241. .base_reg = REG_CRYPTO_CLKSRC2,
  242. .base_bits = 1,
  243. .base_shift = 0,
  244. .base_values = crypto_base,
  245. .n_base_values = ARRAY_SIZE(crypto_base),
  246. }, {
  247. .id = EN7581_CLK_EMMC,
  248. .name = "emmc",
  249. .base_reg = REG_CRYPTO_CLKSRC2,
  250. .base_bits = 1,
  251. .base_shift = 12,
  252. .base_values = emmc7581_base,
  253. .n_base_values = ARRAY_SIZE(emmc7581_base),
  254. }
  255. };
  256. static const u16 en7581_rst_ofs[] = {
  257. REG_RST_CTRL2,
  258. REG_RST_CTRL1,
  259. };
  260. static const u16 en7523_rst_map[] = {
  261. /* RST_CTRL2 */
  262. [EN7523_XPON_PHY_RST] = 0,
  263. [EN7523_XSI_MAC_RST] = 7,
  264. [EN7523_XSI_PHY_RST] = 8,
  265. [EN7523_NPU_RST] = 9,
  266. [EN7523_I2S_RST] = 10,
  267. [EN7523_TRNG_RST] = 11,
  268. [EN7523_TRNG_MSTART_RST] = 12,
  269. [EN7523_DUAL_HSI0_RST] = 13,
  270. [EN7523_DUAL_HSI1_RST] = 14,
  271. [EN7523_HSI_RST] = 15,
  272. [EN7523_DUAL_HSI0_MAC_RST] = 16,
  273. [EN7523_DUAL_HSI1_MAC_RST] = 17,
  274. [EN7523_HSI_MAC_RST] = 18,
  275. [EN7523_WDMA_RST] = 19,
  276. [EN7523_WOE0_RST] = 20,
  277. [EN7523_WOE1_RST] = 21,
  278. [EN7523_HSDMA_RST] = 22,
  279. [EN7523_I2C2RBUS_RST] = 23,
  280. [EN7523_TDMA_RST] = 24,
  281. /* RST_CTRL1 */
  282. [EN7523_PCM1_ZSI_ISI_RST] = RST_NR_PER_BANK + 0,
  283. [EN7523_FE_PDMA_RST] = RST_NR_PER_BANK + 1,
  284. [EN7523_FE_QDMA_RST] = RST_NR_PER_BANK + 2,
  285. [EN7523_PCM_SPIWP_RST] = RST_NR_PER_BANK + 4,
  286. [EN7523_CRYPTO_RST] = RST_NR_PER_BANK + 6,
  287. [EN7523_TIMER_RST] = RST_NR_PER_BANK + 8,
  288. [EN7523_PCM1_RST] = RST_NR_PER_BANK + 11,
  289. [EN7523_UART_RST] = RST_NR_PER_BANK + 12,
  290. [EN7523_GPIO_RST] = RST_NR_PER_BANK + 13,
  291. [EN7523_GDMA_RST] = RST_NR_PER_BANK + 14,
  292. [EN7523_I2C_MASTER_RST] = RST_NR_PER_BANK + 16,
  293. [EN7523_PCM2_ZSI_ISI_RST] = RST_NR_PER_BANK + 17,
  294. [EN7523_SFC_RST] = RST_NR_PER_BANK + 18,
  295. [EN7523_UART2_RST] = RST_NR_PER_BANK + 19,
  296. [EN7523_GDMP_RST] = RST_NR_PER_BANK + 20,
  297. [EN7523_FE_RST] = RST_NR_PER_BANK + 21,
  298. [EN7523_USB_HOST_P0_RST] = RST_NR_PER_BANK + 22,
  299. [EN7523_GSW_RST] = RST_NR_PER_BANK + 23,
  300. [EN7523_SFC2_PCM_RST] = RST_NR_PER_BANK + 25,
  301. [EN7523_PCIE0_RST] = RST_NR_PER_BANK + 26,
  302. [EN7523_PCIE1_RST] = RST_NR_PER_BANK + 27,
  303. [EN7523_PCIE_HB_RST] = RST_NR_PER_BANK + 29,
  304. [EN7523_XPON_MAC_RST] = RST_NR_PER_BANK + 31,
  305. };
  306. static const u16 en7581_rst_map[] = {
  307. /* RST_CTRL2 */
  308. [EN7581_XPON_PHY_RST] = 0,
  309. [EN7581_CPU_TIMER2_RST] = 2,
  310. [EN7581_HSUART_RST] = 3,
  311. [EN7581_UART4_RST] = 4,
  312. [EN7581_UART5_RST] = 5,
  313. [EN7581_I2C2_RST] = 6,
  314. [EN7581_XSI_MAC_RST] = 7,
  315. [EN7581_XSI_PHY_RST] = 8,
  316. [EN7581_NPU_RST] = 9,
  317. [EN7581_I2S_RST] = 10,
  318. [EN7581_TRNG_RST] = 11,
  319. [EN7581_TRNG_MSTART_RST] = 12,
  320. [EN7581_DUAL_HSI0_RST] = 13,
  321. [EN7581_DUAL_HSI1_RST] = 14,
  322. [EN7581_HSI_RST] = 15,
  323. [EN7581_DUAL_HSI0_MAC_RST] = 16,
  324. [EN7581_DUAL_HSI1_MAC_RST] = 17,
  325. [EN7581_HSI_MAC_RST] = 18,
  326. [EN7581_WDMA_RST] = 19,
  327. [EN7581_WOE0_RST] = 20,
  328. [EN7581_WOE1_RST] = 21,
  329. [EN7581_HSDMA_RST] = 22,
  330. [EN7581_TDMA_RST] = 24,
  331. [EN7581_EMMC_RST] = 25,
  332. [EN7581_SOE_RST] = 26,
  333. [EN7581_PCIE2_RST] = 27,
  334. [EN7581_XFP_MAC_RST] = 28,
  335. [EN7581_USB_HOST_P1_RST] = 29,
  336. [EN7581_USB_HOST_P1_U3_PHY_RST] = 30,
  337. /* RST_CTRL1 */
  338. [EN7581_PCM1_ZSI_ISI_RST] = RST_NR_PER_BANK + 0,
  339. [EN7581_FE_PDMA_RST] = RST_NR_PER_BANK + 1,
  340. [EN7581_FE_QDMA_RST] = RST_NR_PER_BANK + 2,
  341. [EN7581_PCM_SPIWP_RST] = RST_NR_PER_BANK + 4,
  342. [EN7581_CRYPTO_RST] = RST_NR_PER_BANK + 6,
  343. [EN7581_TIMER_RST] = RST_NR_PER_BANK + 8,
  344. [EN7581_PCM1_RST] = RST_NR_PER_BANK + 11,
  345. [EN7581_UART_RST] = RST_NR_PER_BANK + 12,
  346. [EN7581_GPIO_RST] = RST_NR_PER_BANK + 13,
  347. [EN7581_GDMA_RST] = RST_NR_PER_BANK + 14,
  348. [EN7581_I2C_MASTER_RST] = RST_NR_PER_BANK + 16,
  349. [EN7581_PCM2_ZSI_ISI_RST] = RST_NR_PER_BANK + 17,
  350. [EN7581_SFC_RST] = RST_NR_PER_BANK + 18,
  351. [EN7581_UART2_RST] = RST_NR_PER_BANK + 19,
  352. [EN7581_GDMP_RST] = RST_NR_PER_BANK + 20,
  353. [EN7581_FE_RST] = RST_NR_PER_BANK + 21,
  354. [EN7581_USB_HOST_P0_RST] = RST_NR_PER_BANK + 22,
  355. [EN7581_GSW_RST] = RST_NR_PER_BANK + 23,
  356. [EN7581_SFC2_PCM_RST] = RST_NR_PER_BANK + 25,
  357. [EN7581_PCIE0_RST] = RST_NR_PER_BANK + 26,
  358. [EN7581_PCIE1_RST] = RST_NR_PER_BANK + 27,
  359. [EN7581_CPU_TIMER_RST] = RST_NR_PER_BANK + 28,
  360. [EN7581_PCIE_HB_RST] = RST_NR_PER_BANK + 29,
  361. [EN7581_XPON_MAC_RST] = RST_NR_PER_BANK + 31,
  362. };
  363. static int en7581_reset_register(struct device *dev, void __iomem *base,
  364. const u16 *rst_map, int nr_resets);
  365. static u32 en7523_get_base_rate(const struct en_clk_desc *desc, u32 val)
  366. {
  367. if (!desc->base_bits)
  368. return desc->base_value;
  369. val >>= desc->base_shift;
  370. val &= (1 << desc->base_bits) - 1;
  371. if (val >= desc->n_base_values)
  372. return 0;
  373. return desc->base_values[val];
  374. }
  375. static u32 en7523_get_div(const struct en_clk_desc *desc, u32 val)
  376. {
  377. if (!desc->div_bits)
  378. return 1;
  379. val >>= desc->div_shift;
  380. val &= (1 << desc->div_bits) - 1;
  381. if (!val && desc->div_val0)
  382. return desc->div_val0;
  383. return (val + desc->div_offset) * desc->div_step;
  384. }
  385. static int en7523_pci_is_enabled(struct clk_hw *hw)
  386. {
  387. struct en_clk_gate *cg = container_of(hw, struct en_clk_gate, hw);
  388. return !!(readl(cg->base + REG_PCI_CONTROL) & REG_PCI_CONTROL_REFCLK_EN1);
  389. }
  390. static int en7523_pci_prepare(struct clk_hw *hw)
  391. {
  392. struct en_clk_gate *cg = container_of(hw, struct en_clk_gate, hw);
  393. void __iomem *np_base = cg->base;
  394. u32 val, mask;
  395. /* Need to pull device low before reset */
  396. val = readl(np_base + REG_PCI_CONTROL);
  397. val &= ~(REG_PCI_CONTROL_PERSTOUT1 | REG_PCI_CONTROL_PERSTOUT);
  398. writel(val, np_base + REG_PCI_CONTROL);
  399. usleep_range(1000, 2000);
  400. /* Enable PCIe port 1 */
  401. val |= REG_PCI_CONTROL_REFCLK_EN1;
  402. writel(val, np_base + REG_PCI_CONTROL);
  403. usleep_range(1000, 2000);
  404. /* Reset to default */
  405. val = readl(np_base + REG_RESET_CONTROL1);
  406. mask = REG_RESET_CONTROL_PCIE1 | REG_RESET_CONTROL_PCIE2 |
  407. REG_RESET_CONTROL_PCIEHB;
  408. writel(val & ~mask, np_base + REG_RESET_CONTROL1);
  409. usleep_range(1000, 2000);
  410. writel(val | mask, np_base + REG_RESET_CONTROL1);
  411. msleep(100);
  412. writel(val & ~mask, np_base + REG_RESET_CONTROL1);
  413. usleep_range(5000, 10000);
  414. /* Release device */
  415. mask = REG_PCI_CONTROL_PERSTOUT1 | REG_PCI_CONTROL_PERSTOUT;
  416. val = readl(np_base + REG_PCI_CONTROL);
  417. writel(val & ~mask, np_base + REG_PCI_CONTROL);
  418. usleep_range(1000, 2000);
  419. writel(val | mask, np_base + REG_PCI_CONTROL);
  420. msleep(250);
  421. return 0;
  422. }
  423. static void en7523_pci_unprepare(struct clk_hw *hw)
  424. {
  425. struct en_clk_gate *cg = container_of(hw, struct en_clk_gate, hw);
  426. void __iomem *np_base = cg->base;
  427. u32 val;
  428. val = readl(np_base + REG_PCI_CONTROL);
  429. val &= ~REG_PCI_CONTROL_REFCLK_EN1;
  430. writel(val, np_base + REG_PCI_CONTROL);
  431. }
  432. static struct clk_hw *en7523_register_pcie_clk(struct device *dev,
  433. void __iomem *np_base)
  434. {
  435. const struct en_clk_soc_data *soc_data = device_get_match_data(dev);
  436. struct clk_init_data init = {
  437. .name = "pcie",
  438. .ops = &soc_data->pcie_ops,
  439. };
  440. struct en_clk_gate *cg;
  441. cg = devm_kzalloc(dev, sizeof(*cg), GFP_KERNEL);
  442. if (!cg)
  443. return NULL;
  444. cg->base = np_base;
  445. cg->hw.init = &init;
  446. if (init.ops->unprepare)
  447. init.ops->unprepare(&cg->hw);
  448. if (clk_hw_register(dev, &cg->hw))
  449. return NULL;
  450. return &cg->hw;
  451. }
  452. static int en7581_pci_is_enabled(struct clk_hw *hw)
  453. {
  454. struct en_clk_gate *cg = container_of(hw, struct en_clk_gate, hw);
  455. u32 val, mask;
  456. mask = REG_PCI_CONTROL_REFCLK_EN0 | REG_PCI_CONTROL_REFCLK_EN1;
  457. val = readl(cg->base + REG_PCI_CONTROL);
  458. return (val & mask) == mask;
  459. }
  460. static int en7581_pci_enable(struct clk_hw *hw)
  461. {
  462. struct en_clk_gate *cg = container_of(hw, struct en_clk_gate, hw);
  463. void __iomem *np_base = cg->base;
  464. u32 val, mask;
  465. mask = REG_PCI_CONTROL_REFCLK_EN0 | REG_PCI_CONTROL_REFCLK_EN1 |
  466. REG_PCI_CONTROL_PERSTOUT1 | REG_PCI_CONTROL_PERSTOUT2 |
  467. REG_PCI_CONTROL_PERSTOUT;
  468. val = readl(np_base + REG_PCI_CONTROL);
  469. writel(val | mask, np_base + REG_PCI_CONTROL);
  470. return 0;
  471. }
  472. static void en7581_pci_disable(struct clk_hw *hw)
  473. {
  474. struct en_clk_gate *cg = container_of(hw, struct en_clk_gate, hw);
  475. void __iomem *np_base = cg->base;
  476. u32 val, mask;
  477. mask = REG_PCI_CONTROL_REFCLK_EN0 | REG_PCI_CONTROL_REFCLK_EN1 |
  478. REG_PCI_CONTROL_PERSTOUT1 | REG_PCI_CONTROL_PERSTOUT2 |
  479. REG_PCI_CONTROL_PERSTOUT;
  480. val = readl(np_base + REG_PCI_CONTROL);
  481. writel(val & ~mask, np_base + REG_PCI_CONTROL);
  482. usleep_range(1000, 2000);
  483. }
  484. static void en7523_register_clocks(struct device *dev, struct clk_hw_onecell_data *clk_data,
  485. void __iomem *base, void __iomem *np_base)
  486. {
  487. struct clk_hw *hw;
  488. u32 rate;
  489. int i;
  490. for (i = 0; i < ARRAY_SIZE(en7523_base_clks); i++) {
  491. const struct en_clk_desc *desc = &en7523_base_clks[i];
  492. u32 reg = desc->div_reg ? desc->div_reg : desc->base_reg;
  493. u32 val = readl(base + desc->base_reg);
  494. rate = en7523_get_base_rate(desc, val);
  495. val = readl(base + reg);
  496. rate /= en7523_get_div(desc, val);
  497. hw = clk_hw_register_fixed_rate(dev, desc->name, NULL, 0, rate);
  498. if (IS_ERR(hw)) {
  499. pr_err("Failed to register clk %s: %ld\n",
  500. desc->name, PTR_ERR(hw));
  501. continue;
  502. }
  503. clk_data->hws[desc->id] = hw;
  504. }
  505. hw = en7523_register_pcie_clk(dev, np_base);
  506. clk_data->hws[EN7523_CLK_PCIE] = hw;
  507. }
  508. static int en7523_clk_hw_init(struct platform_device *pdev,
  509. struct clk_hw_onecell_data *clk_data)
  510. {
  511. void __iomem *base, *np_base;
  512. base = devm_platform_ioremap_resource(pdev, 0);
  513. if (IS_ERR(base))
  514. return PTR_ERR(base);
  515. np_base = devm_platform_ioremap_resource(pdev, 1);
  516. if (IS_ERR(np_base))
  517. return PTR_ERR(np_base);
  518. en7523_register_clocks(&pdev->dev, clk_data, base, np_base);
  519. return en7581_reset_register(&pdev->dev, np_base, en7523_rst_map,
  520. ARRAY_SIZE(en7523_rst_map));
  521. }
  522. static void en7581_register_clocks(struct device *dev, struct clk_hw_onecell_data *clk_data,
  523. struct regmap *map, void __iomem *base)
  524. {
  525. struct clk_hw *hw;
  526. u32 rate;
  527. int i;
  528. for (i = 0; i < ARRAY_SIZE(en7581_base_clks); i++) {
  529. const struct en_clk_desc *desc = &en7581_base_clks[i];
  530. u32 val, reg = desc->div_reg ? desc->div_reg : desc->base_reg;
  531. int err;
  532. err = regmap_read(map, desc->base_reg, &val);
  533. if (err) {
  534. pr_err("Failed reading fixed clk rate %s: %d\n",
  535. desc->name, err);
  536. continue;
  537. }
  538. rate = en7523_get_base_rate(desc, val);
  539. err = regmap_read(map, reg, &val);
  540. if (err) {
  541. pr_err("Failed reading fixed clk div %s: %d\n",
  542. desc->name, err);
  543. continue;
  544. }
  545. rate /= en7523_get_div(desc, val);
  546. hw = clk_hw_register_fixed_rate(dev, desc->name, NULL, 0, rate);
  547. if (IS_ERR(hw)) {
  548. pr_err("Failed to register clk %s: %ld\n",
  549. desc->name, PTR_ERR(hw));
  550. continue;
  551. }
  552. clk_data->hws[desc->id] = hw;
  553. }
  554. hw = en7523_register_pcie_clk(dev, base);
  555. clk_data->hws[EN7523_CLK_PCIE] = hw;
  556. }
  557. static int en7523_reset_update(struct reset_controller_dev *rcdev,
  558. unsigned long id, bool assert)
  559. {
  560. struct en_rst_data *rst_data = container_of(rcdev, struct en_rst_data, rcdev);
  561. void __iomem *addr = rst_data->base + rst_data->bank_ofs[id / RST_NR_PER_BANK];
  562. u32 val;
  563. val = readl(addr);
  564. if (assert)
  565. val |= BIT(id % RST_NR_PER_BANK);
  566. else
  567. val &= ~BIT(id % RST_NR_PER_BANK);
  568. writel(val, addr);
  569. return 0;
  570. }
  571. static int en7523_reset_assert(struct reset_controller_dev *rcdev,
  572. unsigned long id)
  573. {
  574. return en7523_reset_update(rcdev, id, true);
  575. }
  576. static int en7523_reset_deassert(struct reset_controller_dev *rcdev,
  577. unsigned long id)
  578. {
  579. return en7523_reset_update(rcdev, id, false);
  580. }
  581. static int en7523_reset_status(struct reset_controller_dev *rcdev,
  582. unsigned long id)
  583. {
  584. struct en_rst_data *rst_data = container_of(rcdev, struct en_rst_data, rcdev);
  585. void __iomem *addr = rst_data->base + rst_data->bank_ofs[id / RST_NR_PER_BANK];
  586. return !!(readl(addr) & BIT(id % RST_NR_PER_BANK));
  587. }
  588. static int en7523_reset_xlate(struct reset_controller_dev *rcdev,
  589. const struct of_phandle_args *reset_spec)
  590. {
  591. struct en_rst_data *rst_data = container_of(rcdev, struct en_rst_data, rcdev);
  592. if (reset_spec->args[0] >= rcdev->nr_resets)
  593. return -EINVAL;
  594. return rst_data->idx_map[reset_spec->args[0]];
  595. }
  596. static const struct reset_control_ops en7581_reset_ops = {
  597. .assert = en7523_reset_assert,
  598. .deassert = en7523_reset_deassert,
  599. .status = en7523_reset_status,
  600. };
  601. static int en7581_reset_register(struct device *dev, void __iomem *base,
  602. const u16 *rst_map, int nr_resets)
  603. {
  604. struct en_rst_data *rst_data;
  605. rst_data = devm_kzalloc(dev, sizeof(*rst_data), GFP_KERNEL);
  606. if (!rst_data)
  607. return -ENOMEM;
  608. rst_data->bank_ofs = en7581_rst_ofs;
  609. rst_data->idx_map = rst_map;
  610. rst_data->base = base;
  611. rst_data->rcdev.nr_resets = nr_resets;
  612. rst_data->rcdev.of_xlate = en7523_reset_xlate;
  613. rst_data->rcdev.ops = &en7581_reset_ops;
  614. rst_data->rcdev.of_node = dev->of_node;
  615. rst_data->rcdev.of_reset_n_cells = 1;
  616. rst_data->rcdev.owner = THIS_MODULE;
  617. rst_data->rcdev.dev = dev;
  618. return devm_reset_controller_register(dev, &rst_data->rcdev);
  619. }
  620. static int en7581_clk_hw_init(struct platform_device *pdev,
  621. struct clk_hw_onecell_data *clk_data)
  622. {
  623. struct regmap *map;
  624. void __iomem *base;
  625. u32 val;
  626. map = syscon_regmap_lookup_by_compatible("airoha,en7581-chip-scu");
  627. if (IS_ERR(map))
  628. return PTR_ERR(map);
  629. base = devm_platform_ioremap_resource(pdev, 0);
  630. if (IS_ERR(base))
  631. return PTR_ERR(base);
  632. en7581_register_clocks(&pdev->dev, clk_data, map, base);
  633. val = readl(base + REG_NP_SCU_SSTR);
  634. val &= ~(REG_PCIE_XSI0_SEL_MASK | REG_PCIE_XSI1_SEL_MASK);
  635. writel(val, base + REG_NP_SCU_SSTR);
  636. val = readl(base + REG_NP_SCU_PCIC);
  637. writel(val | 3, base + REG_NP_SCU_PCIC);
  638. return en7581_reset_register(&pdev->dev, base, en7581_rst_map,
  639. ARRAY_SIZE(en7581_rst_map));
  640. }
  641. static int en7523_clk_probe(struct platform_device *pdev)
  642. {
  643. struct device_node *node = pdev->dev.of_node;
  644. const struct en_clk_soc_data *soc_data;
  645. struct clk_hw_onecell_data *clk_data;
  646. int r;
  647. soc_data = device_get_match_data(&pdev->dev);
  648. clk_data = devm_kzalloc(&pdev->dev,
  649. struct_size(clk_data, hws, soc_data->num_clocks),
  650. GFP_KERNEL);
  651. if (!clk_data)
  652. return -ENOMEM;
  653. clk_data->num = soc_data->num_clocks;
  654. r = soc_data->hw_init(pdev, clk_data);
  655. if (r)
  656. return r;
  657. return of_clk_add_hw_provider(node, of_clk_hw_onecell_get, clk_data);
  658. }
  659. static const struct en_clk_soc_data en7523_data = {
  660. .num_clocks = ARRAY_SIZE(en7523_base_clks) + 1,
  661. .pcie_ops = {
  662. .is_enabled = en7523_pci_is_enabled,
  663. .prepare = en7523_pci_prepare,
  664. .unprepare = en7523_pci_unprepare,
  665. },
  666. .hw_init = en7523_clk_hw_init,
  667. };
  668. static const struct en_clk_soc_data en7581_data = {
  669. /* We increment num_clocks by 1 to account for additional PCIe clock */
  670. .num_clocks = ARRAY_SIZE(en7581_base_clks) + 1,
  671. .pcie_ops = {
  672. .is_enabled = en7581_pci_is_enabled,
  673. .enable = en7581_pci_enable,
  674. .disable = en7581_pci_disable,
  675. },
  676. .hw_init = en7581_clk_hw_init,
  677. };
  678. static const struct of_device_id of_match_clk_en7523[] = {
  679. { .compatible = "airoha,en7523-scu", .data = &en7523_data },
  680. { .compatible = "airoha,en7581-scu", .data = &en7581_data },
  681. { /* sentinel */ }
  682. };
  683. static struct platform_driver clk_en7523_drv = {
  684. .probe = en7523_clk_probe,
  685. .driver = {
  686. .name = "clk-en7523",
  687. .of_match_table = of_match_clk_en7523,
  688. .suppress_bind_attrs = true,
  689. },
  690. };
  691. static int __init clk_en7523_init(void)
  692. {
  693. return platform_driver_register(&clk_en7523_drv);
  694. }
  695. arch_initcall(clk_en7523_init);