xref: /qemu/include/hw/misc/npcm_clk.h (revision c8283b0f4a7e9397da141d70e73e79341c5df2d7)
1e331f79eSHavard Skinnemoen /*
2e331f79eSHavard Skinnemoen  * Nuvoton NPCM7xx Clock Control Registers.
3e331f79eSHavard Skinnemoen  *
4e331f79eSHavard Skinnemoen  * Copyright 2020 Google LLC
5e331f79eSHavard Skinnemoen  *
6e331f79eSHavard Skinnemoen  * This program is free software; you can redistribute it and/or modify it
7e331f79eSHavard Skinnemoen  * under the terms of the GNU General Public License as published by the
8e331f79eSHavard Skinnemoen  * Free Software Foundation; either version 2 of the License, or
9e331f79eSHavard Skinnemoen  * (at your option) any later version.
10e331f79eSHavard Skinnemoen  *
11e331f79eSHavard Skinnemoen  * This program is distributed in the hope that it will be useful, but WITHOUT
12e331f79eSHavard Skinnemoen  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13e331f79eSHavard Skinnemoen  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14e331f79eSHavard Skinnemoen  * for more details.
15e331f79eSHavard Skinnemoen  */
16*c8283b0fSHao Wu #ifndef NPCM_CLK_H
17*c8283b0fSHao Wu #define NPCM_CLK_H
18e331f79eSHavard Skinnemoen 
19e331f79eSHavard Skinnemoen #include "exec/memory.h"
20bcda710fSHao Wu #include "hw/clock.h"
21e331f79eSHavard Skinnemoen #include "hw/sysbus.h"
22e331f79eSHavard Skinnemoen 
23e331f79eSHavard Skinnemoen /*
24e331f79eSHavard Skinnemoen  * Number of registers in our device state structure. Don't change this without
25e331f79eSHavard Skinnemoen  * incrementing the version_id in the vmstate.
26e331f79eSHavard Skinnemoen  */
27e331f79eSHavard Skinnemoen #define NPCM7XX_CLK_NR_REGS             (0x70 / sizeof(uint32_t))
28e331f79eSHavard Skinnemoen 
297d378ed6SHao Wu #define NPCM7XX_WATCHDOG_RESET_GPIO_IN "npcm7xx-clk-watchdog-reset-gpio-in"
307d378ed6SHao Wu 
31bcda710fSHao Wu /* Maximum amount of clock inputs in a SEL module. */
32bcda710fSHao Wu #define NPCM7XX_CLK_SEL_MAX_INPUT 5
33bcda710fSHao Wu 
34bcda710fSHao Wu /* PLLs in CLK module. */
35bcda710fSHao Wu typedef enum NPCM7xxClockPLL {
36bcda710fSHao Wu     NPCM7XX_CLOCK_PLL0,
37bcda710fSHao Wu     NPCM7XX_CLOCK_PLL1,
38bcda710fSHao Wu     NPCM7XX_CLOCK_PLL2,
39bcda710fSHao Wu     NPCM7XX_CLOCK_PLLG,
40bcda710fSHao Wu     NPCM7XX_CLOCK_NR_PLLS,
41bcda710fSHao Wu } NPCM7xxClockPLL;
42bcda710fSHao Wu 
43bcda710fSHao Wu /* SEL/MUX in CLK module. */
44bcda710fSHao Wu typedef enum NPCM7xxClockSEL {
45bcda710fSHao Wu     NPCM7XX_CLOCK_PIXCKSEL,
46bcda710fSHao Wu     NPCM7XX_CLOCK_MCCKSEL,
47bcda710fSHao Wu     NPCM7XX_CLOCK_CPUCKSEL,
48bcda710fSHao Wu     NPCM7XX_CLOCK_CLKOUTSEL,
49bcda710fSHao Wu     NPCM7XX_CLOCK_UARTCKSEL,
50bcda710fSHao Wu     NPCM7XX_CLOCK_TIMCKSEL,
51bcda710fSHao Wu     NPCM7XX_CLOCK_SDCKSEL,
52bcda710fSHao Wu     NPCM7XX_CLOCK_GFXMSEL,
53bcda710fSHao Wu     NPCM7XX_CLOCK_SUCKSEL,
54bcda710fSHao Wu     NPCM7XX_CLOCK_NR_SELS,
55bcda710fSHao Wu } NPCM7xxClockSEL;
56bcda710fSHao Wu 
57bcda710fSHao Wu /* Dividers in CLK module. */
58bcda710fSHao Wu typedef enum NPCM7xxClockDivider {
59bcda710fSHao Wu     NPCM7XX_CLOCK_PLL1D2, /* PLL1/2 */
60bcda710fSHao Wu     NPCM7XX_CLOCK_PLL2D2, /* PLL2/2 */
61bcda710fSHao Wu     NPCM7XX_CLOCK_MC_DIVIDER,
62bcda710fSHao Wu     NPCM7XX_CLOCK_AXI_DIVIDER,
63bcda710fSHao Wu     NPCM7XX_CLOCK_AHB_DIVIDER,
64bcda710fSHao Wu     NPCM7XX_CLOCK_AHB3_DIVIDER,
65bcda710fSHao Wu     NPCM7XX_CLOCK_SPI0_DIVIDER,
66bcda710fSHao Wu     NPCM7XX_CLOCK_SPIX_DIVIDER,
67bcda710fSHao Wu     NPCM7XX_CLOCK_APB1_DIVIDER,
68bcda710fSHao Wu     NPCM7XX_CLOCK_APB2_DIVIDER,
69bcda710fSHao Wu     NPCM7XX_CLOCK_APB3_DIVIDER,
70bcda710fSHao Wu     NPCM7XX_CLOCK_APB4_DIVIDER,
71bcda710fSHao Wu     NPCM7XX_CLOCK_APB5_DIVIDER,
72bcda710fSHao Wu     NPCM7XX_CLOCK_CLKOUT_DIVIDER,
73bcda710fSHao Wu     NPCM7XX_CLOCK_UART_DIVIDER,
74bcda710fSHao Wu     NPCM7XX_CLOCK_TIMER_DIVIDER,
75bcda710fSHao Wu     NPCM7XX_CLOCK_ADC_DIVIDER,
76bcda710fSHao Wu     NPCM7XX_CLOCK_MMC_DIVIDER,
77bcda710fSHao Wu     NPCM7XX_CLOCK_SDHC_DIVIDER,
78bcda710fSHao Wu     NPCM7XX_CLOCK_GFXM_DIVIDER, /* divide by 3 */
79bcda710fSHao Wu     NPCM7XX_CLOCK_UTMI_DIVIDER,
80bcda710fSHao Wu     NPCM7XX_CLOCK_NR_DIVIDERS,
81bcda710fSHao Wu } NPCM7xxClockConverter;
82bcda710fSHao Wu 
83bcda710fSHao Wu typedef struct NPCM7xxCLKState NPCM7xxCLKState;
84bcda710fSHao Wu 
85bcda710fSHao Wu /**
86bcda710fSHao Wu  * struct NPCM7xxClockPLLState - A PLL module in CLK module.
87bcda710fSHao Wu  * @name: The name of the module.
88bcda710fSHao Wu  * @clk: The CLK module that owns this module.
89bcda710fSHao Wu  * @clock_in: The input clock of this module.
90bcda710fSHao Wu  * @clock_out: The output clock of this module.
91bcda710fSHao Wu  * @reg: The control registers for this PLL module.
92bcda710fSHao Wu  */
93bcda710fSHao Wu typedef struct NPCM7xxClockPLLState {
94bcda710fSHao Wu     DeviceState parent;
95bcda710fSHao Wu 
96bcda710fSHao Wu     const char *name;
97bcda710fSHao Wu     NPCM7xxCLKState *clk;
98bcda710fSHao Wu     Clock *clock_in;
99bcda710fSHao Wu     Clock *clock_out;
100bcda710fSHao Wu 
101bcda710fSHao Wu     int reg;
102bcda710fSHao Wu } NPCM7xxClockPLLState;
103bcda710fSHao Wu 
104bcda710fSHao Wu /**
105bcda710fSHao Wu  * struct NPCM7xxClockSELState - A SEL module in CLK module.
106bcda710fSHao Wu  * @name: The name of the module.
107bcda710fSHao Wu  * @clk: The CLK module that owns this module.
108bcda710fSHao Wu  * @input_size: The size of inputs of this module.
109bcda710fSHao Wu  * @clock_in: The input clocks of this module.
110bcda710fSHao Wu  * @clock_out: The output clocks of this module.
111bcda710fSHao Wu  * @offset: The offset of this module in the control register.
112bcda710fSHao Wu  * @len: The length of this module in the control register.
113bcda710fSHao Wu  */
114bcda710fSHao Wu typedef struct NPCM7xxClockSELState {
115bcda710fSHao Wu     DeviceState parent;
116bcda710fSHao Wu 
117bcda710fSHao Wu     const char *name;
118bcda710fSHao Wu     NPCM7xxCLKState *clk;
119bcda710fSHao Wu     uint8_t input_size;
120bcda710fSHao Wu     Clock *clock_in[NPCM7XX_CLK_SEL_MAX_INPUT];
121bcda710fSHao Wu     Clock *clock_out;
122bcda710fSHao Wu 
123bcda710fSHao Wu     int offset;
124bcda710fSHao Wu     int len;
125bcda710fSHao Wu } NPCM7xxClockSELState;
126bcda710fSHao Wu 
127bcda710fSHao Wu /**
128bcda710fSHao Wu  * struct NPCM7xxClockDividerState - A Divider module in CLK module.
129bcda710fSHao Wu  * @name: The name of the module.
130bcda710fSHao Wu  * @clk: The CLK module that owns this module.
131bcda710fSHao Wu  * @clock_in: The input clock of this module.
132bcda710fSHao Wu  * @clock_out: The output clock of this module.
133bcda710fSHao Wu  * @divide: The function the divider uses to divide the input.
134bcda710fSHao Wu  * @reg: The index of the control register that contains the divisor.
135bcda710fSHao Wu  * @offset: The offset of the divisor in the control register.
136bcda710fSHao Wu  * @len: The length of the divisor in the control register.
137bcda710fSHao Wu  * @divisor: The divisor for a constant divisor
138bcda710fSHao Wu  */
139bcda710fSHao Wu typedef struct NPCM7xxClockDividerState {
140bcda710fSHao Wu     DeviceState parent;
141bcda710fSHao Wu 
142bcda710fSHao Wu     const char *name;
143bcda710fSHao Wu     NPCM7xxCLKState *clk;
144bcda710fSHao Wu     Clock *clock_in;
145bcda710fSHao Wu     Clock *clock_out;
146bcda710fSHao Wu 
147bcda710fSHao Wu     uint32_t (*divide)(struct NPCM7xxClockDividerState *s);
148bcda710fSHao Wu     union {
149bcda710fSHao Wu         struct {
150bcda710fSHao Wu             int reg;
151bcda710fSHao Wu             int offset;
152bcda710fSHao Wu             int len;
153bcda710fSHao Wu         };
154bcda710fSHao Wu         int divisor;
155bcda710fSHao Wu     };
156bcda710fSHao Wu } NPCM7xxClockDividerState;
157bcda710fSHao Wu 
158bcda710fSHao Wu struct NPCM7xxCLKState {
159e331f79eSHavard Skinnemoen     SysBusDevice parent;
160e331f79eSHavard Skinnemoen 
161e331f79eSHavard Skinnemoen     MemoryRegion iomem;
162e331f79eSHavard Skinnemoen 
163bcda710fSHao Wu     /* Clock converters */
164bcda710fSHao Wu     NPCM7xxClockPLLState plls[NPCM7XX_CLOCK_NR_PLLS];
165bcda710fSHao Wu     NPCM7xxClockSELState sels[NPCM7XX_CLOCK_NR_SELS];
166bcda710fSHao Wu     NPCM7xxClockDividerState dividers[NPCM7XX_CLOCK_NR_DIVIDERS];
167bcda710fSHao Wu 
168e331f79eSHavard Skinnemoen     uint32_t regs[NPCM7XX_CLK_NR_REGS];
169e331f79eSHavard Skinnemoen 
170e331f79eSHavard Skinnemoen     /* Time reference for SECCNT and CNTR25M, initialized by power on reset */
171e331f79eSHavard Skinnemoen     int64_t ref_ns;
172bcda710fSHao Wu 
173bcda710fSHao Wu     /* The incoming reference clock. */
174bcda710fSHao Wu     Clock *clkref;
175bcda710fSHao Wu };
176e331f79eSHavard Skinnemoen 
177e331f79eSHavard Skinnemoen #define TYPE_NPCM7XX_CLK "npcm7xx-clk"
178c79aa350SPhilippe Mathieu-Daudé OBJECT_DECLARE_SIMPLE_TYPE(NPCM7xxCLKState, NPCM7XX_CLK)
179e331f79eSHavard Skinnemoen 
180*c8283b0fSHao Wu #endif /* NPCM_CLK_H */
181