Path: blob/master/drivers/gpu/drm/nouveau/nouveau_dp.c
15112 views
/*1* Copyright 2009 Red Hat Inc.2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice shall be included in11* all copies or substantial portions of the Software.12*13* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR14* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL16* THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR17* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,18* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR19* OTHER DEALINGS IN THE SOFTWARE.20*21* Authors: Ben Skeggs22*/2324#include "drmP.h"2526#include "nouveau_drv.h"27#include "nouveau_i2c.h"28#include "nouveau_connector.h"29#include "nouveau_encoder.h"3031static int32auxch_rd(struct drm_encoder *encoder, int address, uint8_t *buf, int size)33{34struct drm_device *dev = encoder->dev;35struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);36struct nouveau_i2c_chan *auxch;37int ret;3839auxch = nouveau_i2c_find(dev, nv_encoder->dcb->i2c_index);40if (!auxch)41return -ENODEV;4243ret = nouveau_dp_auxch(auxch, 9, address, buf, size);44if (ret)45return ret;4647return 0;48}4950static int51auxch_wr(struct drm_encoder *encoder, int address, uint8_t *buf, int size)52{53struct drm_device *dev = encoder->dev;54struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);55struct nouveau_i2c_chan *auxch;56int ret;5758auxch = nouveau_i2c_find(dev, nv_encoder->dcb->i2c_index);59if (!auxch)60return -ENODEV;6162ret = nouveau_dp_auxch(auxch, 8, address, buf, size);63return ret;64}6566static int67nouveau_dp_lane_count_set(struct drm_encoder *encoder, uint8_t cmd)68{69struct drm_device *dev = encoder->dev;70struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);71uint32_t tmp;72int or = nv_encoder->or, link = !(nv_encoder->dcb->sorconf.link & 1);7374tmp = nv_rd32(dev, NV50_SOR_DP_CTRL(or, link));75tmp &= ~(NV50_SOR_DP_CTRL_ENHANCED_FRAME_ENABLED |76NV50_SOR_DP_CTRL_LANE_MASK);77tmp |= ((1 << (cmd & DP_LANE_COUNT_MASK)) - 1) << 16;78if (cmd & DP_LANE_COUNT_ENHANCED_FRAME_EN)79tmp |= NV50_SOR_DP_CTRL_ENHANCED_FRAME_ENABLED;80nv_wr32(dev, NV50_SOR_DP_CTRL(or, link), tmp);8182return auxch_wr(encoder, DP_LANE_COUNT_SET, &cmd, 1);83}8485static int86nouveau_dp_link_bw_set(struct drm_encoder *encoder, uint8_t cmd)87{88struct drm_device *dev = encoder->dev;89struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);90uint32_t tmp;91int reg = 0x614300 + (nv_encoder->or * 0x800);9293tmp = nv_rd32(dev, reg);94tmp &= 0xfff3ffff;95if (cmd == DP_LINK_BW_2_7)96tmp |= 0x00040000;97nv_wr32(dev, reg, tmp);9899return auxch_wr(encoder, DP_LINK_BW_SET, &cmd, 1);100}101102static int103nouveau_dp_link_train_set(struct drm_encoder *encoder, int pattern)104{105struct drm_device *dev = encoder->dev;106struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);107uint32_t tmp;108uint8_t cmd;109int or = nv_encoder->or, link = !(nv_encoder->dcb->sorconf.link & 1);110int ret;111112tmp = nv_rd32(dev, NV50_SOR_DP_CTRL(or, link));113tmp &= ~NV50_SOR_DP_CTRL_TRAINING_PATTERN;114tmp |= (pattern << 24);115nv_wr32(dev, NV50_SOR_DP_CTRL(or, link), tmp);116117ret = auxch_rd(encoder, DP_TRAINING_PATTERN_SET, &cmd, 1);118if (ret)119return ret;120cmd &= ~DP_TRAINING_PATTERN_MASK;121cmd |= (pattern & DP_TRAINING_PATTERN_MASK);122return auxch_wr(encoder, DP_TRAINING_PATTERN_SET, &cmd, 1);123}124125static int126nouveau_dp_max_voltage_swing(struct drm_encoder *encoder)127{128struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);129struct drm_device *dev = encoder->dev;130struct bit_displayport_encoder_table_entry *dpse;131struct bit_displayport_encoder_table *dpe;132int i, dpe_headerlen, max_vs = 0;133134dpe = nouveau_bios_dp_table(dev, nv_encoder->dcb, &dpe_headerlen);135if (!dpe)136return false;137dpse = (void *)((char *)dpe + dpe_headerlen);138139for (i = 0; i < dpe_headerlen; i++, dpse++) {140if (dpse->vs_level > max_vs)141max_vs = dpse->vs_level;142}143144return max_vs;145}146147static int148nouveau_dp_max_pre_emphasis(struct drm_encoder *encoder, int vs)149{150struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);151struct drm_device *dev = encoder->dev;152struct bit_displayport_encoder_table_entry *dpse;153struct bit_displayport_encoder_table *dpe;154int i, dpe_headerlen, max_pre = 0;155156dpe = nouveau_bios_dp_table(dev, nv_encoder->dcb, &dpe_headerlen);157if (!dpe)158return false;159dpse = (void *)((char *)dpe + dpe_headerlen);160161for (i = 0; i < dpe_headerlen; i++, dpse++) {162if (dpse->vs_level != vs)163continue;164165if (dpse->pre_level > max_pre)166max_pre = dpse->pre_level;167}168169return max_pre;170}171172static bool173nouveau_dp_link_train_adjust(struct drm_encoder *encoder, uint8_t *config)174{175struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);176struct drm_device *dev = encoder->dev;177struct bit_displayport_encoder_table *dpe;178int ret, i, dpe_headerlen, vs = 0, pre = 0;179uint8_t request[2];180181dpe = nouveau_bios_dp_table(dev, nv_encoder->dcb, &dpe_headerlen);182if (!dpe)183return false;184185ret = auxch_rd(encoder, DP_ADJUST_REQUEST_LANE0_1, request, 2);186if (ret)187return false;188189NV_DEBUG_KMS(dev, "\t\tadjust 0x%02x 0x%02x\n", request[0], request[1]);190191/* Keep all lanes at the same level.. */192for (i = 0; i < nv_encoder->dp.link_nr; i++) {193int lane_req = (request[i >> 1] >> ((i & 1) << 2)) & 0xf;194int lane_vs = lane_req & 3;195int lane_pre = (lane_req >> 2) & 3;196197if (lane_vs > vs)198vs = lane_vs;199if (lane_pre > pre)200pre = lane_pre;201}202203if (vs >= nouveau_dp_max_voltage_swing(encoder)) {204vs = nouveau_dp_max_voltage_swing(encoder);205vs |= 4;206}207208if (pre >= nouveau_dp_max_pre_emphasis(encoder, vs & 3)) {209pre = nouveau_dp_max_pre_emphasis(encoder, vs & 3);210pre |= 4;211}212213/* Update the configuration for all lanes.. */214for (i = 0; i < nv_encoder->dp.link_nr; i++)215config[i] = (pre << 3) | vs;216217return true;218}219220static bool221nouveau_dp_link_train_commit(struct drm_encoder *encoder, uint8_t *config)222{223struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);224struct drm_device *dev = encoder->dev;225struct bit_displayport_encoder_table_entry *dpse;226struct bit_displayport_encoder_table *dpe;227int or = nv_encoder->or, link = !(nv_encoder->dcb->sorconf.link & 1);228int dpe_headerlen, ret, i;229230NV_DEBUG_KMS(dev, "\t\tconfig 0x%02x 0x%02x 0x%02x 0x%02x\n",231config[0], config[1], config[2], config[3]);232233dpe = nouveau_bios_dp_table(dev, nv_encoder->dcb, &dpe_headerlen);234if (!dpe)235return false;236dpse = (void *)((char *)dpe + dpe_headerlen);237238for (i = 0; i < dpe->record_nr; i++, dpse++) {239if (dpse->vs_level == (config[0] & 3) &&240dpse->pre_level == ((config[0] >> 3) & 3))241break;242}243BUG_ON(i == dpe->record_nr);244245for (i = 0; i < nv_encoder->dp.link_nr; i++) {246const int shift[4] = { 16, 8, 0, 24 };247uint32_t mask = 0xff << shift[i];248uint32_t reg0, reg1, reg2;249250reg0 = nv_rd32(dev, NV50_SOR_DP_UNK118(or, link)) & ~mask;251reg0 |= (dpse->reg0 << shift[i]);252reg1 = nv_rd32(dev, NV50_SOR_DP_UNK120(or, link)) & ~mask;253reg1 |= (dpse->reg1 << shift[i]);254reg2 = nv_rd32(dev, NV50_SOR_DP_UNK130(or, link)) & 0xffff00ff;255reg2 |= (dpse->reg2 << 8);256nv_wr32(dev, NV50_SOR_DP_UNK118(or, link), reg0);257nv_wr32(dev, NV50_SOR_DP_UNK120(or, link), reg1);258nv_wr32(dev, NV50_SOR_DP_UNK130(or, link), reg2);259}260261ret = auxch_wr(encoder, DP_TRAINING_LANE0_SET, config, 4);262if (ret)263return false;264265return true;266}267268bool269nouveau_dp_link_train(struct drm_encoder *encoder)270{271struct drm_device *dev = encoder->dev;272struct drm_nouveau_private *dev_priv = dev->dev_private;273struct nouveau_gpio_engine *pgpio = &dev_priv->engine.gpio;274struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);275struct nouveau_connector *nv_connector;276struct bit_displayport_encoder_table *dpe;277int dpe_headerlen;278uint8_t config[4], status[3];279bool cr_done, cr_max_vs, eq_done, hpd_state;280int ret = 0, i, tries, voltage;281282NV_DEBUG_KMS(dev, "link training!!\n");283284nv_connector = nouveau_encoder_connector_get(nv_encoder);285if (!nv_connector)286return false;287288dpe = nouveau_bios_dp_table(dev, nv_encoder->dcb, &dpe_headerlen);289if (!dpe) {290NV_ERROR(dev, "SOR-%d: no DP encoder table!\n", nv_encoder->or);291return false;292}293294/* disable hotplug detect, this flips around on some panels during295* link training.296*/297hpd_state = pgpio->irq_enable(dev, nv_connector->dcb->gpio_tag, false);298299if (dpe->script0) {300NV_DEBUG_KMS(dev, "SOR-%d: running DP script 0\n", nv_encoder->or);301nouveau_bios_run_init_table(dev, le16_to_cpu(dpe->script0),302nv_encoder->dcb);303}304305train:306cr_done = eq_done = false;307308/* set link configuration */309NV_DEBUG_KMS(dev, "\tbegin train: bw %d, lanes %d\n",310nv_encoder->dp.link_bw, nv_encoder->dp.link_nr);311312ret = nouveau_dp_link_bw_set(encoder, nv_encoder->dp.link_bw);313if (ret)314return false;315316config[0] = nv_encoder->dp.link_nr;317if (nv_encoder->dp.dpcd_version >= 0x11 &&318nv_encoder->dp.enhanced_frame)319config[0] |= DP_LANE_COUNT_ENHANCED_FRAME_EN;320321ret = nouveau_dp_lane_count_set(encoder, config[0]);322if (ret)323return false;324325/* clock recovery */326NV_DEBUG_KMS(dev, "\tbegin cr\n");327ret = nouveau_dp_link_train_set(encoder, DP_TRAINING_PATTERN_1);328if (ret)329goto stop;330331tries = 0;332voltage = -1;333memset(config, 0x00, sizeof(config));334for (;;) {335if (!nouveau_dp_link_train_commit(encoder, config))336break;337338udelay(100);339340ret = auxch_rd(encoder, DP_LANE0_1_STATUS, status, 2);341if (ret)342break;343NV_DEBUG_KMS(dev, "\t\tstatus: 0x%02x 0x%02x\n",344status[0], status[1]);345346cr_done = true;347cr_max_vs = false;348for (i = 0; i < nv_encoder->dp.link_nr; i++) {349int lane = (status[i >> 1] >> ((i & 1) * 4)) & 0xf;350351if (!(lane & DP_LANE_CR_DONE)) {352cr_done = false;353if (config[i] & DP_TRAIN_MAX_PRE_EMPHASIS_REACHED)354cr_max_vs = true;355break;356}357}358359if ((config[0] & DP_TRAIN_VOLTAGE_SWING_MASK) != voltage) {360voltage = config[0] & DP_TRAIN_VOLTAGE_SWING_MASK;361tries = 0;362}363364if (cr_done || cr_max_vs || (++tries == 5))365break;366367if (!nouveau_dp_link_train_adjust(encoder, config))368break;369}370371if (!cr_done)372goto stop;373374/* channel equalisation */375NV_DEBUG_KMS(dev, "\tbegin eq\n");376ret = nouveau_dp_link_train_set(encoder, DP_TRAINING_PATTERN_2);377if (ret)378goto stop;379380for (tries = 0; tries <= 5; tries++) {381udelay(400);382383ret = auxch_rd(encoder, DP_LANE0_1_STATUS, status, 3);384if (ret)385break;386NV_DEBUG_KMS(dev, "\t\tstatus: 0x%02x 0x%02x\n",387status[0], status[1]);388389eq_done = true;390if (!(status[2] & DP_INTERLANE_ALIGN_DONE))391eq_done = false;392393for (i = 0; eq_done && i < nv_encoder->dp.link_nr; i++) {394int lane = (status[i >> 1] >> ((i & 1) * 4)) & 0xf;395396if (!(lane & DP_LANE_CR_DONE)) {397cr_done = false;398break;399}400401if (!(lane & DP_LANE_CHANNEL_EQ_DONE) ||402!(lane & DP_LANE_SYMBOL_LOCKED)) {403eq_done = false;404break;405}406}407408if (eq_done || !cr_done)409break;410411if (!nouveau_dp_link_train_adjust(encoder, config) ||412!nouveau_dp_link_train_commit(encoder, config))413break;414}415416stop:417/* end link training */418ret = nouveau_dp_link_train_set(encoder, DP_TRAINING_PATTERN_DISABLE);419if (ret)420return false;421422/* retry at a lower setting, if possible */423if (!ret && !(eq_done && cr_done)) {424NV_DEBUG_KMS(dev, "\twe failed\n");425if (nv_encoder->dp.link_bw != DP_LINK_BW_1_62) {426NV_DEBUG_KMS(dev, "retry link training at low rate\n");427nv_encoder->dp.link_bw = DP_LINK_BW_1_62;428goto train;429}430}431432if (dpe->script1) {433NV_DEBUG_KMS(dev, "SOR-%d: running DP script 1\n", nv_encoder->or);434nouveau_bios_run_init_table(dev, le16_to_cpu(dpe->script1),435nv_encoder->dcb);436}437438/* re-enable hotplug detect */439pgpio->irq_enable(dev, nv_connector->dcb->gpio_tag, hpd_state);440441return eq_done;442}443444bool445nouveau_dp_detect(struct drm_encoder *encoder)446{447struct nouveau_encoder *nv_encoder = nouveau_encoder(encoder);448struct drm_device *dev = encoder->dev;449uint8_t dpcd[4];450int ret;451452ret = auxch_rd(encoder, 0x0000, dpcd, 4);453if (ret)454return false;455456NV_DEBUG_KMS(dev, "encoder: link_bw %d, link_nr %d\n"457"display: link_bw %d, link_nr %d version 0x%02x\n",458nv_encoder->dcb->dpconf.link_bw,459nv_encoder->dcb->dpconf.link_nr,460dpcd[1], dpcd[2] & 0x0f, dpcd[0]);461462nv_encoder->dp.dpcd_version = dpcd[0];463464nv_encoder->dp.link_bw = dpcd[1];465if (nv_encoder->dp.link_bw != DP_LINK_BW_1_62 &&466!nv_encoder->dcb->dpconf.link_bw)467nv_encoder->dp.link_bw = DP_LINK_BW_1_62;468469nv_encoder->dp.link_nr = dpcd[2] & DP_MAX_LANE_COUNT_MASK;470if (nv_encoder->dp.link_nr > nv_encoder->dcb->dpconf.link_nr)471nv_encoder->dp.link_nr = nv_encoder->dcb->dpconf.link_nr;472473nv_encoder->dp.enhanced_frame = (dpcd[2] & DP_ENHANCED_FRAME_CAP);474475return true;476}477478int479nouveau_dp_auxch(struct nouveau_i2c_chan *auxch, int cmd, int addr,480uint8_t *data, int data_nr)481{482struct drm_device *dev = auxch->dev;483uint32_t tmp, ctrl, stat = 0, data32[4] = {};484int ret = 0, i, index = auxch->rd;485486NV_DEBUG_KMS(dev, "ch %d cmd %d addr 0x%x len %d\n", index, cmd, addr, data_nr);487488tmp = nv_rd32(dev, NV50_AUXCH_CTRL(auxch->rd));489nv_wr32(dev, NV50_AUXCH_CTRL(auxch->rd), tmp | 0x00100000);490tmp = nv_rd32(dev, NV50_AUXCH_CTRL(auxch->rd));491if (!(tmp & 0x01000000)) {492NV_ERROR(dev, "expected bit 24 == 1, got 0x%08x\n", tmp);493ret = -EIO;494goto out;495}496497for (i = 0; i < 3; i++) {498tmp = nv_rd32(dev, NV50_AUXCH_STAT(auxch->rd));499if (tmp & NV50_AUXCH_STAT_STATE_READY)500break;501udelay(100);502}503504if (i == 3) {505ret = -EBUSY;506goto out;507}508509if (!(cmd & 1)) {510memcpy(data32, data, data_nr);511for (i = 0; i < 4; i++) {512NV_DEBUG_KMS(dev, "wr %d: 0x%08x\n", i, data32[i]);513nv_wr32(dev, NV50_AUXCH_DATA_OUT(index, i), data32[i]);514}515}516517nv_wr32(dev, NV50_AUXCH_ADDR(index), addr);518ctrl = nv_rd32(dev, NV50_AUXCH_CTRL(index));519ctrl &= ~(NV50_AUXCH_CTRL_CMD | NV50_AUXCH_CTRL_LEN);520ctrl |= (cmd << NV50_AUXCH_CTRL_CMD_SHIFT);521ctrl |= ((data_nr - 1) << NV50_AUXCH_CTRL_LEN_SHIFT);522523for (i = 0; i < 16; i++) {524nv_wr32(dev, NV50_AUXCH_CTRL(index), ctrl | 0x80000000);525nv_wr32(dev, NV50_AUXCH_CTRL(index), ctrl);526nv_wr32(dev, NV50_AUXCH_CTRL(index), ctrl | 0x00010000);527if (!nv_wait(dev, NV50_AUXCH_CTRL(index),5280x00010000, 0x00000000)) {529NV_ERROR(dev, "expected bit 16 == 0, got 0x%08x\n",530nv_rd32(dev, NV50_AUXCH_CTRL(index)));531ret = -EBUSY;532goto out;533}534535udelay(400);536537stat = nv_rd32(dev, NV50_AUXCH_STAT(index));538if ((stat & NV50_AUXCH_STAT_REPLY_AUX) !=539NV50_AUXCH_STAT_REPLY_AUX_DEFER)540break;541}542543if (i == 16) {544NV_ERROR(dev, "auxch DEFER too many times, bailing\n");545ret = -EREMOTEIO;546goto out;547}548549if (cmd & 1) {550if ((stat & NV50_AUXCH_STAT_COUNT) != data_nr) {551ret = -EREMOTEIO;552goto out;553}554555for (i = 0; i < 4; i++) {556data32[i] = nv_rd32(dev, NV50_AUXCH_DATA_IN(index, i));557NV_DEBUG_KMS(dev, "rd %d: 0x%08x\n", i, data32[i]);558}559memcpy(data, data32, data_nr);560}561562out:563tmp = nv_rd32(dev, NV50_AUXCH_CTRL(auxch->rd));564nv_wr32(dev, NV50_AUXCH_CTRL(auxch->rd), tmp & ~0x00100000);565tmp = nv_rd32(dev, NV50_AUXCH_CTRL(auxch->rd));566if (tmp & 0x01000000) {567NV_ERROR(dev, "expected bit 24 == 0, got 0x%08x\n", tmp);568ret = -EIO;569}570571udelay(400);572573return ret ? ret : (stat & NV50_AUXCH_STAT_REPLY);574}575576static int577nouveau_dp_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num)578{579struct nouveau_i2c_chan *auxch = (struct nouveau_i2c_chan *)adap;580struct drm_device *dev = auxch->dev;581struct i2c_msg *msg = msgs;582int ret, mcnt = num;583584while (mcnt--) {585u8 remaining = msg->len;586u8 *ptr = msg->buf;587588while (remaining) {589u8 cnt = (remaining > 16) ? 16 : remaining;590u8 cmd;591592if (msg->flags & I2C_M_RD)593cmd = AUX_I2C_READ;594else595cmd = AUX_I2C_WRITE;596597if (mcnt || remaining > 16)598cmd |= AUX_I2C_MOT;599600ret = nouveau_dp_auxch(auxch, cmd, msg->addr, ptr, cnt);601if (ret < 0)602return ret;603604switch (ret & NV50_AUXCH_STAT_REPLY_I2C) {605case NV50_AUXCH_STAT_REPLY_I2C_ACK:606break;607case NV50_AUXCH_STAT_REPLY_I2C_NACK:608return -EREMOTEIO;609case NV50_AUXCH_STAT_REPLY_I2C_DEFER:610udelay(100);611continue;612default:613NV_ERROR(dev, "bad auxch reply: 0x%08x\n", ret);614return -EREMOTEIO;615}616617ptr += cnt;618remaining -= cnt;619}620621msg++;622}623624return num;625}626627static u32628nouveau_dp_i2c_func(struct i2c_adapter *adap)629{630return I2C_FUNC_I2C | I2C_FUNC_SMBUS_EMUL;631}632633const struct i2c_algorithm nouveau_dp_i2c_algo = {634.master_xfer = nouveau_dp_i2c_xfer,635.functionality = nouveau_dp_i2c_func636};637638639