18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci *  Copyright (C) 2004 Florian Schirmer <jolt@tuxbox.org>
38c2ecf20Sopenharmony_ci *  Copyright (C) 2007 Aurelien Jarno <aurelien@aurel32.net>
48c2ecf20Sopenharmony_ci *  Copyright (C) 2010-2012 Hauke Mehrtens <hauke@hauke-m.de>
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci *  This program is free software; you can redistribute  it and/or modify it
78c2ecf20Sopenharmony_ci *  under  the terms of  the GNU General  Public License as published by the
88c2ecf20Sopenharmony_ci *  Free Software Foundation;  either version 2 of the  License, or (at your
98c2ecf20Sopenharmony_ci *  option) any later version.
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci *  THIS  SOFTWARE  IS PROVIDED   ``AS  IS'' AND   ANY  EXPRESS OR IMPLIED
128c2ecf20Sopenharmony_ci *  WARRANTIES,   INCLUDING, BUT NOT  LIMITED  TO, THE IMPLIED WARRANTIES OF
138c2ecf20Sopenharmony_ci *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
148c2ecf20Sopenharmony_ci *  NO  EVENT  SHALL   THE AUTHOR  BE    LIABLE FOR ANY   DIRECT, INDIRECT,
158c2ecf20Sopenharmony_ci *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
168c2ecf20Sopenharmony_ci *  NOT LIMITED   TO, PROCUREMENT OF  SUBSTITUTE GOODS  OR SERVICES; LOSS OF
178c2ecf20Sopenharmony_ci *  USE, DATA,  OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
188c2ecf20Sopenharmony_ci *  ANY THEORY OF LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT
198c2ecf20Sopenharmony_ci *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
208c2ecf20Sopenharmony_ci *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci *  You should have received a copy of the  GNU General Public License along
238c2ecf20Sopenharmony_ci *  with this program; if not, write  to the Free Software Foundation, Inc.,
248c2ecf20Sopenharmony_ci *  675 Mass Ave, Cambridge, MA 02139, USA.
258c2ecf20Sopenharmony_ci */
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#include <linux/init.h>
288c2ecf20Sopenharmony_ci#include <linux/types.h>
298c2ecf20Sopenharmony_ci#include <linux/kernel.h>
308c2ecf20Sopenharmony_ci#include <linux/memblock.h>
318c2ecf20Sopenharmony_ci#include <linux/spinlock.h>
328c2ecf20Sopenharmony_ci#include <linux/ssb/ssb_driver_chipcommon.h>
338c2ecf20Sopenharmony_ci#include <linux/ssb/ssb_regs.h>
348c2ecf20Sopenharmony_ci#include <linux/smp.h>
358c2ecf20Sopenharmony_ci#include <asm/bootinfo.h>
368c2ecf20Sopenharmony_ci#include <bcm47xx.h>
378c2ecf20Sopenharmony_ci#include <bcm47xx_board.h>
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cistatic char bcm47xx_system_type[20] = "Broadcom BCM47XX";
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ciconst char *get_system_type(void)
428c2ecf20Sopenharmony_ci{
438c2ecf20Sopenharmony_ci	return bcm47xx_system_type;
448c2ecf20Sopenharmony_ci}
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci__init void bcm47xx_set_system_type(u16 chip_id)
478c2ecf20Sopenharmony_ci{
488c2ecf20Sopenharmony_ci	snprintf(bcm47xx_system_type, sizeof(bcm47xx_system_type),
498c2ecf20Sopenharmony_ci		 (chip_id > 0x9999) ? "Broadcom BCM%d" :
508c2ecf20Sopenharmony_ci				      "Broadcom BCM%04X",
518c2ecf20Sopenharmony_ci		 chip_id);
528c2ecf20Sopenharmony_ci}
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_cistatic unsigned long lowmem __initdata;
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cistatic __init void prom_init_mem(void)
578c2ecf20Sopenharmony_ci{
588c2ecf20Sopenharmony_ci	unsigned long mem;
598c2ecf20Sopenharmony_ci	unsigned long max;
608c2ecf20Sopenharmony_ci	unsigned long off;
618c2ecf20Sopenharmony_ci	struct cpuinfo_mips *c = &current_cpu_data;
628c2ecf20Sopenharmony_ci
638c2ecf20Sopenharmony_ci	/* Figure out memory size by finding aliases.
648c2ecf20Sopenharmony_ci	 *
658c2ecf20Sopenharmony_ci	 * We should theoretically use the mapping from CFE using cfe_enummem().
668c2ecf20Sopenharmony_ci	 * However as the BCM47XX is mostly used on low-memory systems, we
678c2ecf20Sopenharmony_ci	 * want to reuse the memory used by CFE (around 4MB). That means cfe_*
688c2ecf20Sopenharmony_ci	 * functions stop to work at some point during the boot, we should only
698c2ecf20Sopenharmony_ci	 * call them at the beginning of the boot.
708c2ecf20Sopenharmony_ci	 *
718c2ecf20Sopenharmony_ci	 * BCM47XX uses 128MB for addressing the ram, if the system contains
728c2ecf20Sopenharmony_ci	 * less that that amount of ram it remaps the ram more often into the
738c2ecf20Sopenharmony_ci	 * available space.
748c2ecf20Sopenharmony_ci	 */
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci	/* Physical address, without mapping to any kernel segment */
778c2ecf20Sopenharmony_ci	off = CPHYSADDR((unsigned long)prom_init);
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	/* Accessing memory after 128 MiB will cause an exception */
808c2ecf20Sopenharmony_ci	max = 128 << 20;
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci	for (mem = 1 << 20; mem < max; mem += 1 << 20) {
838c2ecf20Sopenharmony_ci		/* Loop condition may be not enough, off may be over 1 MiB */
848c2ecf20Sopenharmony_ci		if (off + mem >= max) {
858c2ecf20Sopenharmony_ci			mem = max;
868c2ecf20Sopenharmony_ci			pr_debug("Assume 128MB RAM\n");
878c2ecf20Sopenharmony_ci			break;
888c2ecf20Sopenharmony_ci		}
898c2ecf20Sopenharmony_ci		if (!memcmp((void *)prom_init, (void *)prom_init + mem, 32))
908c2ecf20Sopenharmony_ci			break;
918c2ecf20Sopenharmony_ci	}
928c2ecf20Sopenharmony_ci	lowmem = mem;
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	/* Ignoring the last page when ddr size is 128M. Cached
958c2ecf20Sopenharmony_ci	 * accesses to last page is causing the processor to prefetch
968c2ecf20Sopenharmony_ci	 * using address above 128M stepping out of the ddr address
978c2ecf20Sopenharmony_ci	 * space.
988c2ecf20Sopenharmony_ci	 */
998c2ecf20Sopenharmony_ci	if (c->cputype == CPU_74K && (mem == (128  << 20)))
1008c2ecf20Sopenharmony_ci		mem -= 0x1000;
1018c2ecf20Sopenharmony_ci	memblock_add(0, mem);
1028c2ecf20Sopenharmony_ci}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci/*
1058c2ecf20Sopenharmony_ci * This is the first serial on the chip common core, it is at this position
1068c2ecf20Sopenharmony_ci * for sb (ssb) and ai (bcma) bus.
1078c2ecf20Sopenharmony_ci */
1088c2ecf20Sopenharmony_ci#define BCM47XX_SERIAL_ADDR (SSB_ENUM_BASE + SSB_CHIPCO_UART0_DATA)
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_civoid __init prom_init(void)
1118c2ecf20Sopenharmony_ci{
1128c2ecf20Sopenharmony_ci	prom_init_mem();
1138c2ecf20Sopenharmony_ci	setup_8250_early_printk_port(CKSEG1ADDR(BCM47XX_SERIAL_ADDR), 0, 0);
1148c2ecf20Sopenharmony_ci}
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_civoid __init prom_free_prom_memory(void)
1178c2ecf20Sopenharmony_ci{
1188c2ecf20Sopenharmony_ci}
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci#if defined(CONFIG_BCM47XX_BCMA) && defined(CONFIG_HIGHMEM)
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci#define EXTVBASE	0xc0000000
1238c2ecf20Sopenharmony_ci#define ENTRYLO(x)	((pte_val(pfn_pte((x) >> _PFN_SHIFT, PAGE_KERNEL_UNCACHED)) >> 6) | 1)
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci#include <asm/tlbflush.h>
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ci/* Stripped version of tlb_init, with the call to build_tlb_refill_handler
1288c2ecf20Sopenharmony_ci * dropped. Calling it at this stage causes a hang.
1298c2ecf20Sopenharmony_ci */
1308c2ecf20Sopenharmony_civoid early_tlb_init(void)
1318c2ecf20Sopenharmony_ci{
1328c2ecf20Sopenharmony_ci	write_c0_pagemask(PM_DEFAULT_MASK);
1338c2ecf20Sopenharmony_ci	write_c0_wired(0);
1348c2ecf20Sopenharmony_ci	temp_tlb_entry = current_cpu_data.tlbsize - 1;
1358c2ecf20Sopenharmony_ci	local_flush_tlb_all();
1368c2ecf20Sopenharmony_ci}
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_civoid __init bcm47xx_prom_highmem_init(void)
1398c2ecf20Sopenharmony_ci{
1408c2ecf20Sopenharmony_ci	unsigned long off = (unsigned long)prom_init;
1418c2ecf20Sopenharmony_ci	unsigned long extmem = 0;
1428c2ecf20Sopenharmony_ci	bool highmem_region = false;
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci	if (WARN_ON(bcm47xx_bus_type != BCM47XX_BUS_TYPE_BCMA))
1458c2ecf20Sopenharmony_ci		return;
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	if (bcm47xx_bus.bcma.bus.chipinfo.id == BCMA_CHIP_ID_BCM4706)
1488c2ecf20Sopenharmony_ci		highmem_region = true;
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	if (lowmem != 128 << 20 || !highmem_region)
1518c2ecf20Sopenharmony_ci		return;
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	early_tlb_init();
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	/* Add one temporary TLB entry to map SDRAM Region 2.
1568c2ecf20Sopenharmony_ci	 *      Physical        Virtual
1578c2ecf20Sopenharmony_ci	 *      0x80000000      0xc0000000      (1st: 256MB)
1588c2ecf20Sopenharmony_ci	 *      0x90000000      0xd0000000      (2nd: 256MB)
1598c2ecf20Sopenharmony_ci	 */
1608c2ecf20Sopenharmony_ci	add_temporary_entry(ENTRYLO(0x80000000),
1618c2ecf20Sopenharmony_ci			    ENTRYLO(0x80000000 + (256 << 20)),
1628c2ecf20Sopenharmony_ci			    EXTVBASE, PM_256M);
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	off = EXTVBASE + __pa(off);
1658c2ecf20Sopenharmony_ci	for (extmem = 128 << 20; extmem < 512 << 20; extmem <<= 1) {
1668c2ecf20Sopenharmony_ci		if (!memcmp((void *)prom_init, (void *)(off + extmem), 16))
1678c2ecf20Sopenharmony_ci			break;
1688c2ecf20Sopenharmony_ci	}
1698c2ecf20Sopenharmony_ci	extmem -= lowmem;
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	early_tlb_init();
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	if (!extmem)
1748c2ecf20Sopenharmony_ci		return;
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	pr_warn("Found %lu MiB of extra memory, but highmem is unsupported yet!\n",
1778c2ecf20Sopenharmony_ci		extmem >> 20);
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	/* TODO: Register extra memory */
1808c2ecf20Sopenharmony_ci}
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci#endif /* defined(CONFIG_BCM47XX_BCMA) && defined(CONFIG_HIGHMEM) */
183