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 */ 29aa7eb2bbSMichal Simek 30b5f177ffSSoren Brinkmann #define SLCR_UNLOCK_MAGIC 0xDF0D 31aa7eb2bbSMichal Simek #define SLCR_A9_CPU_CLKSTOP 0x10 32aa7eb2bbSMichal Simek #define SLCR_A9_CPU_RST 0x1 33aa7eb2bbSMichal Simek 347b274efeSSteffen Trumtrar static void __iomem *zynq_slcr_base; 35016f4dcaSMichal Simek static struct regmap *zynq_slcr_regmap; 3664b889b3SMichal Simek 3764b889b3SMichal Simek /** 38871c6971SMichal Simek * zynq_slcr_write - Write to a register in SLCR block 39871c6971SMichal Simek * 40871c6971SMichal Simek * @val: Value to write to the register 41871c6971SMichal Simek * @offset: Register offset in SLCR block 42871c6971SMichal Simek * 43871c6971SMichal Simek * Return: a negative value on error, 0 on success 44871c6971SMichal Simek */ 45871c6971SMichal Simek static int zynq_slcr_write(u32 val, u32 offset) 46871c6971SMichal Simek { 47871c6971SMichal Simek if (!zynq_slcr_regmap) { 48871c6971SMichal Simek writel(val, zynq_slcr_base + offset); 49871c6971SMichal Simek return 0; 50871c6971SMichal Simek } 51871c6971SMichal Simek 52871c6971SMichal Simek return regmap_write(zynq_slcr_regmap, offset, val); 53871c6971SMichal Simek } 54871c6971SMichal Simek 55871c6971SMichal Simek /** 56871c6971SMichal Simek * zynq_slcr_read - Read a register in SLCR block 57871c6971SMichal Simek * 58871c6971SMichal Simek * @val: Pointer to value to be read from SLCR 59871c6971SMichal Simek * @offset: Register offset in SLCR block 60871c6971SMichal Simek * 61871c6971SMichal Simek * Return: a negative value on error, 0 on success 62871c6971SMichal Simek */ 63871c6971SMichal Simek static int zynq_slcr_read(u32 *val, u32 offset) 64871c6971SMichal Simek { 65871c6971SMichal Simek if (zynq_slcr_regmap) 66871c6971SMichal Simek return regmap_read(zynq_slcr_regmap, offset, val); 67871c6971SMichal Simek 68871c6971SMichal Simek *val = readl(zynq_slcr_base + offset); 69871c6971SMichal Simek 70871c6971SMichal Simek return 0; 71871c6971SMichal Simek } 72871c6971SMichal Simek 73871c6971SMichal Simek /** 74*56880073SMichal Simek * zynq_slcr_unlock - Unlock SLCR registers 75*56880073SMichal Simek * 76*56880073SMichal Simek * Return: a negative value on error, 0 on success 77*56880073SMichal Simek */ 78*56880073SMichal Simek static inline int zynq_slcr_unlock(void) 79*56880073SMichal Simek { 80*56880073SMichal Simek zynq_slcr_write(SLCR_UNLOCK_MAGIC, SLCR_UNLOCK_OFFSET); 81*56880073SMichal Simek 82*56880073SMichal Simek return 0; 83*56880073SMichal Simek } 84*56880073SMichal Simek 85*56880073SMichal Simek /** 8696790f0aSMichal Simek * zynq_slcr_system_reset - Reset the entire system. 8796790f0aSMichal Simek */ 8896790f0aSMichal Simek void zynq_slcr_system_reset(void) 8996790f0aSMichal Simek { 9096790f0aSMichal Simek u32 reboot; 9196790f0aSMichal Simek 9296790f0aSMichal Simek /* 9396790f0aSMichal Simek * Unlock the SLCR then reset the system. 9496790f0aSMichal Simek * Note that this seems to require raw i/o 9596790f0aSMichal Simek * functions or there's a lockup? 9696790f0aSMichal Simek */ 97*56880073SMichal Simek zynq_slcr_unlock(); 9896790f0aSMichal Simek 9996790f0aSMichal Simek /* 10096790f0aSMichal Simek * Clear 0x0F000000 bits of reboot status register to workaround 10196790f0aSMichal Simek * the FSBL not loading the bitstream after soft-reboot 10296790f0aSMichal Simek * This is a temporary solution until we know more. 10396790f0aSMichal Simek */ 104871c6971SMichal Simek zynq_slcr_read(&reboot, SLCR_REBOOT_STATUS_OFFSET); 105871c6971SMichal Simek zynq_slcr_write(reboot & 0xF0FFFFFF, SLCR_REBOOT_STATUS_OFFSET); 106871c6971SMichal Simek zynq_slcr_write(1, SLCR_PS_RST_CTRL_OFFSET); 10796790f0aSMichal Simek } 10896790f0aSMichal Simek 10996790f0aSMichal Simek /** 110aa7eb2bbSMichal Simek * zynq_slcr_cpu_start - Start cpu 111aa7eb2bbSMichal Simek * @cpu: cpu number 112aa7eb2bbSMichal Simek */ 113aa7eb2bbSMichal Simek void zynq_slcr_cpu_start(int cpu) 114aa7eb2bbSMichal Simek { 115871c6971SMichal Simek u32 reg; 116871c6971SMichal Simek 117871c6971SMichal Simek zynq_slcr_read(®, SLCR_A9_CPU_RST_CTRL_OFFSET); 1183db9e860SSoren Brinkmann reg &= ~(SLCR_A9_CPU_RST << cpu); 119871c6971SMichal Simek zynq_slcr_write(reg, SLCR_A9_CPU_RST_CTRL_OFFSET); 1203db9e860SSoren Brinkmann reg &= ~(SLCR_A9_CPU_CLKSTOP << cpu); 121871c6971SMichal Simek zynq_slcr_write(reg, SLCR_A9_CPU_RST_CTRL_OFFSET); 122aa7eb2bbSMichal Simek } 123aa7eb2bbSMichal Simek 124aa7eb2bbSMichal Simek /** 125aa7eb2bbSMichal Simek * zynq_slcr_cpu_stop - Stop cpu 126aa7eb2bbSMichal Simek * @cpu: cpu number 127aa7eb2bbSMichal Simek */ 128aa7eb2bbSMichal Simek void zynq_slcr_cpu_stop(int cpu) 129aa7eb2bbSMichal Simek { 130871c6971SMichal Simek u32 reg; 131871c6971SMichal Simek 132871c6971SMichal Simek zynq_slcr_read(®, SLCR_A9_CPU_RST_CTRL_OFFSET); 1333db9e860SSoren Brinkmann reg |= (SLCR_A9_CPU_CLKSTOP | SLCR_A9_CPU_RST) << cpu; 134871c6971SMichal Simek zynq_slcr_write(reg, SLCR_A9_CPU_RST_CTRL_OFFSET); 135aa7eb2bbSMichal Simek } 136aa7eb2bbSMichal Simek 137aa7eb2bbSMichal Simek /** 138016f4dcaSMichal Simek * zynq_slcr_init - Regular slcr driver init 139016f4dcaSMichal Simek * 140016f4dcaSMichal Simek * Return: 0 on success, negative errno otherwise. 14164b889b3SMichal Simek * 14264b889b3SMichal Simek * Called early during boot from platform code to remap SLCR area. 14364b889b3SMichal Simek */ 14464b889b3SMichal Simek int __init zynq_slcr_init(void) 14564b889b3SMichal Simek { 146016f4dcaSMichal Simek zynq_slcr_regmap = syscon_regmap_lookup_by_compatible("xlnx,zynq-slcr"); 147016f4dcaSMichal Simek if (IS_ERR(zynq_slcr_regmap)) { 148016f4dcaSMichal Simek pr_err("%s: failed to find zynq-slcr\n", __func__); 149016f4dcaSMichal Simek return -ENODEV; 150016f4dcaSMichal Simek } 151016f4dcaSMichal Simek 152016f4dcaSMichal Simek return 0; 153016f4dcaSMichal Simek } 154016f4dcaSMichal Simek 155016f4dcaSMichal Simek /** 156016f4dcaSMichal Simek * zynq_early_slcr_init - Early slcr init function 157016f4dcaSMichal Simek * 158016f4dcaSMichal Simek * Return: 0 on success, negative errno otherwise. 159016f4dcaSMichal Simek * 160016f4dcaSMichal Simek * Called very early during boot from platform code to unlock SLCR. 161016f4dcaSMichal Simek */ 162016f4dcaSMichal Simek int __init zynq_early_slcr_init(void) 163016f4dcaSMichal Simek { 16464b889b3SMichal Simek struct device_node *np; 16564b889b3SMichal Simek 16664b889b3SMichal Simek np = of_find_compatible_node(NULL, NULL, "xlnx,zynq-slcr"); 16764b889b3SMichal Simek if (!np) { 16864b889b3SMichal Simek pr_err("%s: no slcr node found\n", __func__); 16964b889b3SMichal Simek BUG(); 17064b889b3SMichal Simek } 17164b889b3SMichal Simek 17264b889b3SMichal Simek zynq_slcr_base = of_iomap(np, 0); 17364b889b3SMichal Simek if (!zynq_slcr_base) { 17464b889b3SMichal Simek pr_err("%s: Unable to map I/O memory\n", __func__); 17564b889b3SMichal Simek BUG(); 17664b889b3SMichal Simek } 17764b889b3SMichal Simek 1785e218280SSteffen Trumtrar np->data = (__force void *)zynq_slcr_base; 1795e218280SSteffen Trumtrar 18064b889b3SMichal Simek /* unlock the SLCR so that registers can be changed */ 181*56880073SMichal Simek zynq_slcr_unlock(); 18264b889b3SMichal Simek 18364b889b3SMichal Simek pr_info("%s mapped to %p\n", np->name, zynq_slcr_base); 18464b889b3SMichal Simek 18564b889b3SMichal Simek of_node_put(np); 18664b889b3SMichal Simek 18764b889b3SMichal Simek return 0; 18864b889b3SMichal Simek } 189