Ben Skeggs | 330c598 | 2010-09-16 15:39:49 +1000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2010 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 |
| 23 | */ |
| 24 | |
| 25 | #include "drmP.h" |
| 26 | |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame^] | 27 | #include "nouveau_drm.h" |
Ben Skeggs | 330c598 | 2010-09-16 15:39:49 +1000 | [diff] [blame] | 28 | #include "nouveau_pm.h" |
| 29 | |
Ben Skeggs | e0996ae | 2012-07-10 12:20:17 +1000 | [diff] [blame] | 30 | #include <subdev/bios/gpio.h> |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame^] | 31 | #include <subdev/gpio.h> |
Ben Skeggs | e0996ae | 2012-07-10 12:20:17 +1000 | [diff] [blame] | 32 | |
| 33 | static const enum dcb_gpio_func_name vidtag[] = { 0x04, 0x05, 0x06, 0x1a, 0x73 }; |
Ben Skeggs | 330c598 | 2010-09-16 15:39:49 +1000 | [diff] [blame] | 34 | static int nr_vidtag = sizeof(vidtag) / sizeof(vidtag[0]); |
| 35 | |
| 36 | int |
| 37 | nouveau_voltage_gpio_get(struct drm_device *dev) |
| 38 | { |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame^] | 39 | struct nouveau_pm_voltage *volt = &nouveau_pm(dev)->voltage; |
| 40 | struct nouveau_device *device = nouveau_dev(dev); |
| 41 | struct nouveau_gpio *gpio = nouveau_gpio(device); |
Ben Skeggs | 330c598 | 2010-09-16 15:39:49 +1000 | [diff] [blame] | 42 | u8 vid = 0; |
| 43 | int i; |
| 44 | |
| 45 | for (i = 0; i < nr_vidtag; i++) { |
| 46 | if (!(volt->vid_mask & (1 << i))) |
| 47 | continue; |
| 48 | |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame^] | 49 | vid |= gpio->get(gpio, 0, vidtag[i], 0xff) << i; |
Ben Skeggs | 330c598 | 2010-09-16 15:39:49 +1000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | return nouveau_volt_lvl_lookup(dev, vid); |
| 53 | } |
| 54 | |
| 55 | int |
| 56 | nouveau_voltage_gpio_set(struct drm_device *dev, int voltage) |
| 57 | { |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame^] | 58 | struct nouveau_device *device = nouveau_dev(dev); |
| 59 | struct nouveau_gpio *gpio = nouveau_gpio(device); |
| 60 | struct nouveau_pm_voltage *volt = &nouveau_pm(dev)->voltage; |
Ben Skeggs | 330c598 | 2010-09-16 15:39:49 +1000 | [diff] [blame] | 61 | int vid, i; |
| 62 | |
| 63 | vid = nouveau_volt_vid_lookup(dev, voltage); |
| 64 | if (vid < 0) |
| 65 | return vid; |
| 66 | |
| 67 | for (i = 0; i < nr_vidtag; i++) { |
| 68 | if (!(volt->vid_mask & (1 << i))) |
| 69 | continue; |
| 70 | |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame^] | 71 | gpio->set(gpio, 0, vidtag[i], 0xff, !!(vid & (1 << i))); |
Ben Skeggs | 330c598 | 2010-09-16 15:39:49 +1000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | int |
| 78 | nouveau_volt_vid_lookup(struct drm_device *dev, int voltage) |
| 79 | { |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame^] | 80 | struct nouveau_pm_voltage *volt = &nouveau_pm(dev)->voltage; |
Ben Skeggs | 330c598 | 2010-09-16 15:39:49 +1000 | [diff] [blame] | 81 | int i; |
| 82 | |
| 83 | for (i = 0; i < volt->nr_level; i++) { |
| 84 | if (volt->level[i].voltage == voltage) |
| 85 | return volt->level[i].vid; |
| 86 | } |
| 87 | |
| 88 | return -ENOENT; |
| 89 | } |
| 90 | |
| 91 | int |
| 92 | nouveau_volt_lvl_lookup(struct drm_device *dev, int vid) |
| 93 | { |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame^] | 94 | struct nouveau_pm_voltage *volt = &nouveau_pm(dev)->voltage; |
Ben Skeggs | 330c598 | 2010-09-16 15:39:49 +1000 | [diff] [blame] | 95 | int i; |
| 96 | |
| 97 | for (i = 0; i < volt->nr_level; i++) { |
| 98 | if (volt->level[i].vid == vid) |
| 99 | return volt->level[i].voltage; |
| 100 | } |
| 101 | |
| 102 | return -ENOENT; |
| 103 | } |
| 104 | |
| 105 | void |
| 106 | nouveau_volt_init(struct drm_device *dev) |
| 107 | { |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame^] | 108 | struct nouveau_drm *drm = nouveau_drm(dev); |
| 109 | struct nouveau_gpio *gpio = nouveau_gpio(drm->device); |
| 110 | struct nouveau_pm *pm = nouveau_pm(dev); |
Ben Skeggs | 330c598 | 2010-09-16 15:39:49 +1000 | [diff] [blame] | 111 | struct nouveau_pm_voltage *voltage = &pm->voltage; |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame^] | 112 | struct nvbios *bios = &drm->vbios; |
| 113 | struct dcb_gpio_func func; |
Ben Skeggs | 330c598 | 2010-09-16 15:39:49 +1000 | [diff] [blame] | 114 | struct bit_entry P; |
| 115 | u8 *volt = NULL, *entry; |
Ben Skeggs | dc7339d | 2010-09-20 10:38:30 +1000 | [diff] [blame] | 116 | int i, headerlen, recordlen, entries, vidmask, vidshift; |
Ben Skeggs | 330c598 | 2010-09-16 15:39:49 +1000 | [diff] [blame] | 117 | |
| 118 | if (bios->type == NVBIOS_BIT) { |
| 119 | if (bit_table(dev, 'P', &P)) |
| 120 | return; |
| 121 | |
| 122 | if (P.version == 1) |
Ben Skeggs | f9f9f53 | 2011-10-12 16:48:48 +1000 | [diff] [blame] | 123 | volt = ROMPTR(dev, P.data[16]); |
Ben Skeggs | 330c598 | 2010-09-16 15:39:49 +1000 | [diff] [blame] | 124 | else |
| 125 | if (P.version == 2) |
Ben Skeggs | f9f9f53 | 2011-10-12 16:48:48 +1000 | [diff] [blame] | 126 | volt = ROMPTR(dev, P.data[12]); |
Ben Skeggs | 330c598 | 2010-09-16 15:39:49 +1000 | [diff] [blame] | 127 | else { |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame^] | 128 | NV_WARN(drm, "unknown volt for BIT P %d\n", P.version); |
Ben Skeggs | 330c598 | 2010-09-16 15:39:49 +1000 | [diff] [blame] | 129 | } |
| 130 | } else { |
| 131 | if (bios->data[bios->offset + 6] < 0x27) { |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame^] | 132 | NV_DEBUG(drm, "BMP version too old for voltage\n"); |
Ben Skeggs | 330c598 | 2010-09-16 15:39:49 +1000 | [diff] [blame] | 133 | return; |
| 134 | } |
| 135 | |
Ben Skeggs | f9f9f53 | 2011-10-12 16:48:48 +1000 | [diff] [blame] | 136 | volt = ROMPTR(dev, bios->data[bios->offset + 0x98]); |
Ben Skeggs | 330c598 | 2010-09-16 15:39:49 +1000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | if (!volt) { |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame^] | 140 | NV_DEBUG(drm, "voltage table pointer invalid\n"); |
Ben Skeggs | 330c598 | 2010-09-16 15:39:49 +1000 | [diff] [blame] | 141 | return; |
| 142 | } |
| 143 | |
| 144 | switch (volt[0]) { |
| 145 | case 0x10: |
| 146 | case 0x11: |
| 147 | case 0x12: |
Ben Skeggs | dc7339d | 2010-09-20 10:38:30 +1000 | [diff] [blame] | 148 | headerlen = 5; |
| 149 | recordlen = volt[1]; |
Ben Skeggs | 330c598 | 2010-09-16 15:39:49 +1000 | [diff] [blame] | 150 | entries = volt[2]; |
| 151 | vidshift = 0; |
| 152 | vidmask = volt[4]; |
| 153 | break; |
| 154 | case 0x20: |
Ben Skeggs | dc7339d | 2010-09-20 10:38:30 +1000 | [diff] [blame] | 155 | headerlen = volt[1]; |
Ben Skeggs | 330c598 | 2010-09-16 15:39:49 +1000 | [diff] [blame] | 156 | recordlen = volt[3]; |
| 157 | entries = volt[2]; |
| 158 | vidshift = 0; /* could be vidshift like 0x30? */ |
| 159 | vidmask = volt[5]; |
| 160 | break; |
| 161 | case 0x30: |
Ben Skeggs | dc7339d | 2010-09-20 10:38:30 +1000 | [diff] [blame] | 162 | headerlen = volt[1]; |
Ben Skeggs | 330c598 | 2010-09-16 15:39:49 +1000 | [diff] [blame] | 163 | recordlen = volt[2]; |
| 164 | entries = volt[3]; |
Ben Skeggs | 330c598 | 2010-09-16 15:39:49 +1000 | [diff] [blame] | 165 | vidmask = volt[4]; |
Ben Skeggs | 475feff | 2011-03-31 10:39:44 +1000 | [diff] [blame] | 166 | /* no longer certain what volt[5] is, if it's related to |
| 167 | * the vid shift then it's definitely not a function of |
| 168 | * how many bits are set. |
| 169 | * |
| 170 | * after looking at a number of nva3+ vbios images, they |
| 171 | * all seem likely to have a static shift of 2.. lets |
| 172 | * go with that for now until proven otherwise. |
| 173 | */ |
| 174 | vidshift = 2; |
Ben Skeggs | 330c598 | 2010-09-16 15:39:49 +1000 | [diff] [blame] | 175 | break; |
Ben Skeggs | f60dfb9 | 2011-06-09 16:16:38 +1000 | [diff] [blame] | 176 | case 0x40: |
| 177 | headerlen = volt[1]; |
| 178 | recordlen = volt[2]; |
| 179 | entries = volt[3]; /* not a clue what the entries are for.. */ |
| 180 | vidmask = volt[11]; /* guess.. */ |
| 181 | vidshift = 0; |
| 182 | break; |
Ben Skeggs | 330c598 | 2010-09-16 15:39:49 +1000 | [diff] [blame] | 183 | default: |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame^] | 184 | NV_WARN(drm, "voltage table 0x%02x unknown\n", volt[0]); |
Ben Skeggs | 330c598 | 2010-09-16 15:39:49 +1000 | [diff] [blame] | 185 | return; |
| 186 | } |
| 187 | |
| 188 | /* validate vid mask */ |
| 189 | voltage->vid_mask = vidmask; |
| 190 | if (!voltage->vid_mask) |
| 191 | return; |
| 192 | |
| 193 | i = 0; |
| 194 | while (vidmask) { |
| 195 | if (i > nr_vidtag) { |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame^] | 196 | NV_DEBUG(drm, "vid bit %d unknown\n", i); |
Ben Skeggs | 330c598 | 2010-09-16 15:39:49 +1000 | [diff] [blame] | 197 | return; |
| 198 | } |
| 199 | |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame^] | 200 | if (gpio && gpio->find(gpio, 0, vidtag[i], 0xff, &func)) { |
| 201 | NV_DEBUG(drm, "vid bit %d has no gpio tag\n", i); |
Ben Skeggs | 330c598 | 2010-09-16 15:39:49 +1000 | [diff] [blame] | 202 | return; |
| 203 | } |
| 204 | |
| 205 | vidmask >>= 1; |
| 206 | i++; |
| 207 | } |
| 208 | |
| 209 | /* parse vbios entries into common format */ |
Ben Skeggs | 03ce8d9 | 2011-06-10 15:33:11 +1000 | [diff] [blame] | 210 | voltage->version = volt[0]; |
| 211 | if (voltage->version < 0x40) { |
Ben Skeggs | f60dfb9 | 2011-06-09 16:16:38 +1000 | [diff] [blame] | 212 | voltage->nr_level = entries; |
| 213 | voltage->level = |
| 214 | kcalloc(entries, sizeof(*voltage->level), GFP_KERNEL); |
| 215 | if (!voltage->level) |
| 216 | return; |
Ben Skeggs | 330c598 | 2010-09-16 15:39:49 +1000 | [diff] [blame] | 217 | |
Ben Skeggs | f60dfb9 | 2011-06-09 16:16:38 +1000 | [diff] [blame] | 218 | entry = volt + headerlen; |
| 219 | for (i = 0; i < entries; i++, entry += recordlen) { |
| 220 | voltage->level[i].voltage = entry[0] * 10000; |
| 221 | voltage->level[i].vid = entry[1] >> vidshift; |
| 222 | } |
| 223 | } else { |
| 224 | u32 volt_uv = ROM32(volt[4]); |
| 225 | s16 step_uv = ROM16(volt[8]); |
| 226 | u8 vid; |
| 227 | |
| 228 | voltage->nr_level = voltage->vid_mask + 1; |
| 229 | voltage->level = kcalloc(voltage->nr_level, |
| 230 | sizeof(*voltage->level), GFP_KERNEL); |
| 231 | if (!voltage->level) |
| 232 | return; |
| 233 | |
| 234 | for (vid = 0; vid <= voltage->vid_mask; vid++) { |
| 235 | voltage->level[vid].voltage = volt_uv; |
| 236 | voltage->level[vid].vid = vid; |
| 237 | volt_uv += step_uv; |
| 238 | } |
Ben Skeggs | 330c598 | 2010-09-16 15:39:49 +1000 | [diff] [blame] | 239 | } |
Ben Skeggs | f60dfb9 | 2011-06-09 16:16:38 +1000 | [diff] [blame] | 240 | |
Ben Skeggs | 330c598 | 2010-09-16 15:39:49 +1000 | [diff] [blame] | 241 | voltage->supported = true; |
| 242 | } |
| 243 | |
| 244 | void |
| 245 | nouveau_volt_fini(struct drm_device *dev) |
| 246 | { |
Ben Skeggs | 77145f1 | 2012-07-31 16:16:21 +1000 | [diff] [blame^] | 247 | struct nouveau_pm_voltage *volt = &nouveau_pm(dev)->voltage; |
Ben Skeggs | 330c598 | 2010-09-16 15:39:49 +1000 | [diff] [blame] | 248 | |
| 249 | kfree(volt->level); |
| 250 | } |