18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (c) Intel Corp. 2007.
48c2ecf20Sopenharmony_ci * All Rights Reserved.
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * Intel funded Tungsten Graphics (http://www.tungstengraphics.com) to
78c2ecf20Sopenharmony_ci * develop this driver.
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * This file is part of the Carillo Ranch video subsystem driver.
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * Authors:
128c2ecf20Sopenharmony_ci *   Thomas Hellstrom <thomas-at-tungstengraphics-dot-com>
138c2ecf20Sopenharmony_ci *   Alan Hourihane <alanh-at-tungstengraphics-dot-com>
148c2ecf20Sopenharmony_ci */
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include <linux/module.h>
178c2ecf20Sopenharmony_ci#include <linux/kernel.h>
188c2ecf20Sopenharmony_ci#include <linux/pci.h>
198c2ecf20Sopenharmony_ci#include <linux/errno.h>
208c2ecf20Sopenharmony_ci#include <linux/fb.h>
218c2ecf20Sopenharmony_ci#include "vermilion.h"
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci/* The PLL Clock register sits on Host bridge */
248c2ecf20Sopenharmony_ci#define CRVML_DEVICE_MCH   0x5001
258c2ecf20Sopenharmony_ci#define CRVML_REG_MCHBAR   0x44
268c2ecf20Sopenharmony_ci#define CRVML_REG_MCHEN    0x54
278c2ecf20Sopenharmony_ci#define CRVML_MCHEN_BIT    (1 << 28)
288c2ecf20Sopenharmony_ci#define CRVML_MCHMAP_SIZE  4096
298c2ecf20Sopenharmony_ci#define CRVML_REG_CLOCK    0xc3c
308c2ecf20Sopenharmony_ci#define CRVML_CLOCK_SHIFT  8
318c2ecf20Sopenharmony_ci#define CRVML_CLOCK_MASK   0x00000f00
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_cistatic struct pci_dev *mch_dev;
348c2ecf20Sopenharmony_cistatic u32 mch_bar;
358c2ecf20Sopenharmony_cistatic void __iomem *mch_regs_base;
368c2ecf20Sopenharmony_cistatic u32 saved_clock;
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistatic const unsigned crvml_clocks[] = {
398c2ecf20Sopenharmony_ci	6750,
408c2ecf20Sopenharmony_ci	13500,
418c2ecf20Sopenharmony_ci	27000,
428c2ecf20Sopenharmony_ci	29700,
438c2ecf20Sopenharmony_ci	37125,
448c2ecf20Sopenharmony_ci	54000,
458c2ecf20Sopenharmony_ci	59400,
468c2ecf20Sopenharmony_ci	74250,
478c2ecf20Sopenharmony_ci	120000
488c2ecf20Sopenharmony_ci	    /*
498c2ecf20Sopenharmony_ci	     * There are more clocks, but they are disabled on the CR board.
508c2ecf20Sopenharmony_ci	     */
518c2ecf20Sopenharmony_ci};
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_cistatic const u32 crvml_clock_bits[] = {
548c2ecf20Sopenharmony_ci	0x0a,
558c2ecf20Sopenharmony_ci	0x09,
568c2ecf20Sopenharmony_ci	0x08,
578c2ecf20Sopenharmony_ci	0x07,
588c2ecf20Sopenharmony_ci	0x06,
598c2ecf20Sopenharmony_ci	0x05,
608c2ecf20Sopenharmony_ci	0x04,
618c2ecf20Sopenharmony_ci	0x03,
628c2ecf20Sopenharmony_ci	0x0b
638c2ecf20Sopenharmony_ci};
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_cistatic const unsigned crvml_num_clocks = ARRAY_SIZE(crvml_clocks);
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_cistatic int crvml_sys_restore(struct vml_sys *sys)
688c2ecf20Sopenharmony_ci{
698c2ecf20Sopenharmony_ci	void __iomem *clock_reg = mch_regs_base + CRVML_REG_CLOCK;
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	iowrite32(saved_clock, clock_reg);
728c2ecf20Sopenharmony_ci	ioread32(clock_reg);
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	return 0;
758c2ecf20Sopenharmony_ci}
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_cistatic int crvml_sys_save(struct vml_sys *sys)
788c2ecf20Sopenharmony_ci{
798c2ecf20Sopenharmony_ci	void __iomem *clock_reg = mch_regs_base + CRVML_REG_CLOCK;
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	saved_clock = ioread32(clock_reg);
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	return 0;
848c2ecf20Sopenharmony_ci}
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_cistatic int crvml_nearest_index(const struct vml_sys *sys, int clock)
878c2ecf20Sopenharmony_ci{
888c2ecf20Sopenharmony_ci	int i;
898c2ecf20Sopenharmony_ci	int cur_index = 0;
908c2ecf20Sopenharmony_ci	int cur_diff;
918c2ecf20Sopenharmony_ci	int diff;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	cur_diff = clock - crvml_clocks[0];
948c2ecf20Sopenharmony_ci	cur_diff = (cur_diff < 0) ? -cur_diff : cur_diff;
958c2ecf20Sopenharmony_ci	for (i = 1; i < crvml_num_clocks; ++i) {
968c2ecf20Sopenharmony_ci		diff = clock - crvml_clocks[i];
978c2ecf20Sopenharmony_ci		diff = (diff < 0) ? -diff : diff;
988c2ecf20Sopenharmony_ci		if (diff < cur_diff) {
998c2ecf20Sopenharmony_ci			cur_index = i;
1008c2ecf20Sopenharmony_ci			cur_diff = diff;
1018c2ecf20Sopenharmony_ci		}
1028c2ecf20Sopenharmony_ci	}
1038c2ecf20Sopenharmony_ci	return cur_index;
1048c2ecf20Sopenharmony_ci}
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_cistatic int crvml_nearest_clock(const struct vml_sys *sys, int clock)
1078c2ecf20Sopenharmony_ci{
1088c2ecf20Sopenharmony_ci	return crvml_clocks[crvml_nearest_index(sys, clock)];
1098c2ecf20Sopenharmony_ci}
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_cistatic int crvml_set_clock(struct vml_sys *sys, int clock)
1128c2ecf20Sopenharmony_ci{
1138c2ecf20Sopenharmony_ci	void __iomem *clock_reg = mch_regs_base + CRVML_REG_CLOCK;
1148c2ecf20Sopenharmony_ci	int index;
1158c2ecf20Sopenharmony_ci	u32 clock_val;
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	index = crvml_nearest_index(sys, clock);
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	if (crvml_clocks[index] != clock)
1208c2ecf20Sopenharmony_ci		return -EINVAL;
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	clock_val = ioread32(clock_reg) & ~CRVML_CLOCK_MASK;
1238c2ecf20Sopenharmony_ci	clock_val = crvml_clock_bits[index] << CRVML_CLOCK_SHIFT;
1248c2ecf20Sopenharmony_ci	iowrite32(clock_val, clock_reg);
1258c2ecf20Sopenharmony_ci	ioread32(clock_reg);
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci	return 0;
1288c2ecf20Sopenharmony_ci}
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_cistatic struct vml_sys cr_pll_ops = {
1318c2ecf20Sopenharmony_ci	.name = "Carillo Ranch",
1328c2ecf20Sopenharmony_ci	.save = crvml_sys_save,
1338c2ecf20Sopenharmony_ci	.restore = crvml_sys_restore,
1348c2ecf20Sopenharmony_ci	.set_clock = crvml_set_clock,
1358c2ecf20Sopenharmony_ci	.nearest_clock = crvml_nearest_clock,
1368c2ecf20Sopenharmony_ci};
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_cistatic int __init cr_pll_init(void)
1398c2ecf20Sopenharmony_ci{
1408c2ecf20Sopenharmony_ci	int err;
1418c2ecf20Sopenharmony_ci	u32 dev_en;
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	mch_dev = pci_get_device(PCI_VENDOR_ID_INTEL,
1448c2ecf20Sopenharmony_ci					CRVML_DEVICE_MCH, NULL);
1458c2ecf20Sopenharmony_ci	if (!mch_dev) {
1468c2ecf20Sopenharmony_ci		printk(KERN_ERR
1478c2ecf20Sopenharmony_ci		       "Could not find Carillo Ranch MCH device.\n");
1488c2ecf20Sopenharmony_ci		return -ENODEV;
1498c2ecf20Sopenharmony_ci	}
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci	pci_read_config_dword(mch_dev, CRVML_REG_MCHEN, &dev_en);
1528c2ecf20Sopenharmony_ci	if (!(dev_en & CRVML_MCHEN_BIT)) {
1538c2ecf20Sopenharmony_ci		printk(KERN_ERR
1548c2ecf20Sopenharmony_ci		       "Carillo Ranch MCH device was not enabled.\n");
1558c2ecf20Sopenharmony_ci		pci_dev_put(mch_dev);
1568c2ecf20Sopenharmony_ci		return -ENODEV;
1578c2ecf20Sopenharmony_ci	}
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	pci_read_config_dword(mch_dev, CRVML_REG_MCHBAR,
1608c2ecf20Sopenharmony_ci			      &mch_bar);
1618c2ecf20Sopenharmony_ci	mch_regs_base =
1628c2ecf20Sopenharmony_ci	    ioremap(mch_bar, CRVML_MCHMAP_SIZE);
1638c2ecf20Sopenharmony_ci	if (!mch_regs_base) {
1648c2ecf20Sopenharmony_ci		printk(KERN_ERR
1658c2ecf20Sopenharmony_ci		       "Carillo Ranch MCH device was not enabled.\n");
1668c2ecf20Sopenharmony_ci		pci_dev_put(mch_dev);
1678c2ecf20Sopenharmony_ci		return -ENODEV;
1688c2ecf20Sopenharmony_ci	}
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	err = vmlfb_register_subsys(&cr_pll_ops);
1718c2ecf20Sopenharmony_ci	if (err) {
1728c2ecf20Sopenharmony_ci		printk(KERN_ERR
1738c2ecf20Sopenharmony_ci		       "Carillo Ranch failed to initialize vml_sys.\n");
1748c2ecf20Sopenharmony_ci		iounmap(mch_regs_base);
1758c2ecf20Sopenharmony_ci		pci_dev_put(mch_dev);
1768c2ecf20Sopenharmony_ci		return err;
1778c2ecf20Sopenharmony_ci	}
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	return 0;
1808c2ecf20Sopenharmony_ci}
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_cistatic void __exit cr_pll_exit(void)
1838c2ecf20Sopenharmony_ci{
1848c2ecf20Sopenharmony_ci	vmlfb_unregister_subsys(&cr_pll_ops);
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	iounmap(mch_regs_base);
1878c2ecf20Sopenharmony_ci	pci_dev_put(mch_dev);
1888c2ecf20Sopenharmony_ci}
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_cimodule_init(cr_pll_init);
1918c2ecf20Sopenharmony_cimodule_exit(cr_pll_exit);
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ciMODULE_AUTHOR("Tungsten Graphics Inc.");
1948c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Carillo Ranch PLL Driver");
1958c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
196