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