1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Hisilicon Hibmc SoC drm driver
3  *
4  * Based on the bochs drm driver.
5  *
6  * Copyright (c) 2016 Huawei Limited.
7  *
8  * Author:
9  *	Rongrong Zou <zourongrong@huawei.com>
10  *	Rongrong Zou <zourongrong@gmail.com>
11  *	Jianhua Li <lijianhua@huawei.com>
12  */
13 
14 #include <linux/module.h>
15 #include <linux/pci.h>
16 
17 #include <drm/drm_atomic_helper.h>
18 #include <drm/drm_drv.h>
19 #include <drm/drm_gem_framebuffer_helper.h>
20 #include <drm/drm_gem_vram_helper.h>
21 #include <drm/drm_irq.h>
22 #include <drm/drm_managed.h>
23 #include <drm/drm_vblank.h>
24 
25 #include "hibmc_drm_drv.h"
26 #include "hibmc_drm_regs.h"
27 
28 DEFINE_DRM_GEM_FOPS(hibmc_fops);
29 
30 static irqreturn_t hibmc_drm_interrupt(int irq, void *arg)
31 {
32 	struct drm_device *dev = (struct drm_device *)arg;
33 	struct hibmc_drm_private *priv = to_hibmc_drm_private(dev);
34 	u32 status;
35 
36 	status = readl(priv->mmio + HIBMC_RAW_INTERRUPT);
37 
38 	if (status & HIBMC_RAW_INTERRUPT_VBLANK(1)) {
39 		writel(HIBMC_RAW_INTERRUPT_VBLANK(1),
40 		       priv->mmio + HIBMC_RAW_INTERRUPT);
41 		drm_handle_vblank(dev, 0);
42 	}
43 
44 	return IRQ_HANDLED;
45 }
46 
47 static int hibmc_dumb_create(struct drm_file *file, struct drm_device *dev,
48 			     struct drm_mode_create_dumb *args)
49 {
50 	return drm_gem_vram_fill_create_dumb(file, dev, 0, 128, args);
51 }
52 
53 static const struct drm_driver hibmc_driver = {
54 	.driver_features	= DRIVER_GEM | DRIVER_MODESET | DRIVER_ATOMIC,
55 	.fops			= &hibmc_fops,
56 	.name			= "hibmc",
57 	.date			= "20160828",
58 	.desc			= "hibmc drm driver",
59 	.major			= 1,
60 	.minor			= 0,
61 	.debugfs_init		= drm_vram_mm_debugfs_init,
62 	.dumb_create            = hibmc_dumb_create,
63 	.dumb_map_offset        = drm_gem_vram_driver_dumb_mmap_offset,
64 	.gem_prime_mmap		= drm_gem_prime_mmap,
65 	.irq_handler		= hibmc_drm_interrupt,
66 };
67 
68 static int __maybe_unused hibmc_pm_suspend(struct device *dev)
69 {
70 	struct drm_device *drm_dev = dev_get_drvdata(dev);
71 
72 	return drm_mode_config_helper_suspend(drm_dev);
73 }
74 
75 static int  __maybe_unused hibmc_pm_resume(struct device *dev)
76 {
77 	struct drm_device *drm_dev = dev_get_drvdata(dev);
78 
79 	return drm_mode_config_helper_resume(drm_dev);
80 }
81 
82 static const struct dev_pm_ops hibmc_pm_ops = {
83 	SET_SYSTEM_SLEEP_PM_OPS(hibmc_pm_suspend,
84 				hibmc_pm_resume)
85 };
86 
87 static const struct drm_mode_config_funcs hibmc_mode_funcs = {
88 	.mode_valid = drm_vram_helper_mode_valid,
89 	.atomic_check = drm_atomic_helper_check,
90 	.atomic_commit = drm_atomic_helper_commit,
91 	.fb_create = drm_gem_fb_create,
92 };
93 
94 static int hibmc_kms_init(struct hibmc_drm_private *priv)
95 {
96 	struct drm_device *dev = &priv->dev;
97 	int ret;
98 
99 	ret = drmm_mode_config_init(dev);
100 	if (ret)
101 		return ret;
102 
103 	dev->mode_config.min_width = 0;
104 	dev->mode_config.min_height = 0;
105 	dev->mode_config.max_width = 1920;
106 	dev->mode_config.max_height = 1200;
107 
108 	dev->mode_config.fb_base = priv->fb_base;
109 	dev->mode_config.preferred_depth = 32;
110 	dev->mode_config.prefer_shadow = 1;
111 
112 	dev->mode_config.funcs = (void *)&hibmc_mode_funcs;
113 
114 	ret = hibmc_de_init(priv);
115 	if (ret) {
116 		drm_err(dev, "failed to init de: %d\n", ret);
117 		return ret;
118 	}
119 
120 	ret = hibmc_vdac_init(priv);
121 	if (ret) {
122 		drm_err(dev, "failed to init vdac: %d\n", ret);
123 		return ret;
124 	}
125 
126 	return 0;
127 }
128 
129 /*
130  * It can operate in one of three modes: 0, 1 or Sleep.
131  */
132 void hibmc_set_power_mode(struct hibmc_drm_private *priv, u32 power_mode)
133 {
134 	u32 control_value = 0;
135 	void __iomem   *mmio = priv->mmio;
136 	u32 input = 1;
137 
138 	if (power_mode > HIBMC_PW_MODE_CTL_MODE_SLEEP)
139 		return;
140 
141 	if (power_mode == HIBMC_PW_MODE_CTL_MODE_SLEEP)
142 		input = 0;
143 
144 	control_value = readl(mmio + HIBMC_POWER_MODE_CTRL);
145 	control_value &= ~(HIBMC_PW_MODE_CTL_MODE_MASK |
146 			   HIBMC_PW_MODE_CTL_OSC_INPUT_MASK);
147 	control_value |= HIBMC_FIELD(HIBMC_PW_MODE_CTL_MODE, power_mode);
148 	control_value |= HIBMC_FIELD(HIBMC_PW_MODE_CTL_OSC_INPUT, input);
149 	writel(control_value, mmio + HIBMC_POWER_MODE_CTRL);
150 }
151 
152 void hibmc_set_current_gate(struct hibmc_drm_private *priv, unsigned int gate)
153 {
154 	u32 gate_reg;
155 	u32 mode;
156 	void __iomem   *mmio = priv->mmio;
157 
158 	/* Get current power mode. */
159 	mode = (readl(mmio + HIBMC_POWER_MODE_CTRL) &
160 		HIBMC_PW_MODE_CTL_MODE_MASK) >> HIBMC_PW_MODE_CTL_MODE_SHIFT;
161 
162 	switch (mode) {
163 	case HIBMC_PW_MODE_CTL_MODE_MODE0:
164 		gate_reg = HIBMC_MODE0_GATE;
165 		break;
166 
167 	case HIBMC_PW_MODE_CTL_MODE_MODE1:
168 		gate_reg = HIBMC_MODE1_GATE;
169 		break;
170 
171 	default:
172 		gate_reg = HIBMC_MODE0_GATE;
173 		break;
174 	}
175 	writel(gate, mmio + gate_reg);
176 }
177 
178 static void hibmc_hw_config(struct hibmc_drm_private *priv)
179 {
180 	u32 reg;
181 
182 	/* On hardware reset, power mode 0 is default. */
183 	hibmc_set_power_mode(priv, HIBMC_PW_MODE_CTL_MODE_MODE0);
184 
185 	/* Enable display power gate & LOCALMEM power gate*/
186 	reg = readl(priv->mmio + HIBMC_CURRENT_GATE);
187 	reg &= ~HIBMC_CURR_GATE_DISPLAY_MASK;
188 	reg &= ~HIBMC_CURR_GATE_LOCALMEM_MASK;
189 	reg |= HIBMC_CURR_GATE_DISPLAY(1);
190 	reg |= HIBMC_CURR_GATE_LOCALMEM(1);
191 
192 	hibmc_set_current_gate(priv, reg);
193 
194 	/*
195 	 * Reset the memory controller. If the memory controller
196 	 * is not reset in chip,the system might hang when sw accesses
197 	 * the memory.The memory should be resetted after
198 	 * changing the MXCLK.
199 	 */
200 	reg = readl(priv->mmio + HIBMC_MISC_CTRL);
201 	reg &= ~HIBMC_MSCCTL_LOCALMEM_RESET_MASK;
202 	reg |= HIBMC_MSCCTL_LOCALMEM_RESET(0);
203 	writel(reg, priv->mmio + HIBMC_MISC_CTRL);
204 
205 	reg &= ~HIBMC_MSCCTL_LOCALMEM_RESET_MASK;
206 	reg |= HIBMC_MSCCTL_LOCALMEM_RESET(1);
207 
208 	writel(reg, priv->mmio + HIBMC_MISC_CTRL);
209 }
210 
211 static int hibmc_hw_map(struct hibmc_drm_private *priv)
212 {
213 	struct drm_device *dev = &priv->dev;
214 	struct pci_dev *pdev = to_pci_dev(dev->dev);
215 	resource_size_t addr, size, ioaddr, iosize;
216 
217 	ioaddr = pci_resource_start(pdev, 1);
218 	iosize = pci_resource_len(pdev, 1);
219 	priv->mmio = devm_ioremap(dev->dev, ioaddr, iosize);
220 	if (!priv->mmio) {
221 		drm_err(dev, "Cannot map mmio region\n");
222 		return -ENOMEM;
223 	}
224 
225 	addr = pci_resource_start(pdev, 0);
226 	size = pci_resource_len(pdev, 0);
227 	priv->fb_map = devm_ioremap(dev->dev, addr, size);
228 	if (!priv->fb_map) {
229 		drm_err(dev, "Cannot map framebuffer\n");
230 		return -ENOMEM;
231 	}
232 	priv->fb_base = addr;
233 	priv->fb_size = size;
234 
235 	return 0;
236 }
237 
238 static int hibmc_hw_init(struct hibmc_drm_private *priv)
239 {
240 	int ret;
241 
242 	ret = hibmc_hw_map(priv);
243 	if (ret)
244 		return ret;
245 
246 	hibmc_hw_config(priv);
247 
248 	return 0;
249 }
250 
251 static int hibmc_unload(struct drm_device *dev)
252 {
253 	drm_atomic_helper_shutdown(dev);
254 
255 	if (dev->irq_enabled)
256 		drm_irq_uninstall(dev);
257 
258 	pci_disable_msi(to_pci_dev(dev->dev));
259 
260 	return 0;
261 }
262 
263 static int hibmc_load(struct drm_device *dev)
264 {
265 	struct pci_dev *pdev = to_pci_dev(dev->dev);
266 	struct hibmc_drm_private *priv = to_hibmc_drm_private(dev);
267 	int ret;
268 
269 	ret = hibmc_hw_init(priv);
270 	if (ret)
271 		goto err;
272 
273 	ret = drmm_vram_helper_init(dev, pci_resource_start(pdev, 0), priv->fb_size);
274 	if (ret) {
275 		drm_err(dev, "Error initializing VRAM MM; %d\n", ret);
276 		goto err;
277 	}
278 
279 	ret = hibmc_kms_init(priv);
280 	if (ret)
281 		goto err;
282 
283 	ret = drm_vblank_init(dev, dev->mode_config.num_crtc);
284 	if (ret) {
285 		drm_err(dev, "failed to initialize vblank: %d\n", ret);
286 		goto err;
287 	}
288 
289 	ret = pci_enable_msi(pdev);
290 	if (ret) {
291 		drm_warn(dev, "enabling MSI failed: %d\n", ret);
292 	} else {
293 		ret = drm_irq_install(dev, pdev->irq);
294 		if (ret)
295 			drm_warn(dev, "install irq failed: %d\n", ret);
296 	}
297 
298 	/* reset all the states of crtc/plane/encoder/connector */
299 	drm_mode_config_reset(dev);
300 
301 	return 0;
302 
303 err:
304 	hibmc_unload(dev);
305 	drm_err(dev, "failed to initialize drm driver: %d\n", ret);
306 	return ret;
307 }
308 
309 static int hibmc_pci_probe(struct pci_dev *pdev,
310 			   const struct pci_device_id *ent)
311 {
312 	struct hibmc_drm_private *priv;
313 	struct drm_device *dev;
314 	int ret;
315 
316 	ret = drm_fb_helper_remove_conflicting_pci_framebuffers(pdev,
317 								"hibmcdrmfb");
318 	if (ret)
319 		return ret;
320 
321 	priv = devm_drm_dev_alloc(&pdev->dev, &hibmc_driver,
322 				  struct hibmc_drm_private, dev);
323 	if (IS_ERR(priv)) {
324 		DRM_ERROR("failed to allocate drm_device\n");
325 		return PTR_ERR(priv);
326 	}
327 
328 	dev = &priv->dev;
329 	pci_set_drvdata(pdev, dev);
330 
331 	ret = pcim_enable_device(pdev);
332 	if (ret) {
333 		drm_err(dev, "failed to enable pci device: %d\n", ret);
334 		goto err_return;
335 	}
336 
337 	ret = hibmc_load(dev);
338 	if (ret) {
339 		drm_err(dev, "failed to load hibmc: %d\n", ret);
340 		goto err_return;
341 	}
342 
343 	ret = drm_dev_register(dev, 0);
344 	if (ret) {
345 		drm_err(dev, "failed to register drv for userspace access: %d\n",
346 			  ret);
347 		goto err_unload;
348 	}
349 
350 	drm_fbdev_generic_setup(dev, dev->mode_config.preferred_depth);
351 
352 	return 0;
353 
354 err_unload:
355 	hibmc_unload(dev);
356 err_return:
357 	return ret;
358 }
359 
360 static void hibmc_pci_remove(struct pci_dev *pdev)
361 {
362 	struct drm_device *dev = pci_get_drvdata(pdev);
363 
364 	drm_dev_unregister(dev);
365 	hibmc_unload(dev);
366 }
367 
368 static const struct pci_device_id hibmc_pci_table[] = {
369 	{ PCI_VDEVICE(HUAWEI, 0x1711) },
370 	{0,}
371 };
372 
373 static struct pci_driver hibmc_pci_driver = {
374 	.name =		"hibmc-drm",
375 	.id_table =	hibmc_pci_table,
376 	.probe =	hibmc_pci_probe,
377 	.remove =	hibmc_pci_remove,
378 	.driver.pm =    &hibmc_pm_ops,
379 };
380 
381 module_pci_driver(hibmc_pci_driver);
382 
383 MODULE_DEVICE_TABLE(pci, hibmc_pci_table);
384 MODULE_AUTHOR("RongrongZou <zourongrong@huawei.com>");
385 MODULE_DESCRIPTION("DRM Driver for Hisilicon Hibmc");
386 MODULE_LICENSE("GPL v2");
387