uboot/arch/arm/cpu/armv7/tegra2/emc.c
<<
>>
Prefs
   1/*
   2 * Copyright (c) 2011 The Chromium OS Authors.
   3 *
   4 * See file CREDITS for list of people who contributed to this
   5 * project.
   6 *
   7 * This program is free software; you can redistribute it and/or
   8 * modify it under the terms of the GNU General Public License as
   9 * published by the Free Software Foundation; either version 2 of
  10 * the License, or (at your option) any later version.
  11 *
  12 * This program is distributed in the hope that it will be useful,
  13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15 * GNU General Public License for more details.
  16 *
  17 * You should have received a copy of the GNU General Public License
  18 * along with this program; if not, write to the Free Software
  19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20 * MA 02111-1307 USA
  21 */
  22
  23#include <common.h>
  24#include <fdtdec.h>
  25#include <asm/io.h>
  26#include <asm/arch/ap20.h>
  27#include <asm/arch/apb_misc.h>
  28#include <asm/arch/clock.h>
  29#include <asm/arch/emc.h>
  30#include <asm/arch/tegra2.h>
  31
  32/*
  33 * The EMC registers have shadow registers.  When the EMC clock is updated
  34 * in the clock controller, the shadow registers are copied to the active
  35 * registers, allowing glitchless memory bus frequency changes.
  36 * This function updates the shadow registers for a new clock frequency,
  37 * and relies on the clock lock on the emc clock to avoid races between
  38 * multiple frequency changes
  39 */
  40
  41/*
  42 * This table defines the ordering of the registers provided to
  43 * tegra_set_mmc()
  44 * TODO: Convert to fdt version once available
  45 */
  46static const unsigned long emc_reg_addr[TEGRA_EMC_NUM_REGS] = {
  47        0x2c,   /* RC */
  48        0x30,   /* RFC */
  49        0x34,   /* RAS */
  50        0x38,   /* RP */
  51        0x3c,   /* R2W */
  52        0x40,   /* W2R */
  53        0x44,   /* R2P */
  54        0x48,   /* W2P */
  55        0x4c,   /* RD_RCD */
  56        0x50,   /* WR_RCD */
  57        0x54,   /* RRD */
  58        0x58,   /* REXT */
  59        0x5c,   /* WDV */
  60        0x60,   /* QUSE */
  61        0x64,   /* QRST */
  62        0x68,   /* QSAFE */
  63        0x6c,   /* RDV */
  64        0x70,   /* REFRESH */
  65        0x74,   /* BURST_REFRESH_NUM */
  66        0x78,   /* PDEX2WR */
  67        0x7c,   /* PDEX2RD */
  68        0x80,   /* PCHG2PDEN */
  69        0x84,   /* ACT2PDEN */
  70        0x88,   /* AR2PDEN */
  71        0x8c,   /* RW2PDEN */
  72        0x90,   /* TXSR */
  73        0x94,   /* TCKE */
  74        0x98,   /* TFAW */
  75        0x9c,   /* TRPAB */
  76        0xa0,   /* TCLKSTABLE */
  77        0xa4,   /* TCLKSTOP */
  78        0xa8,   /* TREFBW */
  79        0xac,   /* QUSE_EXTRA */
  80        0x114,  /* FBIO_CFG6 */
  81        0xb0,   /* ODT_WRITE */
  82        0xb4,   /* ODT_READ */
  83        0x104,  /* FBIO_CFG5 */
  84        0x2bc,  /* CFG_DIG_DLL */
  85        0x2c0,  /* DLL_XFORM_DQS */
  86        0x2c4,  /* DLL_XFORM_QUSE */
  87        0x2e0,  /* ZCAL_REF_CNT */
  88        0x2e4,  /* ZCAL_WAIT_CNT */
  89        0x2a8,  /* AUTO_CAL_INTERVAL */
  90        0x2d0,  /* CFG_CLKTRIM_0 */
  91        0x2d4,  /* CFG_CLKTRIM_1 */
  92        0x2d8,  /* CFG_CLKTRIM_2 */
  93};
  94
  95struct emc_ctlr *emc_get_controller(const void *blob)
  96{
  97        fdt_addr_t addr;
  98        int node;
  99
 100        node = fdtdec_next_compatible(blob, 0, COMPAT_NVIDIA_TEGRA20_EMC);
 101        if (node > 0) {
 102                addr = fdtdec_get_addr(blob, node, "reg");
 103                if (addr != FDT_ADDR_T_NONE)
 104                        return (struct emc_ctlr *)addr;
 105        }
 106        return NULL;
 107}
 108
 109/* Error codes we use */
 110enum {
 111        ERR_NO_EMC_NODE = -10,
 112        ERR_NO_EMC_REG,
 113        ERR_NO_FREQ,
 114        ERR_FREQ_NOT_FOUND,
 115        ERR_BAD_REGS,
 116        ERR_NO_RAM_CODE,
 117        ERR_RAM_CODE_NOT_FOUND,
 118};
 119
 120/**
 121 * Find EMC tables for the given ram code.
 122 *
 123 * The tegra EMC binding has two options, one using the ram code and one not.
 124 * We detect which is in use by looking for the nvidia,use-ram-code property.
 125 * If this is not present, then the EMC tables are directly below 'node',
 126 * otherwise we select the correct emc-tables subnode based on the 'ram_code'
 127 * value.
 128 *
 129 * @param blob          Device tree blob
 130 * @param node          EMC node (nvidia,tegra20-emc compatible string)
 131 * @param ram_code      RAM code to select (0-3, or -1 if unknown)
 132 * @return 0 if ok, otherwise a -ve ERR_ code (see enum above)
 133 */
 134static int find_emc_tables(const void *blob, int node, int ram_code)
 135{
 136        int need_ram_code;
 137        int depth;
 138        int offset;
 139
 140        /* If we are using RAM codes, scan through the tables for our code */
 141        need_ram_code = fdtdec_get_bool(blob, node, "nvidia,use-ram-code");
 142        if (!need_ram_code)
 143                return node;
 144        if (ram_code == -1) {
 145                debug("%s: RAM code required but not supplied\n", __func__);
 146                return ERR_NO_RAM_CODE;
 147        }
 148
 149        offset = node;
 150        depth = 0;
 151        do {
 152                /*
 153                 * Sadly there is no compatible string so we cannot use
 154                 * fdtdec_next_compatible_subnode().
 155                 */
 156                offset = fdt_next_node(blob, offset, &depth);
 157                if (depth <= 0)
 158                        break;
 159
 160                /* Make sure this is a direct subnode */
 161                if (depth != 1)
 162                        continue;
 163                if (strcmp("emc-tables", fdt_get_name(blob, offset, NULL)))
 164                        continue;
 165
 166                if (fdtdec_get_int(blob, offset, "nvidia,ram-code", -1)
 167                                == ram_code)
 168                        return offset;
 169        } while (1);
 170
 171        debug("%s: Could not find tables for RAM code %d\n", __func__,
 172              ram_code);
 173        return ERR_RAM_CODE_NOT_FOUND;
 174}
 175
 176/**
 177 * Decode the EMC node of the device tree, returning a pointer to the emc
 178 * controller and the table to be used for the given rate.
 179 *
 180 * @param blob  Device tree blob
 181 * @param rate  Clock speed of memory controller in Hz (=2x memory bus rate)
 182 * @param emcp  Returns address of EMC controller registers
 183 * @param tablep Returns pointer to table to program into EMC. There are
 184 *              TEGRA_EMC_NUM_REGS entries, destined for offsets as per the
 185 *              emc_reg_addr array.
 186 * @return 0 if ok, otherwise a -ve error code which will allow someone to
 187 * figure out roughly what went wrong by looking at this code.
 188 */
 189static int decode_emc(const void *blob, unsigned rate, struct emc_ctlr **emcp,
 190                      const u32 **tablep)
 191{
 192        struct apb_misc_pp_ctlr *pp =
 193                (struct apb_misc_pp_ctlr *)NV_PA_APB_MISC_BASE;
 194        int ram_code;
 195        int depth;
 196        int node;
 197
 198        ram_code = (readl(&pp->strapping_opt_a) & RAM_CODE_MASK)
 199                        >> RAM_CODE_SHIFT;
 200        /*
 201         * The EMC clock rate is twice the bus rate, and the bus rate is
 202         * measured in kHz
 203         */
 204        rate = rate / 2 / 1000;
 205
 206        node = fdtdec_next_compatible(blob, 0, COMPAT_NVIDIA_TEGRA20_EMC);
 207        if (node < 0) {
 208                debug("%s: No EMC node found in FDT\n", __func__);
 209                return ERR_NO_EMC_NODE;
 210        }
 211        *emcp = (struct emc_ctlr *)fdtdec_get_addr(blob, node, "reg");
 212        if (*emcp == (struct emc_ctlr *)FDT_ADDR_T_NONE) {
 213                debug("%s: No EMC node reg property\n", __func__);
 214                return ERR_NO_EMC_REG;
 215        }
 216
 217        /* Work out the parent node which contains our EMC tables */
 218        node = find_emc_tables(blob, node, ram_code & 3);
 219        if (node < 0)
 220                return node;
 221
 222        depth = 0;
 223        for (;;) {
 224                int node_rate;
 225
 226                node = fdtdec_next_compatible_subnode(blob, node,
 227                                COMPAT_NVIDIA_TEGRA20_EMC_TABLE, &depth);
 228                if (node < 0)
 229                        break;
 230                node_rate = fdtdec_get_int(blob, node, "clock-frequency", -1);
 231                if (node_rate == -1) {
 232                        debug("%s: Missing clock-frequency\n", __func__);
 233                        return ERR_NO_FREQ; /* we expect this property */
 234                }
 235
 236                if (node_rate == rate)
 237                        break;
 238        }
 239        if (node < 0) {
 240                debug("%s: No node found for clock frequency %d\n", __func__,
 241                      rate);
 242                return ERR_FREQ_NOT_FOUND;
 243        }
 244
 245        *tablep = fdtdec_locate_array(blob, node, "nvidia,emc-registers",
 246                                      TEGRA_EMC_NUM_REGS);
 247        if (!*tablep) {
 248                debug("%s: node '%s' array missing / wrong size\n", __func__,
 249                      fdt_get_name(blob, node, NULL));
 250                return ERR_BAD_REGS;
 251        }
 252
 253        /* All seems well */
 254        return 0;
 255}
 256
 257int tegra_set_emc(const void *blob, unsigned rate)
 258{
 259        struct emc_ctlr *emc;
 260        const u32 *table;
 261        int err, i;
 262
 263        err = decode_emc(blob, rate, &emc, &table);
 264        if (err) {
 265                debug("Warning: no valid EMC (%d), memory timings unset\n",
 266                       err);
 267                return err;
 268        }
 269
 270        debug("%s: Table found, setting EMC values as follows:\n", __func__);
 271        for (i = 0; i < TEGRA_EMC_NUM_REGS; i++) {
 272                u32 value = fdt32_to_cpu(table[i]);
 273                u32 addr = (uintptr_t)emc + emc_reg_addr[i];
 274
 275                debug("   %#x: %#x\n", addr, value);
 276                writel(value, addr);
 277        }
 278
 279        /* trigger emc with new settings */
 280        clock_adjust_periph_pll_div(PERIPH_ID_EMC, CLOCK_ID_MEMORY,
 281                                clock_get_rate(CLOCK_ID_MEMORY), NULL);
 282        debug("EMC clock set to %lu\n",
 283              clock_get_periph_rate(PERIPH_ID_EMC, CLOCK_ID_MEMORY));
 284
 285        return 0;
 286}
 287