1/*	$NetBSD: fbmem.h,v 1.5 2021/12/18 23:45:39 riastradh Exp $	*/
2
3/*
4 * Copyright (C) 2010 Francisco Jerez.
5 * All Rights Reserved.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice (including the
16 * next paragraph) shall be included in all copies or substantial
17 * portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
23 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 */
28#include <subdev/fb/regsnv04.h>
29
30#define NV04_PFB_DEBUG_0					0x00100080
31#	define NV04_PFB_DEBUG_0_PAGE_MODE			0x00000001
32#	define NV04_PFB_DEBUG_0_REFRESH_OFF			0x00000010
33#	define NV04_PFB_DEBUG_0_REFRESH_COUNTX64		0x00003f00
34#	define NV04_PFB_DEBUG_0_REFRESH_SLOW_CLK		0x00004000
35#	define NV04_PFB_DEBUG_0_SAFE_MODE			0x00008000
36#	define NV04_PFB_DEBUG_0_ALOM_ENABLE			0x00010000
37#	define NV04_PFB_DEBUG_0_CASOE				0x00100000
38#	define NV04_PFB_DEBUG_0_CKE_INVERT			0x10000000
39#	define NV04_PFB_DEBUG_0_REFINC				0x20000000
40#	define NV04_PFB_DEBUG_0_SAVE_POWER_OFF			0x40000000
41#define NV04_PFB_CFG0						0x00100200
42#	define NV04_PFB_CFG0_SCRAMBLE				0x20000000
43#define NV04_PFB_CFG1						0x00100204
44#define NV04_PFB_SCRAMBLE(i)                         (0x00100400 + 4 * (i))
45
46#define NV10_PFB_REFCTRL					0x00100210
47#	define NV10_PFB_REFCTRL_VALID_1				(1 << 31)
48
49static inline struct io_mapping *
50fbmem_init(struct nvkm_device *dev)
51{
52#ifdef __NetBSD__
53	return bus_space_io_mapping_create_wc(dev->func->resource_tag(dev, 1),
54	    dev->func->resource_addr(dev, 1),
55	    dev->func->resource_size(dev, 1));
56#else
57	return io_mapping_create_wc(dev->func->resource_addr(dev, 1),
58				    dev->func->resource_size(dev, 1));
59#endif
60}
61
62static inline void
63fbmem_fini(struct io_mapping *fb)
64{
65	io_mapping_free(fb);
66}
67
68#ifdef __NetBSD__
69/*
70 * XXX Consider using bus_space_reserve/map instead.  Don't want to use
71 * bus_space_map because presumably that will eat too much KVA.
72 */
73
74#  define	__iomem		volatile
75#  define	ioread32	fake_ioread32
76#  define	iowrite32	fake_iowrite32
77
78static inline uint32_t
79ioread32(const void __iomem *p)
80{
81	const uint32_t v = *(const uint32_t __iomem *)p;
82
83	membar_consumer();
84
85	return v;		/* XXX nouveau byte order */
86}
87
88static inline void
89iowrite32(uint32_t v, void __iomem *p)
90{
91
92	membar_producer();
93	*(uint32_t __iomem *)p = v; /* XXX nouveau byte order */
94}
95#endif
96
97static inline u32
98fbmem_peek(struct io_mapping *fb, u32 off)
99{
100	u8 __iomem *p = io_mapping_map_atomic_wc(fb, off & PAGE_MASK);
101	u32 val = ioread32(p + (off & ~PAGE_MASK));
102#ifdef __NetBSD__
103	io_mapping_unmap_atomic(fb, __UNVOLATILE(p));
104#else
105	io_mapping_unmap_atomic(p);
106#endif
107	return val;
108}
109
110static inline void
111fbmem_poke(struct io_mapping *fb, u32 off, u32 val)
112{
113	u8 __iomem *p = io_mapping_map_atomic_wc(fb, off & PAGE_MASK);
114	iowrite32(val, p + (off & ~PAGE_MASK));
115	wmb();
116#ifdef __NetBSD__
117	io_mapping_unmap_atomic(fb, __UNVOLATILE(p));
118#else
119	io_mapping_unmap_atomic(p);
120#endif
121}
122
123static inline bool
124fbmem_readback(struct io_mapping *fb, u32 off, u32 val)
125{
126	fbmem_poke(fb, off, val);
127	return val == fbmem_peek(fb, off);
128}
129
130#ifdef __NetBSD__
131#  undef	__iomem
132#  undef	ioread32
133#  undef	iowrite32
134#endif
135