mpc83xx_clk.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * (C) Copyright 2018
  4. * Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc
  5. */
  6. /**
  7. * enum ratio - Description of a core clock ratio
  8. * @RAT_UNK: Unknown ratio
  9. * @RAT_BYP: Bypass
  10. * @RAT_1_TO_8: Ratio 1:8
  11. * @RAT_1_TO_4: Ratio 1:4
  12. * @RAT_1_TO_2: Ratio 1:2
  13. * @RAT_1_TO_1: Ratio 1:1
  14. * @RAT_1_5_TO_1: Ratio 1.5:1
  15. * @RAT_2_TO_1: Ratio 2:1
  16. * @RAT_2_5_TO_1: Ratio 2.5:1
  17. * @RAT_3_TO_1: Ratio 3:1
  18. */
  19. enum ratio {
  20. RAT_UNK,
  21. RAT_BYP,
  22. RAT_1_TO_8,
  23. RAT_1_TO_4,
  24. RAT_1_TO_2,
  25. RAT_1_TO_1,
  26. RAT_1_5_TO_1,
  27. RAT_2_TO_1,
  28. RAT_2_5_TO_1,
  29. RAT_3_TO_1
  30. };
  31. /**
  32. * struct corecnf - Description for a core clock configuration
  33. * @core_csb_ratio: Core clock frequency to CSB clock frequency ratio
  34. * @vco_divider: VCO divider (Core VCO frequency = Core frequency * VCO divider)
  35. */
  36. struct corecnf {
  37. int core_csb_ratio;
  38. int vco_divider;
  39. };
  40. /*
  41. * Table with all valid Core CSB frequency ratio / VCO divider combinations as
  42. * indexed by the COREPLL field of the SPMR
  43. */
  44. static const struct corecnf corecnf_tab[] = {
  45. {RAT_BYP, RAT_BYP}, /* 0x00 */
  46. {RAT_BYP, RAT_BYP}, /* 0x01 */
  47. {RAT_BYP, RAT_BYP}, /* 0x02 */
  48. {RAT_BYP, RAT_BYP}, /* 0x03 */
  49. {RAT_BYP, RAT_BYP}, /* 0x04 */
  50. {RAT_BYP, RAT_BYP}, /* 0x05 */
  51. {RAT_BYP, RAT_BYP}, /* 0x06 */
  52. {RAT_BYP, RAT_BYP}, /* 0x07 */
  53. {RAT_1_TO_1, RAT_1_TO_2}, /* 0x08 */
  54. {RAT_1_TO_1, RAT_1_TO_4}, /* 0x09 */
  55. {RAT_1_TO_1, RAT_1_TO_8}, /* 0x0A */
  56. {RAT_1_TO_1, RAT_1_TO_8}, /* 0x0B */
  57. {RAT_1_5_TO_1, RAT_1_TO_2}, /* 0x0C */
  58. {RAT_1_5_TO_1, RAT_1_TO_4}, /* 0x0D */
  59. {RAT_1_5_TO_1, RAT_1_TO_8}, /* 0x0E */
  60. {RAT_1_5_TO_1, RAT_1_TO_8}, /* 0x0F */
  61. {RAT_2_TO_1, RAT_1_TO_2}, /* 0x10 */
  62. {RAT_2_TO_1, RAT_1_TO_4}, /* 0x11 */
  63. {RAT_2_TO_1, RAT_1_TO_8}, /* 0x12 */
  64. {RAT_2_TO_1, RAT_1_TO_8}, /* 0x13 */
  65. {RAT_2_5_TO_1, RAT_1_TO_2}, /* 0x14 */
  66. {RAT_2_5_TO_1, RAT_1_TO_4}, /* 0x15 */
  67. {RAT_2_5_TO_1, RAT_1_TO_8}, /* 0x16 */
  68. {RAT_2_5_TO_1, RAT_1_TO_8}, /* 0x17 */
  69. {RAT_3_TO_1, RAT_1_TO_2}, /* 0x18 */
  70. {RAT_3_TO_1, RAT_1_TO_4}, /* 0x19 */
  71. {RAT_3_TO_1, RAT_1_TO_8}, /* 0x1A */
  72. {RAT_3_TO_1, RAT_1_TO_8}, /* 0x1B */
  73. };
  74. /**
  75. * enum reg_type - Register to read a field from
  76. * @REG_SCCR: Use the SCCR register
  77. * @REG_SPMR: Use the SPMR register
  78. */
  79. enum reg_type {
  80. REG_SCCR,
  81. REG_SPMR,
  82. };
  83. /**
  84. * enum mode_type - Description of how to read a specific frequency value
  85. * @TYPE_INVALID: Unknown type, will provoke error
  86. * @TYPE_SCCR_STANDARD: Read a field from the SCCR register, and use it
  87. * as a divider for the CSB clock to compute the
  88. * frequency
  89. * @TYPE_SCCR_ONOFF: The field describes a bit flag that can turn the
  90. * clock on or off
  91. * @TYPE_SPMR_DIRECT_MULTIPLY: Read a field from the SPMR register, and use it
  92. * as a multiplier for the CSB clock to compute the
  93. * frequency
  94. * @TYPE_SPECIAL: The frequency is calculated in a non-standard way
  95. */
  96. enum mode_type {
  97. TYPE_INVALID = 0,
  98. TYPE_SCCR_STANDARD,
  99. TYPE_SCCR_ONOFF,
  100. TYPE_SPMR_DIRECT_MULTIPLY,
  101. TYPE_SPECIAL,
  102. };
  103. /* Map of each clock index to its human-readable name */
  104. static const char * const names[] = {
  105. [MPC83XX_CLK_CORE] = "Core",
  106. [MPC83XX_CLK_CSB] = "Coherent System Bus",
  107. [MPC83XX_CLK_QE] = "QE",
  108. [MPC83XX_CLK_BRG] = "BRG",
  109. [MPC83XX_CLK_LBIU] = "Local Bus Controller",
  110. [MPC83XX_CLK_LCLK] = "Local Bus",
  111. [MPC83XX_CLK_MEM] = "DDR",
  112. [MPC83XX_CLK_MEM_SEC] = "DDR Secondary",
  113. [MPC83XX_CLK_ENC] = "SEC",
  114. [MPC83XX_CLK_I2C1] = "I2C1",
  115. [MPC83XX_CLK_I2C2] = "I2C2",
  116. [MPC83XX_CLK_TDM] = "TDM",
  117. [MPC83XX_CLK_SDHC] = "SDHC",
  118. [MPC83XX_CLK_TSEC1] = "TSEC1",
  119. [MPC83XX_CLK_TSEC2] = "TSEC2",
  120. [MPC83XX_CLK_USBDR] = "USB DR",
  121. [MPC83XX_CLK_USBMPH] = "USB MPH",
  122. [MPC83XX_CLK_PCIEXP1] = "PCIEXP1",
  123. [MPC83XX_CLK_PCIEXP2] = "PCIEXP2",
  124. [MPC83XX_CLK_SATA] = "SATA",
  125. [MPC83XX_CLK_DMAC] = "DMAC",
  126. [MPC83XX_CLK_PCI] = "PCI",
  127. };
  128. /**
  129. * struct clk_mode - Structure for clock mode descriiptions
  130. * @low: The low bit of the data field to read for this mode (may not apply to
  131. * some modes)
  132. * @high: The high bit of the data field to read for this mode (may not apply to
  133. * some modes)
  134. * @type: The type of the mode description (one of enum mode_type)
  135. */
  136. struct clk_mode {
  137. u8 low;
  138. u8 high;
  139. int type;
  140. };
  141. /**
  142. * set_mode() - Build a clock mode description from data
  143. * @mode: The clock mode description to be filled out
  144. * @low: The low bit of the data field to read for this mode (may not apply to
  145. * some modes)
  146. * @high: The high bit of the data field to read for this mode (may not apply to
  147. * some modes)
  148. * @type: The type of the mode description (one of enum mode_type)
  149. *
  150. * Clock mode descriptions are a succinct description of how to read a specific
  151. * clock's rate from the hardware; usually by reading a specific field of a
  152. * register, such a s the SCCR register, but some types use different methods
  153. * for obtaining the clock rate.
  154. */
  155. static void set_mode(struct clk_mode *mode, u8 low, u8 high, int type)
  156. {
  157. mode->low = low;
  158. mode->high = high;
  159. mode->type = type;
  160. }
  161. /**
  162. * retrieve_mode() - Get the clock mode description for a specific clock
  163. * @clk: The identifier of the clock for which the clock description should
  164. * be retrieved
  165. * @soc_type: The type of MPC83xx SoC for which the clock description should be
  166. * retrieved
  167. * @mode: Pointer to a clk_mode structure to be filled with data for the
  168. * clock
  169. *
  170. * Since some clock rate are stored in different places on different MPC83xx
  171. * SoCs, the SoC type has to be supplied along with the clock's identifier.
  172. *
  173. * Return: 0 if OK, -ve on error
  174. */
  175. static int retrieve_mode(int clk, int soc_type, struct clk_mode *mode)
  176. {
  177. switch (clk) {
  178. case MPC83XX_CLK_CORE:
  179. case MPC83XX_CLK_CSB:
  180. case MPC83XX_CLK_QE:
  181. case MPC83XX_CLK_BRG:
  182. case MPC83XX_CLK_LCLK:
  183. case MPC83XX_CLK_I2C2:
  184. set_mode(mode, 0, 0, TYPE_SPECIAL);
  185. break;
  186. case MPC83XX_CLK_MEM:
  187. set_mode(mode, 1, 1, TYPE_SPMR_DIRECT_MULTIPLY);
  188. break;
  189. case MPC83XX_CLK_LBIU:
  190. case MPC83XX_CLK_MEM_SEC:
  191. set_mode(mode, 0, 0, TYPE_SPMR_DIRECT_MULTIPLY);
  192. break;
  193. case MPC83XX_CLK_TSEC1:
  194. set_mode(mode, 0, 1, TYPE_SCCR_STANDARD);
  195. break;
  196. case MPC83XX_CLK_TSEC2:
  197. if (soc_type == SOC_MPC8313) /* I2C and TSEC2 are the same register */
  198. set_mode(mode, 2, 3, TYPE_SCCR_STANDARD);
  199. else /* FIXME(mario.six@gdsys.cc): This has separate enable/disable bit! */
  200. set_mode(mode, 0, 1, TYPE_SCCR_STANDARD);
  201. break;
  202. case MPC83XX_CLK_SDHC:
  203. set_mode(mode, 4, 5, TYPE_SCCR_STANDARD);
  204. break;
  205. case MPC83XX_CLK_ENC:
  206. set_mode(mode, 6, 7, TYPE_SCCR_STANDARD);
  207. break;
  208. case MPC83XX_CLK_I2C1:
  209. if (soc_type == SOC_MPC8349)
  210. set_mode(mode, 2, 3, TYPE_SCCR_STANDARD);
  211. else /* I2C and ENC are the same register */
  212. set_mode(mode, 6, 7, TYPE_SCCR_STANDARD);
  213. break;
  214. case MPC83XX_CLK_PCIEXP1:
  215. set_mode(mode, 10, 11, TYPE_SCCR_STANDARD);
  216. break;
  217. case MPC83XX_CLK_PCIEXP2:
  218. set_mode(mode, 12, 13, TYPE_SCCR_STANDARD);
  219. break;
  220. case MPC83XX_CLK_USBDR:
  221. if (soc_type == SOC_MPC8313 || soc_type == SOC_MPC8349)
  222. set_mode(mode, 10, 11, TYPE_SCCR_STANDARD);
  223. else
  224. set_mode(mode, 8, 9, TYPE_SCCR_STANDARD);
  225. break;
  226. case MPC83XX_CLK_USBMPH:
  227. set_mode(mode, 8, 9, TYPE_SCCR_STANDARD);
  228. break;
  229. case MPC83XX_CLK_PCI:
  230. set_mode(mode, 15, 15, TYPE_SCCR_ONOFF);
  231. break;
  232. case MPC83XX_CLK_DMAC:
  233. set_mode(mode, 26, 27, TYPE_SCCR_STANDARD);
  234. break;
  235. case MPC83XX_CLK_SATA:
  236. /* FIXME(mario.six@gdsys.cc): All SATA controllers must have the same clock ratio */
  237. if (soc_type == SOC_MPC8379) {
  238. set_mode(mode, 24, 25, TYPE_SCCR_STANDARD);
  239. set_mode(mode, 26, 27, TYPE_SCCR_STANDARD);
  240. set_mode(mode, 28, 29, TYPE_SCCR_STANDARD);
  241. set_mode(mode, 30, 31, TYPE_SCCR_STANDARD);
  242. } else {
  243. set_mode(mode, 18, 19, TYPE_SCCR_STANDARD);
  244. set_mode(mode, 20, 21, TYPE_SCCR_STANDARD);
  245. }
  246. break;
  247. case MPC83XX_CLK_TDM:
  248. set_mode(mode, 26, 27, TYPE_SCCR_STANDARD);
  249. break;
  250. default:
  251. debug("%s: Unknown clock type %d on soc type %d\n",
  252. __func__, clk, soc_type);
  253. set_mode(mode, 0, 0, TYPE_INVALID);
  254. return -EINVAL;
  255. }
  256. return 0;
  257. }
  258. /**
  259. * get_spmr() - Read the SPMR (System PLL Mode Register)
  260. * @im: Pointer to the MPC83xx main register map in question
  261. *
  262. * Return: The SPMR value as a 32-bit number.
  263. */
  264. static inline u32 get_spmr(immap_t *im)
  265. {
  266. u32 res = in_be32(&im->clk.spmr);
  267. return res;
  268. }
  269. /**
  270. * get_sccr() - Read the SCCR (System Clock Control Register)
  271. * @im: Pointer to the MPC83xx main register map in question
  272. *
  273. * Return: The SCCR value as a 32-bit number.
  274. */
  275. static inline u32 get_sccr(immap_t *im)
  276. {
  277. u32 res = in_be32(&im->clk.sccr);
  278. return res;
  279. }
  280. /**
  281. * get_lcrr() - Read the LCRR (Clock Ratio Register)
  282. * @im: Pointer to the MPC83xx main register map in question
  283. *
  284. * Return: The LCRR value as a 32-bit number.
  285. */
  286. static inline u32 get_lcrr(immap_t *im)
  287. {
  288. u32 res = in_be32(&im->im_lbc.lcrr);
  289. return res;
  290. }
  291. /**
  292. * get_pci_sync_in() - Read the PCI synchronization clock speed
  293. * @im: Pointer to the MPC83xx main register map in question
  294. *
  295. * Return: The PCI synchronization clock speed value as a 32-bit number.
  296. */
  297. static inline u32 get_pci_sync_in(immap_t *im)
  298. {
  299. u8 clkin_div;
  300. clkin_div = (get_spmr(im) & SPMR_CKID) >> SPMR_CKID_SHIFT;
  301. return CONFIG_SYS_CLK_FREQ / (1 + clkin_div);
  302. }
  303. /**
  304. * get_csb_clk() - Read the CSB (Coheren System Bus) clock speed
  305. * @im: Pointer to the MPC83xx main register map in question
  306. *
  307. * Return: The CSB clock speed value as a 32-bit number.
  308. */
  309. static inline u32 get_csb_clk(immap_t *im)
  310. {
  311. u8 spmf;
  312. spmf = (get_spmr(im) & SPMR_SPMF) >> SPMR_SPMF_SHIFT;
  313. return CONFIG_SYS_CLK_FREQ * spmf;
  314. }
  315. /**
  316. * spmr_field() - Read a specific SPMR field
  317. * @im: Pointer to the MPC83xx main register map in question
  318. * @mask: A bitmask that describes the bitfield to be read
  319. *
  320. * Return: The value of the bit field as a 32-bit number.
  321. */
  322. static inline uint spmr_field(immap_t *im, u32 mask)
  323. {
  324. /* Extract shift from bitmask */
  325. uint shift = mask ? ffs(mask) - 1 : 0;
  326. return (get_spmr(im) & mask) >> shift;
  327. }
  328. /**
  329. * sccr_field() - Read a specific SCCR field
  330. * @im: Pointer to the MPC83xx main register map in question
  331. * @mask: A bitmask that describes the bitfield to be read
  332. *
  333. * Return: The value of the bit field as a 32-bit number.
  334. */
  335. static inline uint sccr_field(immap_t *im, u32 mask)
  336. {
  337. /* Extract shift from bitmask */
  338. uint shift = mask ? ffs(mask) - 1 : 0;
  339. return (get_sccr(im) & mask) >> shift;
  340. }
  341. /**
  342. * lcrr_field() - Read a specific LCRR field
  343. * @im: Pointer to the MPC83xx main register map in question
  344. * @mask: A bitmask that describes the bitfield to be read
  345. *
  346. * Return: The value of the bit field as a 32-bit number.
  347. */
  348. static inline uint lcrr_field(immap_t *im, u32 mask)
  349. {
  350. /* Extract shift from bitmask */
  351. uint shift = mask ? ffs(mask) - 1 : 0;
  352. return (get_lcrr(im) & mask) >> shift;
  353. }