forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 6
/
gs.c
344 lines (298 loc) · 8.96 KB
/
gs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// SPDX-License-Identifier: GPL-2.0
/*
* PlayStation 2 Graphics Synthesizer (GS)
*
* Copyright (C) 2019 Fredrik Noring
*/
#include <linux/ctype.h>
#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/types.h>
#include <asm/mach-ps2/gif.h>
#include <asm/mach-ps2/gs.h>
#include <asm/mach-ps2/irq.h>
#include <asm/mach-ps2/rom.h>
#include <uapi/asm/gs.h>
static struct device *gs_dev;
/**
* gs_region_pal - is the machine for a PAL video mode region?
*
* See also gs_region_ntsc(). The system region is determined by rom_version(),
* which is an approximation because the ROM region does not always correspdond
* to the video region.
*
* Return: %true if PAL video mode is appropriate for the region, else %false
*/
bool gs_region_pal(void)
{
return rom_version().region == 'E';
}
EXPORT_SYMBOL_GPL(gs_region_pal);
/**
* gs_region_ntsc - is the machine for an NTSC video mode region?
*
* See also gs_region_pal(). The system region is determined by rom_version(),
* which is an approximation because the ROM region does not always correspdond
* to the video region.
*
* Return: %true if NTSC video mode is appropriate for the region, else %false
*/
bool gs_region_ntsc(void)
{
return !gs_region_pal();
}
EXPORT_SYMBOL_GPL(gs_region_ntsc);
/**
* gs_video_clock - video clock (VCK) frequency given SMODE1 bit fields
* @t1248 - &gs_smode1.t1248 PLL output divider
* @lc - &gs_smode1.lc PLL loop divider
* @rc - &gs_smode1.rc PLL reference divider
*
* Return: video clock (VCK)
*/
u32 gs_video_clock(const u32 t1248, const u32 lc, const u32 rc)
{
return (13500000 * lc) / ((t1248 + 1) * rc);
}
EXPORT_SYMBOL_GPL(gs_video_clock);
/**
* gs_video_clock_for_smode1 - video clock (VCK) frequency given SMODE1
* register value
* @smode1: SMODE1 register value
*
* Return: video clock (VCK)
*/
u32 gs_video_clock_for_smode1(const struct gs_smode1 smode1)
{
return gs_video_clock(smode1.t1248, smode1.lc, smode1.rc);
}
EXPORT_SYMBOL_GPL(gs_video_clock_for_smode1);
/**
* gs_psm_ct16_block_count - number of blocks for 16-bit pixel storage
* @fbw: buffer width/64
* @fbh: buffer height
*
* Return: number of blocks for 16-bit pixel storage of given width and height
*/
u32 gs_psm_ct16_block_count(const u32 fbw, const u32 fbh)
{
const u32 block_cols = fbw * GS_PSM_CT16_PAGE_COLS;
const u32 block_rows = (fbh + GS_PSM_CT16_BLOCK_HEIGHT - 1) /
GS_PSM_CT16_BLOCK_HEIGHT;
return block_cols * block_rows;
}
EXPORT_SYMBOL_GPL(gs_psm_ct16_block_count);
/**
* gs_psm_ct32_block_count - number of blocks for 32-bit pixel storage
* @fbw: buffer width/64
* @fbh: buffer height
*
* Return: number of blocks for 32-bit pixel storage of given width and height
*/
u32 gs_psm_ct32_block_count(const u32 fbw, const u32 fbh)
{
const u32 block_cols = fbw * GS_PSM_CT32_PAGE_COLS;
const u32 block_rows = (fbh + GS_PSM_CT32_BLOCK_HEIGHT - 1) /
GS_PSM_CT32_BLOCK_HEIGHT;
return block_cols * block_rows;
}
EXPORT_SYMBOL_GPL(gs_psm_ct32_block_count);
/* FIXME */
u32 gs_psm_ct16_blocks_available(const u32 fbw, const u32 fbh)
{
const u32 block_count = gs_psm_ct16_block_count(fbw, fbh);
return block_count <= GS_BLOCK_COUNT ?
GS_BLOCK_COUNT - block_count : 0;
}
EXPORT_SYMBOL_GPL(gs_psm_ct16_blocks_available);
/* FIXME */
u32 gs_psm_ct32_blocks_available(const u32 fbw, const u32 fbh)
{
const u32 block_count = gs_psm_ct32_block_count(fbw, fbh);
return block_count <= GS_BLOCK_COUNT ?
GS_BLOCK_COUNT - block_count : 0;
}
EXPORT_SYMBOL_GPL(gs_psm_ct32_blocks_available);
/**
* gs_psm_ct16_block_address - 16-bit block address given a block index
* @fbw: buffer width/64
* @block_index: block index starting at the top left corner
*
* Return: block address for a given block index
*/
u32 gs_psm_ct16_block_address(const u32 fbw, const u32 block_index)
{
static const u32 block[GS_PSM_CT16_PAGE_ROWS][GS_PSM_CT16_PAGE_COLS] = {
{ 0, 2, 8, 10 },
{ 1, 3, 9, 11 },
{ 4, 6, 12, 14 },
{ 5, 7, 13, 15 },
{ 16, 18, 24, 26 },
{ 17, 19, 25, 27 },
{ 20, 22, 28, 30 },
{ 21, 23, 29, 31 }
};
const u32 fw = GS_PSM_CT16_PAGE_COLS * fbw;
const u32 fc = block_index % fw;
const u32 fr = block_index / fw;
const u32 bc = fc % GS_PSM_CT16_PAGE_COLS;
const u32 br = fr % GS_PSM_CT16_PAGE_ROWS;
const u32 pc = fc / GS_PSM_CT16_PAGE_COLS;
const u32 pr = fr / GS_PSM_CT16_PAGE_ROWS;
return GS_BLOCKS_PER_PAGE * (fbw * pr + pc) + block[br][bc];
}
EXPORT_SYMBOL_GPL(gs_psm_ct16_block_address);
/**
* gs_psm_ct32_block_address - 32-bit block address given a block index
* @fbw: buffer width/64
* @block_index: block index starting at the top left corner
*
* Return: block address for a given block index
*/
u32 gs_psm_ct32_block_address(const u32 fbw, const u32 block_index)
{
static const u32 block[GS_PSM_CT32_PAGE_ROWS][GS_PSM_CT32_PAGE_COLS] = {
{ 0, 1, 4, 5, 16, 17, 20, 21 },
{ 2, 3, 6, 7, 18, 19, 22, 23 },
{ 8, 9, 12, 13, 24, 25, 28, 29 },
{ 10, 11, 14, 15, 26, 27, 30, 31 }
};
const u32 fw = GS_PSM_CT32_PAGE_COLS * fbw;
const u32 fc = block_index % fw;
const u32 fr = block_index / fw;
const u32 bc = fc % GS_PSM_CT32_PAGE_COLS;
const u32 br = fr % GS_PSM_CT32_PAGE_ROWS;
const u32 pc = fc / GS_PSM_CT32_PAGE_COLS;
const u32 pr = fr / GS_PSM_CT32_PAGE_ROWS;
return GS_BLOCKS_PER_PAGE * (fbw * pr + pc) + block[br][bc];
}
EXPORT_SYMBOL_GPL(gs_psm_ct32_block_address);
static u32 div_round_ps(u32 a, u32 b)
{
return DIV_ROUND_CLOSEST_ULL(a * 1000000000000ll, b);
}
static u32 vck_to_pix_clock(const u32 vck, const u32 spml)
{
return div_round_ps(spml, vck);
}
/**
* gs_synch_gen_for_vck - determine video synchronization register fields for
* a given video clock
* @vck: video clock to compute video synchronization register fields for
*
* Some combinations of registers appear to generate equivalent video clock
* frequencies. For the standard ones, the combinations used by Sony are
* preferred and tabulated. For all others, a search is performed.
*
* Return: video synchronization register fields RC, LC, T1248 and SPML
**/
struct gs_synch_gen gs_synch_gen_for_vck(const u32 vck)
{
static const struct gs_synch_gen preferred[] = {
{ .spml = 2, .t1248 = 1, .lc = 15, .rc = 2 }, /* 50.625 MHz */
{ .spml = 2, .t1248 = 1, .lc = 32, .rc = 4 }, /* 54.000 MHz */
{ .spml = 4, .t1248 = 1, .lc = 32, .rc = 4 }, /* 54.000 MHz */
{ .spml = 2, .t1248 = 1, .lc = 28, .rc = 3 }, /* 63.000 MHz */
{ .spml = 1, .t1248 = 1, .lc = 22, .rc = 2 }, /* 74.250 MHz */
{ .spml = 1, .t1248 = 1, .lc = 35, .rc = 3 }, /* 78.750 MHz */
{ .spml = 2, .t1248 = 1, .lc = 71, .rc = 6 }, /* 79.875 MHz */
{ .spml = 2, .t1248 = 1, .lc = 44, .rc = 3 }, /* 99.000 MHz */
{ .spml = 1, .t1248 = 0, .lc = 8, .rc = 1 }, /* 108.000 MHz */
{ .spml = 2, .t1248 = 0, .lc = 58, .rc = 6 }, /* 130.500 MHz */
{ .spml = 1, .t1248 = 0, .lc = 10, .rc = 1 }, /* 135.000 MHz */
{ .spml = 1, .t1248 = 1, .lc = 22, .rc = 1 } /* 148.500 MHz */
};
struct gs_synch_gen sg = { };
u32 spml, t1248, lc, rc;
int best = -1;
int diff, i;
for (i = 0; i < ARRAY_SIZE(preferred); i++) {
spml = preferred[i].spml;
t1248 = preferred[i].t1248;
lc = preferred[i].lc;
rc = preferred[i].rc;
diff = abs(vck - vck_to_pix_clock(
gs_video_clock(t1248, lc, rc), spml));
if (best == -1 || diff < best) {
best = diff;
sg = (struct gs_synch_gen) {
.rc = rc,
.lc = lc,
.t1248 = t1248,
.spml = spml
};
}
}
for (spml = 1; spml < 5; spml++)
for (t1248 = 0; t1248 < 2; t1248++)
for (lc = 1; lc < 128; lc++)
for (rc = 1; rc < 7; rc++) {
diff = abs(vck - vck_to_pix_clock(
gs_video_clock(t1248, lc, rc), spml));
if (best == -1 || diff < best) {
best = diff;
sg = (struct gs_synch_gen) {
.rc = rc,
.lc = lc,
.t1248 = t1248,
.spml = spml
};
}
}
return sg;
}
EXPORT_SYMBOL_GPL(gs_synch_gen_for_vck);
/**
* gs_rfsh_from_synch_gen - DRAM refresh for the given synchronization registers
*
* Return: DRAM refresh register value
*/
u32 gs_rfsh_from_synch_gen(const struct gs_synch_gen sg)
{
const u32 pck = gs_video_clock(sg.t1248, sg.lc, sg.rc) / sg.spml;
return pck < 20000000 ? 8 :
pck < 70000000 ? 4 : 2;
}
EXPORT_SYMBOL_GPL(gs_rfsh_from_synch_gen);
struct device *gs_device_driver(void)
{
return gs_dev;
}
EXPORT_SYMBOL_GPL(gs_device_driver);
static int gs_probe(struct platform_device *pdev)
{
gs_dev = &pdev->dev;
gs_irq_init(); /* FIXME Errors? */
gif_reset(); /* FIXME Errors? */
#if 0
gs_reset();
setcrtc(1, 1);
#endif
return 0;
}
static int gs_remove(struct platform_device *pdev)
{
return 0;
}
static struct platform_driver gs_driver = {
.probe = gs_probe,
.remove = gs_remove,
.driver = {
.name = "gs",
},
};
static int __init gs_init(void)
{
return platform_driver_register(&gs_driver);
}
static void __exit gs_exit(void)
{
platform_driver_unregister(&gs_driver);
}
module_init(gs_init);
module_exit(gs_exit);
MODULE_DESCRIPTION("PlayStation 2 Graphics Synthesizer device driver");
MODULE_AUTHOR("Fredrik Noring");
MODULE_LICENSE("GPL");