164b889b3SMichal Simek /* 264b889b3SMichal Simek * Xilinx SLCR driver 364b889b3SMichal Simek * 464b889b3SMichal Simek * Copyright (c) 2011-2013 Xilinx Inc. 564b889b3SMichal Simek * 664b889b3SMichal Simek * This program is free software; you can redistribute it and/or 764b889b3SMichal Simek * modify it under the terms of the GNU General Public License 864b889b3SMichal Simek * as published by the Free Software Foundation; either version 964b889b3SMichal Simek * 2 of the License, or (at your option) any later version. 1064b889b3SMichal Simek * 1164b889b3SMichal Simek * You should have received a copy of the GNU General Public 1264b889b3SMichal Simek * License along with this program; if not, write to the Free 1364b889b3SMichal Simek * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 1464b889b3SMichal Simek * 02139, USA. 1564b889b3SMichal Simek */ 1664b889b3SMichal Simek 1764b889b3SMichal Simek #include <linux/io.h> 18016f4dcaSMichal Simek #include <linux/mfd/syscon.h> 1964b889b3SMichal Simek #include <linux/of_address.h> 20016f4dcaSMichal Simek #include <linux/regmap.h> 2164b889b3SMichal Simek #include <linux/clk/zynq.h> 2264b889b3SMichal Simek #include "common.h" 2364b889b3SMichal Simek 24b5f177ffSSoren Brinkmann /* register offsets */ 25b5f177ffSSoren Brinkmann #define SLCR_UNLOCK_OFFSET 0x8 /* SCLR unlock register */ 2696790f0aSMichal Simek #define SLCR_PS_RST_CTRL_OFFSET 0x200 /* PS Software Reset Control */ 27b5f177ffSSoren Brinkmann #define SLCR_A9_CPU_RST_CTRL_OFFSET 0x244 /* CPU Software Reset Control */ 28b5f177ffSSoren Brinkmann #define SLCR_REBOOT_STATUS_OFFSET 0x258 /* PS Reboot Status */ 29*00f7dc63SMichal Simek #define SLCR_PSS_IDCODE 0x530 /* PS IDCODE */ 30aa7eb2bbSMichal Simek 31b5f177ffSSoren Brinkmann #define SLCR_UNLOCK_MAGIC 0xDF0D 32aa7eb2bbSMichal Simek #define SLCR_A9_CPU_CLKSTOP 0x10 33aa7eb2bbSMichal Simek #define SLCR_A9_CPU_RST 0x1 34*00f7dc63SMichal Simek #define SLCR_PSS_IDCODE_DEVICE_SHIFT 12 35*00f7dc63SMichal Simek #define SLCR_PSS_IDCODE_DEVICE_MASK 0x1F 36aa7eb2bbSMichal Simek 377b274efeSSteffen Trumtrar static void __iomem *zynq_slcr_base; 38016f4dcaSMichal Simek static struct regmap *zynq_slcr_regmap; 3964b889b3SMichal Simek 4064b889b3SMichal Simek /** 41871c6971SMichal Simek * zynq_slcr_write - Write to a register in SLCR block 42871c6971SMichal Simek * 43871c6971SMichal Simek * @val: Value to write to the register 44871c6971SMichal Simek * @offset: Register offset in SLCR block 45871c6971SMichal Simek * 46871c6971SMichal Simek * Return: a negative value on error, 0 on success 47871c6971SMichal Simek */ 48871c6971SMichal Simek static int zynq_slcr_write(u32 val, u32 offset) 49871c6971SMichal Simek { 50871c6971SMichal Simek if (!zynq_slcr_regmap) { 51871c6971SMichal Simek writel(val, zynq_slcr_base + offset); 52871c6971SMichal Simek return 0; 53871c6971SMichal Simek } 54871c6971SMichal Simek 55871c6971SMichal Simek return regmap_write(zynq_slcr_regmap, offset, val); 56871c6971SMichal Simek } 57871c6971SMichal Simek 58871c6971SMichal Simek /** 59871c6971SMichal Simek * zynq_slcr_read - Read a register in SLCR block 60871c6971SMichal Simek * 61871c6971SMichal Simek * @val: Pointer to value to be read from SLCR 62871c6971SMichal Simek * @offset: Register offset in SLCR block 63871c6971SMichal Simek * 64871c6971SMichal Simek * Return: a negative value on error, 0 on success 65871c6971SMichal Simek */ 66871c6971SMichal Simek static int zynq_slcr_read(u32 *val, u32 offset) 67871c6971SMichal Simek { 68871c6971SMichal Simek if (zynq_slcr_regmap) 69871c6971SMichal Simek return regmap_read(zynq_slcr_regmap, offset, val); 70871c6971SMichal Simek 71871c6971SMichal Simek *val = readl(zynq_slcr_base + offset); 72871c6971SMichal Simek 73871c6971SMichal Simek return 0; 74871c6971SMichal Simek } 75871c6971SMichal Simek 76871c6971SMichal Simek /** 7756880073SMichal Simek * zynq_slcr_unlock - Unlock SLCR registers 7856880073SMichal Simek * 7956880073SMichal Simek * Return: a negative value on error, 0 on success 8056880073SMichal Simek */ 8156880073SMichal Simek static inline int zynq_slcr_unlock(void) 8256880073SMichal Simek { 8356880073SMichal Simek zynq_slcr_write(SLCR_UNLOCK_MAGIC, SLCR_UNLOCK_OFFSET); 8456880073SMichal Simek 8556880073SMichal Simek return 0; 8656880073SMichal Simek } 8756880073SMichal Simek 8856880073SMichal Simek /** 89*00f7dc63SMichal Simek * zynq_slcr_get_device_id - Read device code id 90*00f7dc63SMichal Simek * 91*00f7dc63SMichal Simek * Return: Device code id 92*00f7dc63SMichal Simek */ 93*00f7dc63SMichal Simek u32 zynq_slcr_get_device_id(void) 94*00f7dc63SMichal Simek { 95*00f7dc63SMichal Simek u32 val; 96*00f7dc63SMichal Simek 97*00f7dc63SMichal Simek zynq_slcr_read(&val, SLCR_PSS_IDCODE); 98*00f7dc63SMichal Simek val >>= SLCR_PSS_IDCODE_DEVICE_SHIFT; 99*00f7dc63SMichal Simek val &= SLCR_PSS_IDCODE_DEVICE_MASK; 100*00f7dc63SMichal Simek 101*00f7dc63SMichal Simek return val; 102*00f7dc63SMichal Simek } 103*00f7dc63SMichal Simek 104*00f7dc63SMichal Simek /** 10596790f0aSMichal Simek * zynq_slcr_system_reset - Reset the entire system. 10696790f0aSMichal Simek */ 10796790f0aSMichal Simek void zynq_slcr_system_reset(void) 10896790f0aSMichal Simek { 10996790f0aSMichal Simek u32 reboot; 11096790f0aSMichal Simek 11196790f0aSMichal Simek /* 11296790f0aSMichal Simek * Unlock the SLCR then reset the system. 11396790f0aSMichal Simek * Note that this seems to require raw i/o 11496790f0aSMichal Simek * functions or there's a lockup? 11596790f0aSMichal Simek */ 11656880073SMichal Simek zynq_slcr_unlock(); 11796790f0aSMichal Simek 11896790f0aSMichal Simek /* 11996790f0aSMichal Simek * Clear 0x0F000000 bits of reboot status register to workaround 12096790f0aSMichal Simek * the FSBL not loading the bitstream after soft-reboot 12196790f0aSMichal Simek * This is a temporary solution until we know more. 12296790f0aSMichal Simek */ 123871c6971SMichal Simek zynq_slcr_read(&reboot, SLCR_REBOOT_STATUS_OFFSET); 124871c6971SMichal Simek zynq_slcr_write(reboot & 0xF0FFFFFF, SLCR_REBOOT_STATUS_OFFSET); 125871c6971SMichal Simek zynq_slcr_write(1, SLCR_PS_RST_CTRL_OFFSET); 12696790f0aSMichal Simek } 12796790f0aSMichal Simek 12896790f0aSMichal Simek /** 129aa7eb2bbSMichal Simek * zynq_slcr_cpu_start - Start cpu 130aa7eb2bbSMichal Simek * @cpu: cpu number 131aa7eb2bbSMichal Simek */ 132aa7eb2bbSMichal Simek void zynq_slcr_cpu_start(int cpu) 133aa7eb2bbSMichal Simek { 134871c6971SMichal Simek u32 reg; 135871c6971SMichal Simek 136871c6971SMichal Simek zynq_slcr_read(®, SLCR_A9_CPU_RST_CTRL_OFFSET); 1373db9e860SSoren Brinkmann reg &= ~(SLCR_A9_CPU_RST << cpu); 138871c6971SMichal Simek zynq_slcr_write(reg, SLCR_A9_CPU_RST_CTRL_OFFSET); 1393db9e860SSoren Brinkmann reg &= ~(SLCR_A9_CPU_CLKSTOP << cpu); 140871c6971SMichal Simek zynq_slcr_write(reg, SLCR_A9_CPU_RST_CTRL_OFFSET); 141aa7eb2bbSMichal Simek } 142aa7eb2bbSMichal Simek 143aa7eb2bbSMichal Simek /** 144aa7eb2bbSMichal Simek * zynq_slcr_cpu_stop - Stop cpu 145aa7eb2bbSMichal Simek * @cpu: cpu number 146aa7eb2bbSMichal Simek */ 147aa7eb2bbSMichal Simek void zynq_slcr_cpu_stop(int cpu) 148aa7eb2bbSMichal Simek { 149871c6971SMichal Simek u32 reg; 150871c6971SMichal Simek 151871c6971SMichal Simek zynq_slcr_read(®, SLCR_A9_CPU_RST_CTRL_OFFSET); 1523db9e860SSoren Brinkmann reg |= (SLCR_A9_CPU_CLKSTOP | SLCR_A9_CPU_RST) << cpu; 153871c6971SMichal Simek zynq_slcr_write(reg, SLCR_A9_CPU_RST_CTRL_OFFSET); 154aa7eb2bbSMichal Simek } 155aa7eb2bbSMichal Simek 156aa7eb2bbSMichal Simek /** 157016f4dcaSMichal Simek * zynq_slcr_init - Regular slcr driver init 158016f4dcaSMichal Simek * 159016f4dcaSMichal Simek * Return: 0 on success, negative errno otherwise. 16064b889b3SMichal Simek * 16164b889b3SMichal Simek * Called early during boot from platform code to remap SLCR area. 16264b889b3SMichal Simek */ 16364b889b3SMichal Simek int __init zynq_slcr_init(void) 16464b889b3SMichal Simek { 165016f4dcaSMichal Simek zynq_slcr_regmap = syscon_regmap_lookup_by_compatible("xlnx,zynq-slcr"); 166016f4dcaSMichal Simek if (IS_ERR(zynq_slcr_regmap)) { 167016f4dcaSMichal Simek pr_err("%s: failed to find zynq-slcr\n", __func__); 168016f4dcaSMichal Simek return -ENODEV; 169016f4dcaSMichal Simek } 170016f4dcaSMichal Simek 171016f4dcaSMichal Simek return 0; 172016f4dcaSMichal Simek } 173016f4dcaSMichal Simek 174016f4dcaSMichal Simek /** 175016f4dcaSMichal Simek * zynq_early_slcr_init - Early slcr init function 176016f4dcaSMichal Simek * 177016f4dcaSMichal Simek * Return: 0 on success, negative errno otherwise. 178016f4dcaSMichal Simek * 179016f4dcaSMichal Simek * Called very early during boot from platform code to unlock SLCR. 180016f4dcaSMichal Simek */ 181016f4dcaSMichal Simek int __init zynq_early_slcr_init(void) 182016f4dcaSMichal Simek { 18364b889b3SMichal Simek struct device_node *np; 18464b889b3SMichal Simek 18564b889b3SMichal Simek np = of_find_compatible_node(NULL, NULL, "xlnx,zynq-slcr"); 18664b889b3SMichal Simek if (!np) { 18764b889b3SMichal Simek pr_err("%s: no slcr node found\n", __func__); 18864b889b3SMichal Simek BUG(); 18964b889b3SMichal Simek } 19064b889b3SMichal Simek 19164b889b3SMichal Simek zynq_slcr_base = of_iomap(np, 0); 19264b889b3SMichal Simek if (!zynq_slcr_base) { 19364b889b3SMichal Simek pr_err("%s: Unable to map I/O memory\n", __func__); 19464b889b3SMichal Simek BUG(); 19564b889b3SMichal Simek } 19664b889b3SMichal Simek 1975e218280SSteffen Trumtrar np->data = (__force void *)zynq_slcr_base; 1985e218280SSteffen Trumtrar 19964b889b3SMichal Simek /* unlock the SLCR so that registers can be changed */ 20056880073SMichal Simek zynq_slcr_unlock(); 20164b889b3SMichal Simek 20264b889b3SMichal Simek pr_info("%s mapped to %p\n", np->name, zynq_slcr_base); 20364b889b3SMichal Simek 20464b889b3SMichal Simek of_node_put(np); 20564b889b3SMichal Simek 20664b889b3SMichal Simek return 0; 20764b889b3SMichal Simek } 208