Path: blob/master/arch/powerpc/platforms/512x/mpc512x_shared.c
26481 views
// SPDX-License-Identifier: GPL-2.0-or-later1/*2* Copyright (C) 2007,2008 Freescale Semiconductor, Inc. All rights reserved.3*4* Author: John Rigby <[email protected]>5*6* Description:7* MPC512x Shared code8*/910#include <linux/clk.h>11#include <linux/kernel.h>12#include <linux/io.h>13#include <linux/irq.h>14#include <linux/of_address.h>15#include <linux/of_platform.h>16#include <linux/fsl-diu-fb.h>17#include <linux/memblock.h>18#include <sysdev/fsl_soc.h>1920#include <asm/cacheflush.h>21#include <asm/machdep.h>22#include <asm/ipic.h>23#include <asm/time.h>24#include <asm/mpc5121.h>25#include <asm/mpc52xx_psc.h>2627#include "mpc512x.h"2829static struct mpc512x_reset_module __iomem *reset_module_base;3031void __noreturn mpc512x_restart(char *cmd)32{33if (reset_module_base) {34/* Enable software reset "RSTE" */35out_be32(&reset_module_base->rpr, 0x52535445);36/* Set software hard reset */37out_be32(&reset_module_base->rcr, 0x2);38} else {39pr_err("Restart module not mapped.\n");40}41for (;;)42;43}4445struct fsl_diu_shared_fb {46u8 gamma[0x300]; /* 32-bit aligned! */47struct diu_ad ad0; /* 32-bit aligned! */48phys_addr_t fb_phys;49size_t fb_len;50bool in_use;51};5253/* receives a pixel clock spec in pico seconds, adjusts the DIU clock rate */54static void mpc512x_set_pixel_clock(unsigned int pixclock)55{56struct device_node *np;57struct clk *clk_diu;58unsigned long epsilon, minpixclock, maxpixclock;59unsigned long offset, want, got, delta;6061/* lookup and enable the DIU clock */62np = of_find_compatible_node(NULL, NULL, "fsl,mpc5121-diu");63if (!np) {64pr_err("Could not find DIU device tree node.\n");65return;66}67clk_diu = of_clk_get(np, 0);68if (IS_ERR(clk_diu)) {69/* backwards compat with device trees that lack clock specs */70clk_diu = clk_get_sys(np->name, "ipg");71}72of_node_put(np);73if (IS_ERR(clk_diu)) {74pr_err("Could not lookup DIU clock.\n");75return;76}77if (clk_prepare_enable(clk_diu)) {78pr_err("Could not enable DIU clock.\n");79return;80}8182/*83* convert the picoseconds spec into the desired clock rate,84* determine the acceptable clock range for the monitor (+/- 5%),85* do the calculation in steps to avoid integer overflow86*/87pr_debug("DIU pixclock in ps - %u\n", pixclock);88pixclock = (1000000000 / pixclock) * 1000;89pr_debug("DIU pixclock freq - %u\n", pixclock);90epsilon = pixclock / 20; /* pixclock * 0.05 */91pr_debug("DIU deviation - %lu\n", epsilon);92minpixclock = pixclock - epsilon;93maxpixclock = pixclock + epsilon;94pr_debug("DIU minpixclock - %lu\n", minpixclock);95pr_debug("DIU maxpixclock - %lu\n", maxpixclock);9697/*98* check whether the DIU supports the desired pixel clock99*100* - simply request the desired clock and see what the101* platform's clock driver will make of it, assuming that it102* will setup the best approximation of the requested value103* - try other candidate frequencies in the order of decreasing104* preference (i.e. with increasing distance from the desired105* pixel clock, and checking the lower frequency before the106* higher frequency to not overload the hardware) until the107* first match is found -- any potential subsequent match108* would only be as good as the former match or typically109* would be less preferrable110*111* the offset increment of pixelclock divided by 64 is an112* arbitrary choice -- it's simple to calculate, in the typical113* case we expect the first check to succeed already, in the114* worst case seven frequencies get tested (the exact center and115* three more values each to the left and to the right) before116* the 5% tolerance window is exceeded, resulting in fast enough117* execution yet high enough probability of finding a suitable118* value, while the error rate will be in the order of single119* percents120*/121for (offset = 0; offset <= epsilon; offset += pixclock / 64) {122want = pixclock - offset;123pr_debug("DIU checking clock - %lu\n", want);124clk_set_rate(clk_diu, want);125got = clk_get_rate(clk_diu);126delta = abs(pixclock - got);127if (delta < epsilon)128break;129if (!offset)130continue;131want = pixclock + offset;132pr_debug("DIU checking clock - %lu\n", want);133clk_set_rate(clk_diu, want);134got = clk_get_rate(clk_diu);135delta = abs(pixclock - got);136if (delta < epsilon)137break;138}139if (offset <= epsilon) {140pr_debug("DIU clock accepted - %lu\n", want);141pr_debug("DIU pixclock want %u, got %lu, delta %lu, eps %lu\n",142pixclock, got, delta, epsilon);143return;144}145pr_warn("DIU pixclock auto search unsuccessful\n");146147/*148* what is the most appropriate action to take when the search149* for an available pixel clock which is acceptable to the150* monitor has failed? disable the DIU (clock) or just provide151* a "best effort"? we go with the latter152*/153pr_warn("DIU pixclock best effort fallback (backend's choice)\n");154clk_set_rate(clk_diu, pixclock);155got = clk_get_rate(clk_diu);156delta = abs(pixclock - got);157pr_debug("DIU pixclock want %u, got %lu, delta %lu, eps %lu\n",158pixclock, got, delta, epsilon);159}160161static enum fsl_diu_monitor_port162mpc512x_valid_monitor_port(enum fsl_diu_monitor_port port)163{164return FSL_DIU_PORT_DVI;165}166167static struct fsl_diu_shared_fb __attribute__ ((__aligned__(8))) diu_shared_fb;168169static inline void mpc512x_free_bootmem(struct page *page)170{171BUG_ON(PageTail(page));172BUG_ON(page_ref_count(page) > 1);173free_reserved_page(page);174}175176static void mpc512x_release_bootmem(void)177{178unsigned long addr = diu_shared_fb.fb_phys & PAGE_MASK;179unsigned long size = diu_shared_fb.fb_len;180unsigned long start, end;181182if (diu_shared_fb.in_use) {183start = PFN_UP(addr);184end = PFN_DOWN(addr + size);185186for (; start < end; start++)187mpc512x_free_bootmem(pfn_to_page(start));188189diu_shared_fb.in_use = false;190}191diu_ops.release_bootmem = NULL;192}193194/*195* Check if DIU was pre-initialized. If so, perform steps196* needed to continue displaying through the whole boot process.197* Move area descriptor and gamma table elsewhere, they are198* destroyed by bootmem allocator otherwise. The frame buffer199* address range will be reserved in setup_arch() after bootmem200* allocator is up.201*/202static void __init mpc512x_init_diu(void)203{204struct device_node *np;205struct diu __iomem *diu_reg;206phys_addr_t desc;207void __iomem *vaddr;208unsigned long mode, pix_fmt, res, bpp;209unsigned long dst;210211np = of_find_compatible_node(NULL, NULL, "fsl,mpc5121-diu");212if (!np) {213pr_err("No DIU node\n");214return;215}216217diu_reg = of_iomap(np, 0);218of_node_put(np);219if (!diu_reg) {220pr_err("Can't map DIU\n");221return;222}223224mode = in_be32(&diu_reg->diu_mode);225if (mode == MFB_MODE0) {226pr_info("%s: DIU OFF\n", __func__);227goto out;228}229230desc = in_be32(&diu_reg->desc[0]);231vaddr = ioremap(desc, sizeof(struct diu_ad));232if (!vaddr) {233pr_err("Can't map DIU area desc.\n");234goto out;235}236memcpy(&diu_shared_fb.ad0, vaddr, sizeof(struct diu_ad));237/* flush fb area descriptor */238dst = (unsigned long)&diu_shared_fb.ad0;239flush_dcache_range(dst, dst + sizeof(struct diu_ad) - 1);240241res = in_be32(&diu_reg->disp_size);242pix_fmt = in_le32(vaddr);243bpp = ((pix_fmt >> 16) & 0x3) + 1;244diu_shared_fb.fb_phys = in_le32(vaddr + 4);245diu_shared_fb.fb_len = ((res & 0xfff0000) >> 16) * (res & 0xfff) * bpp;246diu_shared_fb.in_use = true;247iounmap(vaddr);248249desc = in_be32(&diu_reg->gamma);250vaddr = ioremap(desc, sizeof(diu_shared_fb.gamma));251if (!vaddr) {252pr_err("Can't map DIU area desc.\n");253diu_shared_fb.in_use = false;254goto out;255}256memcpy(&diu_shared_fb.gamma, vaddr, sizeof(diu_shared_fb.gamma));257/* flush gamma table */258dst = (unsigned long)&diu_shared_fb.gamma;259flush_dcache_range(dst, dst + sizeof(diu_shared_fb.gamma) - 1);260261iounmap(vaddr);262out_be32(&diu_reg->gamma, virt_to_phys(&diu_shared_fb.gamma));263out_be32(&diu_reg->desc[1], 0);264out_be32(&diu_reg->desc[2], 0);265out_be32(&diu_reg->desc[0], virt_to_phys(&diu_shared_fb.ad0));266267out:268iounmap(diu_reg);269}270271static void __init mpc512x_setup_diu(void)272{273int ret;274275/*276* We do not allocate and configure new area for bitmap buffer277* because it would require copying bitmap data (splash image)278* and so negatively affect boot time. Instead we reserve the279* already configured frame buffer area so that it won't be280* destroyed. The starting address of the area to reserve and281* also its length is passed to memblock_reserve(). It will be282* freed later on first open of fbdev, when splash image is not283* needed any more.284*/285if (diu_shared_fb.in_use) {286ret = memblock_reserve(diu_shared_fb.fb_phys,287diu_shared_fb.fb_len);288if (ret) {289pr_err("%s: reserve bootmem failed\n", __func__);290diu_shared_fb.in_use = false;291}292}293294diu_ops.set_pixel_clock = mpc512x_set_pixel_clock;295diu_ops.valid_monitor_port = mpc512x_valid_monitor_port;296diu_ops.release_bootmem = mpc512x_release_bootmem;297}298299void __init mpc512x_init_IRQ(void)300{301struct device_node *np;302303np = of_find_compatible_node(NULL, NULL, "fsl,mpc5121-ipic");304if (!np)305return;306307ipic_init(np, 0);308of_node_put(np);309310/*311* Initialize the default interrupt mapping priorities,312* in case the boot rom changed something on us.313*/314ipic_set_default_priority();315}316317/*318* Nodes to do bus probe on, soc and localbus319*/320static const struct of_device_id of_bus_ids[] __initconst = {321{ .compatible = "fsl,mpc5121-immr", },322{ .compatible = "fsl,mpc5121-localbus", },323{ .compatible = "fsl,mpc5121-mbx", },324{ .compatible = "fsl,mpc5121-nfc", },325{ .compatible = "fsl,mpc5121-sram", },326{ .compatible = "fsl,mpc5121-pci", },327{ .compatible = "gpio-leds", },328{},329};330331static void __init mpc512x_declare_of_platform_devices(void)332{333if (of_platform_bus_probe(NULL, of_bus_ids, NULL))334printk(KERN_ERR __FILE__ ": "335"Error while probing of_platform bus\n");336}337338#define DEFAULT_FIFO_SIZE 16339340const char *__init mpc512x_select_psc_compat(void)341{342if (of_machine_is_compatible("fsl,mpc5121"))343return "fsl,mpc5121-psc";344345if (of_machine_is_compatible("fsl,mpc5125"))346return "fsl,mpc5125-psc";347348return NULL;349}350351static const char *__init mpc512x_select_reset_compat(void)352{353if (of_machine_is_compatible("fsl,mpc5121"))354return "fsl,mpc5121-reset";355356if (of_machine_is_compatible("fsl,mpc5125"))357return "fsl,mpc5125-reset";358359return NULL;360}361362static unsigned int __init get_fifo_size(struct device_node *np,363char *prop_name)364{365const unsigned int *fp;366367fp = of_get_property(np, prop_name, NULL);368if (fp)369return *fp;370371pr_warn("no %s property in %pOF node, defaulting to %d\n",372prop_name, np, DEFAULT_FIFO_SIZE);373374return DEFAULT_FIFO_SIZE;375}376377#define FIFOC(_base) ((struct mpc512x_psc_fifo __iomem *) \378((u32)(_base) + sizeof(struct mpc52xx_psc)))379380/* Init PSC FIFO space for TX and RX slices */381static void __init mpc512x_psc_fifo_init(void)382{383struct device_node *np;384void __iomem *psc;385unsigned int tx_fifo_size;386unsigned int rx_fifo_size;387const char *psc_compat;388int fifobase = 0; /* current fifo address in 32 bit words */389390psc_compat = mpc512x_select_psc_compat();391if (!psc_compat) {392pr_err("%s: no compatible devices found\n", __func__);393return;394}395396for_each_compatible_node(np, NULL, psc_compat) {397tx_fifo_size = get_fifo_size(np, "fsl,tx-fifo-size");398rx_fifo_size = get_fifo_size(np, "fsl,rx-fifo-size");399400/* size in register is in 4 byte units */401tx_fifo_size /= 4;402rx_fifo_size /= 4;403if (!tx_fifo_size)404tx_fifo_size = 1;405if (!rx_fifo_size)406rx_fifo_size = 1;407408psc = of_iomap(np, 0);409if (!psc) {410pr_err("%s: Can't map %pOF device\n",411__func__, np);412continue;413}414415/* FIFO space is 4KiB, check if requested size is available */416if ((fifobase + tx_fifo_size + rx_fifo_size) > 0x1000) {417pr_err("%s: no fifo space available for %pOF\n",418__func__, np);419iounmap(psc);420/*421* chances are that another device requests less422* fifo space, so we continue.423*/424continue;425}426427/* set tx and rx fifo size registers */428out_be32(&FIFOC(psc)->txsz, (fifobase << 16) | tx_fifo_size);429fifobase += tx_fifo_size;430out_be32(&FIFOC(psc)->rxsz, (fifobase << 16) | rx_fifo_size);431fifobase += rx_fifo_size;432433/* reset and enable the slices */434out_be32(&FIFOC(psc)->txcmd, 0x80);435out_be32(&FIFOC(psc)->txcmd, 0x01);436out_be32(&FIFOC(psc)->rxcmd, 0x80);437out_be32(&FIFOC(psc)->rxcmd, 0x01);438439iounmap(psc);440}441}442443static void __init mpc512x_restart_init(void)444{445struct device_node *np;446const char *reset_compat;447448reset_compat = mpc512x_select_reset_compat();449np = of_find_compatible_node(NULL, NULL, reset_compat);450if (!np)451return;452453reset_module_base = of_iomap(np, 0);454of_node_put(np);455}456457void __init mpc512x_init_early(void)458{459mpc512x_restart_init();460if (IS_ENABLED(CONFIG_FB_FSL_DIU))461mpc512x_init_diu();462}463464void __init mpc512x_init(void)465{466mpc5121_clk_init();467mpc512x_declare_of_platform_devices();468mpc512x_psc_fifo_init();469}470471void __init mpc512x_setup_arch(void)472{473if (IS_ENABLED(CONFIG_FB_FSL_DIU))474mpc512x_setup_diu();475}476477/**478* mpc512x_cs_config - Setup chip select configuration479* @cs: chip select number480* @val: chip select configuration value481*482* Perform chip select configuration for devices on LocalPlus Bus.483* Intended to dynamically reconfigure the chip select parameters484* for configurable devices on the bus.485*/486int mpc512x_cs_config(unsigned int cs, u32 val)487{488static struct mpc512x_lpc __iomem *lpc;489struct device_node *np;490491if (cs > 7)492return -EINVAL;493494if (!lpc) {495np = of_find_compatible_node(NULL, NULL, "fsl,mpc5121-lpc");496lpc = of_iomap(np, 0);497of_node_put(np);498if (!lpc)499return -ENOMEM;500}501502out_be32(&lpc->cs_cfg[cs], val);503return 0;504}505EXPORT_SYMBOL(mpc512x_cs_config);506507508