1/*
2 * Copyright (C) 2010 Francisco Jerez.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 *
25 */
26#include "nv04.h"
27#include "fbmem.h"
28
29#include <subdev/bios.h>
30#include <subdev/bios/init.h>
31
32static void
33nv10_devinit_meminit(struct nvkm_devinit *devinit)
34{
35	struct nv04_devinit_priv *priv = (void *)devinit;
36	static const int mem_width[] = { 0x10, 0x00, 0x20 };
37	int mem_width_count;
38	uint32_t patt = 0xdeadbeef;
39	struct io_mapping *fb;
40	int i, j, k;
41
42	if (nv_device(priv)->card_type >= NV_11 &&
43	    nv_device(priv)->chipset >= 0x17)
44		mem_width_count = 3;
45	else
46		mem_width_count = 2;
47
48	/* Map the framebuffer aperture */
49	fb = fbmem_init(nv_device(priv));
50	if (!fb) {
51		nv_error(priv, "failed to map fb\n");
52		return;
53	}
54
55	nv_wr32(priv, NV10_PFB_REFCTRL, NV10_PFB_REFCTRL_VALID_1);
56
57	/* Probe memory bus width */
58	for (i = 0; i < mem_width_count; i++) {
59		nv_mask(priv, NV04_PFB_CFG0, 0x30, mem_width[i]);
60
61		for (j = 0; j < 4; j++) {
62			for (k = 0; k < 4; k++)
63				fbmem_poke(fb, 0x1c, 0);
64
65			fbmem_poke(fb, 0x1c, patt);
66			fbmem_poke(fb, 0x3c, 0);
67
68			if (fbmem_peek(fb, 0x1c) == patt)
69				goto mem_width_found;
70		}
71	}
72
73mem_width_found:
74	patt <<= 1;
75
76	/* Probe amount of installed memory */
77	for (i = 0; i < 4; i++) {
78		int off = nv_rd32(priv, 0x10020c) - 0x100000;
79
80		fbmem_poke(fb, off, patt);
81		fbmem_poke(fb, 0, 0);
82
83		fbmem_peek(fb, 0);
84		fbmem_peek(fb, 0);
85		fbmem_peek(fb, 0);
86		fbmem_peek(fb, 0);
87
88		if (fbmem_peek(fb, off) == patt)
89			goto amount_found;
90	}
91
92	/* IC missing - disable the upper half memory space. */
93	nv_mask(priv, NV04_PFB_CFG0, 0x1000, 0);
94
95amount_found:
96	fbmem_fini(fb);
97}
98
99struct nvkm_oclass *
100nv10_devinit_oclass = &(struct nvkm_devinit_impl) {
101	.base.handle = NV_SUBDEV(DEVINIT, 0x10),
102	.base.ofuncs = &(struct nvkm_ofuncs) {
103		.ctor = nv04_devinit_ctor,
104		.dtor = nv04_devinit_dtor,
105		.init = nv04_devinit_init,
106		.fini = nv04_devinit_fini,
107	},
108	.meminit = nv10_devinit_meminit,
109	.pll_set = nv04_devinit_pll_set,
110	.post = nvbios_init,
111}.base;
112