1/* $NetBSD: nouveau_subdev_fb_sddr3.c,v 1.1.1.1 2014/08/06 12:36:30 riastradh Exp $ */
2
3/*
4 * Copyright 2013 Red Hat Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: Ben Skeggs <bskeggs@redhat.com>
25 */
26
27#include <sys/cdefs.h>
28__KERNEL_RCSID(0, "$NetBSD: nouveau_subdev_fb_sddr3.c,v 1.1.1.1 2014/08/06 12:36:30 riastradh Exp $");
29
30#include <subdev/bios.h>
31#include "priv.h"
32
33struct ramxlat {
34 int id;
35 u8 enc;
36};
37
38static inline int
39ramxlat(const struct ramxlat *xlat, int id)
40{
41 while (xlat->id >= 0) {
42 if (xlat->id == id)
43 return xlat->enc;
44 xlat++;
45 }
46 return -EINVAL;
47}
48
49static const struct ramxlat
50ramddr3_cl[] = {
51 { 5, 2 }, { 6, 4 }, { 7, 6 }, { 8, 8 }, { 9, 10 }, { 10, 12 },
52 { 11, 14 },
53 /* the below are mentioned in some, but not all, ddr3 docs */
54 { 12, 1 }, { 13, 3 }, { 14, 5 },
55 { -1 }
56};
57
58static const struct ramxlat
59ramddr3_wr[] = {
60 { 5, 1 }, { 6, 2 }, { 7, 3 }, { 8, 4 }, { 10, 5 }, { 12, 6 },
61 /* the below are mentioned in some, but not all, ddr3 docs */
62 { 14, 7 }, { 16, 0 },
63 { -1 }
64};
65
66static const struct ramxlat
67ramddr3_cwl[] = {
68 { 5, 0 }, { 6, 1 }, { 7, 2 }, { 8, 3 },
69 /* the below are mentioned in some, but not all, ddr3 docs */
70 { 9, 4 },
71 { -1 }
72};
73
74int
75nouveau_sddr3_calc(struct nouveau_ram *ram)
76{
77 struct nouveau_bios *bios = nouveau_bios(ram);
78 int WL, CL, WR;
79
80 switch (!!ram->timing.data * ram->timing.version) {
81 case 0x20:
82 WL = (nv_ro16(bios, ram->timing.data + 0x04) & 0x0f80) >> 7;
83 CL = nv_ro08(bios, ram->timing.data + 0x04) & 0x1f;
84 WR = nv_ro08(bios, ram->timing.data + 0x0a) & 0x7f;
85 break;
86 default:
87 return -ENOSYS;
88 }
89
90 WL = ramxlat(ramddr3_cwl, WL);
91 CL = ramxlat(ramddr3_cl, CL);
92 WR = ramxlat(ramddr3_wr, WR);
93 if (WL < 0 || CL < 0 || WR < 0)
94 return -EINVAL;
95
96 ram->mr[0] &= ~0xe74;
97 ram->mr[0] |= (WR & 0x07) << 9;
98 ram->mr[0] |= (CL & 0x0e) << 3;
99 ram->mr[0] |= (CL & 0x01) << 2;
100
101 ram->mr[2] &= ~0x038;
102 ram->mr[2] |= (WL & 0x07) << 3;
103 return 0;
104}
105