1 /* linux/drivers/mfd/sm501.c
 
   3  * Copyright (C) 2006 Simtec Electronics
 
   4  *      Ben Dooks <ben@simtec.co.uk>
 
   5  *      Vincent Sanders <vince@simtec.co.uk>
 
   7  * This program is free software; you can redistribute it and/or modify
 
   8  * it under the terms of the GNU General Public License version 2 as
 
   9  * published by the Free Software Foundation.
 
  14 #include <linux/kernel.h>
 
  15 #include <linux/module.h>
 
  16 #include <linux/delay.h>
 
  17 #include <linux/init.h>
 
  18 #include <linux/list.h>
 
  19 #include <linux/device.h>
 
  20 #include <linux/platform_device.h>
 
  21 #include <linux/pci.h>
 
  23 #include <linux/sm501.h>
 
  24 #include <linux/sm501-regs.h>
 
  29         struct list_head                list;
 
  30         struct platform_device          pdev;
 
  33 struct sm501_devdata {
 
  35         struct mutex                     clock_lock;
 
  36         struct list_head                 devices;
 
  39         struct resource                 *io_res;
 
  40         struct resource                 *mem_res;
 
  41         struct resource                 *regs_claim;
 
  42         struct sm501_platdata           *platdata;
 
  44         unsigned int                     in_suspend;
 
  45         unsigned long                    pm_misc;
 
  54 #define MHZ (1000 * 1000)
 
  57 static const unsigned int div_tab[] = {
 
  84 static unsigned long decode_div(unsigned long pll2, unsigned long val,
 
  85                                 unsigned int lshft, unsigned int selbit,
 
  91         return pll2 / div_tab[(val >> lshft) & mask];
 
  94 #define fmt_freq(x) ((x) / MHZ), ((x) % MHZ), (x)
 
  98  * Print out the current clock configuration for the device
 
 101 static void sm501_dump_clk(struct sm501_devdata *sm)
 
 103         unsigned long misct = readl(sm->regs + SM501_MISC_TIMING);
 
 104         unsigned long pm0 = readl(sm->regs + SM501_POWER_MODE_0_CLOCK);
 
 105         unsigned long pm1 = readl(sm->regs + SM501_POWER_MODE_1_CLOCK);
 
 106         unsigned long pmc = readl(sm->regs + SM501_POWER_MODE_CONTROL);
 
 107         unsigned long sdclk0, sdclk1;
 
 108         unsigned long pll2 = 0;
 
 110         switch (misct & 0x30) {
 
 125         sdclk0 = (misct & (1<<12)) ? pll2 : 288 * MHZ;
 
 126         sdclk0 /= div_tab[((misct >> 8) & 0xf)];
 
 128         sdclk1 = (misct & (1<<20)) ? pll2 : 288 * MHZ;
 
 129         sdclk1 /= div_tab[((misct >> 16) & 0xf)];
 
 131         dev_dbg(sm->dev, "MISCT=%08lx, PM0=%08lx, PM1=%08lx\n",
 
 134         dev_dbg(sm->dev, "PLL2 = %ld.%ld MHz (%ld), SDCLK0=%08lx, SDCLK1=%08lx\n",
 
 135                 fmt_freq(pll2), sdclk0, sdclk1);
 
 137         dev_dbg(sm->dev, "SDRAM: PM0=%ld, PM1=%ld\n", sdclk0, sdclk1);
 
 139         dev_dbg(sm->dev, "PM0[%c]: "
 
 140                  "P2 %ld.%ld MHz (%ld), V2 %ld.%ld (%ld), "
 
 141                  "M %ld.%ld (%ld), MX1 %ld.%ld (%ld)\n",
 
 142                  (pmc & 3 ) == 0 ? '*' : '-',
 
 143                  fmt_freq(decode_div(pll2, pm0, 24, 1<<29, 31)),
 
 144                  fmt_freq(decode_div(pll2, pm0, 16, 1<<20, 15)),
 
 145                  fmt_freq(decode_div(pll2, pm0, 8,  1<<12, 15)),
 
 146                  fmt_freq(decode_div(pll2, pm0, 0,  1<<4,  15)));
 
 148         dev_dbg(sm->dev, "PM1[%c]: "
 
 149                 "P2 %ld.%ld MHz (%ld), V2 %ld.%ld (%ld), "
 
 150                 "M %ld.%ld (%ld), MX1 %ld.%ld (%ld)\n",
 
 151                 (pmc & 3 ) == 1 ? '*' : '-',
 
 152                 fmt_freq(decode_div(pll2, pm1, 24, 1<<29, 31)),
 
 153                 fmt_freq(decode_div(pll2, pm1, 16, 1<<20, 15)),
 
 154                 fmt_freq(decode_div(pll2, pm1, 8,  1<<12, 15)),
 
 155                 fmt_freq(decode_div(pll2, pm1, 0,  1<<4,  15)));
 
 158 static void sm501_dump_regs(struct sm501_devdata *sm)
 
 160         void __iomem *regs = sm->regs;
 
 162         dev_info(sm->dev, "System Control   %08x\n",
 
 163                         readl(regs + SM501_SYSTEM_CONTROL));
 
 164         dev_info(sm->dev, "Misc Control     %08x\n",
 
 165                         readl(regs + SM501_MISC_CONTROL));
 
 166         dev_info(sm->dev, "GPIO Control Low %08x\n",
 
 167                         readl(regs + SM501_GPIO31_0_CONTROL));
 
 168         dev_info(sm->dev, "GPIO Control Hi  %08x\n",
 
 169                         readl(regs + SM501_GPIO63_32_CONTROL));
 
 170         dev_info(sm->dev, "DRAM Control     %08x\n",
 
 171                         readl(regs + SM501_DRAM_CONTROL));
 
 172         dev_info(sm->dev, "Arbitration Ctrl %08x\n",
 
 173                         readl(regs + SM501_ARBTRTN_CONTROL));
 
 174         dev_info(sm->dev, "Misc Timing      %08x\n",
 
 175                         readl(regs + SM501_MISC_TIMING));
 
 178 static void sm501_dump_gate(struct sm501_devdata *sm)
 
 180         dev_info(sm->dev, "CurrentGate      %08x\n",
 
 181                         readl(sm->regs + SM501_CURRENT_GATE));
 
 182         dev_info(sm->dev, "CurrentClock     %08x\n",
 
 183                         readl(sm->regs + SM501_CURRENT_CLOCK));
 
 184         dev_info(sm->dev, "PowerModeControl %08x\n",
 
 185                         readl(sm->regs + SM501_POWER_MODE_CONTROL));
 
 189 static inline void sm501_dump_gate(struct sm501_devdata *sm) { }
 
 190 static inline void sm501_dump_regs(struct sm501_devdata *sm) { }
 
 191 static inline void sm501_dump_clk(struct sm501_devdata *sm) { }
 
 199 static void sm501_sync_regs(struct sm501_devdata *sm)
 
 204 static inline void sm501_mdelay(struct sm501_devdata *sm, unsigned int delay)
 
 206         /* during suspend/resume, we are currently not allowed to sleep,
 
 207          * so change to using mdelay() instead of msleep() if we
 
 208          * are in one of these paths */
 
 216 /* sm501_misc_control
 
 218  * alters the miscellaneous control parameters
 
 221 int sm501_misc_control(struct device *dev,
 
 222                        unsigned long set, unsigned long clear)
 
 224         struct sm501_devdata *sm = dev_get_drvdata(dev);
 
 229         spin_lock_irqsave(&sm->reg_lock, save);
 
 231         misc = readl(sm->regs + SM501_MISC_CONTROL);
 
 232         to = (misc & ~clear) | set;
 
 235                 writel(to, sm->regs + SM501_MISC_CONTROL);
 
 238                 dev_dbg(sm->dev, "MISC_CONTROL %08lx\n", misc);
 
 241         spin_unlock_irqrestore(&sm->reg_lock, save);
 
 245 EXPORT_SYMBOL_GPL(sm501_misc_control);
 
 249  * Modify a register in the SM501 which may be shared with other
 
 253 unsigned long sm501_modify_reg(struct device *dev,
 
 258         struct sm501_devdata *sm = dev_get_drvdata(dev);
 
 262         spin_lock_irqsave(&sm->reg_lock, save);
 
 264         data = readl(sm->regs + reg);
 
 268         writel(data, sm->regs + reg);
 
 271         spin_unlock_irqrestore(&sm->reg_lock, save);
 
 276 EXPORT_SYMBOL_GPL(sm501_modify_reg);
 
 278 unsigned long sm501_gpio_get(struct device *dev,
 
 281         struct sm501_devdata *sm = dev_get_drvdata(dev);
 
 282         unsigned long result;
 
 285         reg = (gpio > 32) ? SM501_GPIO_DATA_HIGH : SM501_GPIO_DATA_LOW;
 
 286         result = readl(sm->regs + reg);
 
 288         result >>= (gpio & 31);
 
 292 EXPORT_SYMBOL_GPL(sm501_gpio_get);
 
 294 void sm501_gpio_set(struct device *dev,
 
 299         struct sm501_devdata *sm = dev_get_drvdata(dev);
 
 301         unsigned long bit = 1 << (gpio & 31);
 
 306         base = (gpio > 32) ? SM501_GPIO_DATA_HIGH : SM501_GPIO_DATA_LOW;
 
 309         spin_lock_irqsave(&sm->reg_lock, save);
 
 311         val = readl(sm->regs + base) & ~bit;
 
 314         writel(val, sm->regs + base);
 
 316         val = readl(sm->regs + SM501_GPIO_DDR_LOW) & ~bit;
 
 320         writel(val, sm->regs + SM501_GPIO_DDR_LOW);
 
 323         spin_unlock_irqrestore(&sm->reg_lock, save);
 
 327 EXPORT_SYMBOL_GPL(sm501_gpio_set);
 
 332  * alters the power active gate to set specific units on or off
 
 335 int sm501_unit_power(struct device *dev, unsigned int unit, unsigned int to)
 
 337         struct sm501_devdata *sm = dev_get_drvdata(dev);
 
 342         mutex_lock(&sm->clock_lock);
 
 344         mode = readl(sm->regs + SM501_POWER_MODE_CONTROL);
 
 345         gate = readl(sm->regs + SM501_CURRENT_GATE);
 
 346         clock = readl(sm->regs + SM501_CURRENT_CLOCK);
 
 348         mode &= 3;              /* get current power mode */
 
 350         if (unit >= ARRAY_SIZE(sm->unit_power)) {
 
 351                 dev_err(dev, "%s: bad unit %d\n", __FUNCTION__, unit);
 
 355         dev_dbg(sm->dev, "%s: unit %d, cur %d, to %d\n", __FUNCTION__, unit,
 
 356                 sm->unit_power[unit], to);
 
 358         if (to == 0 && sm->unit_power[unit] == 0) {
 
 359                 dev_err(sm->dev, "unit %d is already shutdown\n", unit);
 
 363         sm->unit_power[unit] += to ? 1 : -1;
 
 364         to = sm->unit_power[unit] ? 1 : 0;
 
 367                 if (gate & (1 << unit))
 
 371                 if (!(gate & (1 << unit)))
 
 373                 gate &= ~(1 << unit);
 
 378                 writel(gate, sm->regs + SM501_POWER_MODE_0_GATE);
 
 379                 writel(clock, sm->regs + SM501_POWER_MODE_0_CLOCK);
 
 384                 writel(gate, sm->regs + SM501_POWER_MODE_1_GATE);
 
 385                 writel(clock, sm->regs + SM501_POWER_MODE_1_CLOCK);
 
 393         writel(mode, sm->regs + SM501_POWER_MODE_CONTROL);
 
 396         dev_dbg(sm->dev, "gate %08lx, clock %08lx, mode %08lx\n",
 
 399         sm501_mdelay(sm, 16);
 
 402         mutex_unlock(&sm->clock_lock);
 
 406 EXPORT_SYMBOL_GPL(sm501_unit_power);
 
 409 /* Perform a rounded division. */
 
 410 static long sm501fb_round_div(long num, long denom)
 
 412         /* n / d + 1 / 2 = (2n + d) / 2d */
 
 413         return (2 * num + denom) / (2 * denom);
 
 416 /* clock value structure. */
 
 421         unsigned int m, n, k;
 
 426  * Calculates the nearest discrete clock frequency that
 
 427  * can be achieved with the specified input clock.
 
 428  *   the maximum divisor is 3 or 5
 
 431 static int sm501_calc_clock(unsigned long freq,
 
 432                             struct sm501_clock *clock,
 
 442         /* try dividers 1 and 3 for CRT and for panel,
 
 443            try divider 5 for panel only.*/
 
 445         for (divider = 1; divider <= max_div; divider += 2) {
 
 446                 /* try all 8 shift values.*/
 
 447                 for (shift = 0; shift < 8; shift++) {
 
 448                         /* Calculate difference to requested clock */
 
 449                         diff = sm501fb_round_div(mclk, divider << shift) - freq;
 
 453                         /* If it is less than the current, use it */
 
 454                         if (diff < *best_diff) {
 
 458                                 clock->divider = divider;
 
 459                                 clock->shift = shift;
 
 470  * Calculates the nearest discrete clock frequency that can be
 
 471  * achieved using the programmable PLL.
 
 472  *   the maximum divisor is 3 or 5
 
 475 static unsigned long sm501_calc_pll(unsigned long freq,
 
 476                                         struct sm501_clock *clock,
 
 480         unsigned int m, n, k;
 
 481         long best_diff = 999999999;
 
 484          * The SM502 datasheet doesn't specify the min/max values for M and N.
 
 485          * N = 1 at least doesn't work in practice.
 
 487         for (m = 2; m <= 255; m++) {
 
 488                 for (n = 2; n <= 127; n++) {
 
 489                         for (k = 0; k <= 1; k++) {
 
 490                                 mclk = (24000000UL * m / n) >> k;
 
 492                                 if (sm501_calc_clock(freq, clock, max_div,
 
 502         /* Return best clock. */
 
 503         return clock->mclk / (clock->divider << clock->shift);
 
 506 /* sm501_select_clock
 
 508  * Calculates the nearest discrete clock frequency that can be
 
 509  * achieved using the 288MHz and 336MHz PLLs.
 
 510  *   the maximum divisor is 3 or 5
 
 513 static unsigned long sm501_select_clock(unsigned long freq,
 
 514                                         struct sm501_clock *clock,
 
 518         long best_diff = 999999999;
 
 520         /* Try 288MHz and 336MHz clocks. */
 
 521         for (mclk = 288000000; mclk <= 336000000; mclk += 48000000) {
 
 522                 sm501_calc_clock(freq, clock, max_div, mclk, &best_diff);
 
 525         /* Return best clock. */
 
 526         return clock->mclk / (clock->divider << clock->shift);
 
 531  * set one of the four clock sources to the closest available frequency to
 
 535 unsigned long sm501_set_clock(struct device *dev,
 
 537                               unsigned long req_freq)
 
 539         struct sm501_devdata *sm = dev_get_drvdata(dev);
 
 540         unsigned long mode = readl(sm->regs + SM501_POWER_MODE_CONTROL);
 
 541         unsigned long gate = readl(sm->regs + SM501_CURRENT_GATE);
 
 542         unsigned long clock = readl(sm->regs + SM501_CURRENT_CLOCK);
 
 544         unsigned int pll_reg = 0;
 
 545         unsigned long sm501_freq; /* the actual frequency acheived */
 
 547         struct sm501_clock to;
 
 549         /* find achivable discrete frequency and setup register value
 
 550          * accordingly, V2XCLK, MCLK and M1XCLK are the same P2XCLK
 
 551          * has an extra bit for the divider */
 
 554         case SM501_CLOCK_P2XCLK:
 
 555                 /* This clock is divided in half so to achive the
 
 556                  * requested frequency the value must be multiplied by
 
 557                  * 2. This clock also has an additional pre divisor */
 
 559                 if (sm->rev >= 0xC0) {
 
 560                         /* SM502 -> use the programmable PLL */
 
 561                         sm501_freq = (sm501_calc_pll(2 * req_freq,
 
 563                         reg = to.shift & 0x07;/* bottom 3 bits are shift */
 
 565                                 reg |= 0x08; /* /3 divider required */
 
 566                         else if (to.divider == 5)
 
 567                                 reg |= 0x10; /* /5 divider required */
 
 568                         reg |= 0x40; /* select the programmable PLL */
 
 569                         pll_reg = 0x20000 | (to.k << 15) | (to.n << 8) | to.m;
 
 571                         sm501_freq = (sm501_select_clock(2 * req_freq,
 
 573                         reg = to.shift & 0x07;/* bottom 3 bits are shift */
 
 575                                 reg |= 0x08; /* /3 divider required */
 
 576                         else if (to.divider == 5)
 
 577                                 reg |= 0x10; /* /5 divider required */
 
 578                         if (to.mclk != 288000000)
 
 579                                 reg |= 0x20; /* which mclk pll is source */
 
 583         case SM501_CLOCK_V2XCLK:
 
 584                 /* This clock is divided in half so to achive the
 
 585                  * requested frequency the value must be multiplied by 2. */
 
 587                 sm501_freq = (sm501_select_clock(2 * req_freq, &to, 3) / 2);
 
 588                 reg=to.shift & 0x07;    /* bottom 3 bits are shift */
 
 590                         reg |= 0x08;    /* /3 divider required */
 
 591                 if (to.mclk != 288000000)
 
 592                         reg |= 0x10;    /* which mclk pll is source */
 
 595         case SM501_CLOCK_MCLK:
 
 596         case SM501_CLOCK_M1XCLK:
 
 597                 /* These clocks are the same and not further divided */
 
 599                 sm501_freq = sm501_select_clock( req_freq, &to, 3);
 
 600                 reg=to.shift & 0x07;    /* bottom 3 bits are shift */
 
 602                         reg |= 0x08;    /* /3 divider required */
 
 603                 if (to.mclk != 288000000)
 
 604                         reg |= 0x10;    /* which mclk pll is source */
 
 608                 return 0; /* this is bad */
 
 611         mutex_lock(&sm->clock_lock);
 
 613         mode = readl(sm->regs + SM501_POWER_MODE_CONTROL);
 
 614         gate = readl(sm->regs + SM501_CURRENT_GATE);
 
 615         clock = readl(sm->regs + SM501_CURRENT_CLOCK);
 
 617         clock = clock & ~(0xFF << clksrc);
 
 618         clock |= reg<<clksrc;
 
 620         mode &= 3;      /* find current mode */
 
 624                 writel(gate, sm->regs + SM501_POWER_MODE_0_GATE);
 
 625                 writel(clock, sm->regs + SM501_POWER_MODE_0_CLOCK);
 
 630                 writel(gate, sm->regs + SM501_POWER_MODE_1_GATE);
 
 631                 writel(clock, sm->regs + SM501_POWER_MODE_1_CLOCK);
 
 636                 mutex_unlock(&sm->clock_lock);
 
 640         writel(mode, sm->regs + SM501_POWER_MODE_CONTROL);
 
 643                 writel(pll_reg, sm->regs + SM501_PROGRAMMABLE_PLL_CONTROL);
 
 647         dev_info(sm->dev, "gate %08lx, clock %08lx, mode %08lx\n",
 
 650         sm501_mdelay(sm, 16);
 
 651         mutex_unlock(&sm->clock_lock);
 
 658 EXPORT_SYMBOL_GPL(sm501_set_clock);
 
 662  * finds the closest available frequency for a given clock
 
 665 unsigned long sm501_find_clock(struct device *dev,
 
 667                                unsigned long req_freq)
 
 669         struct sm501_devdata *sm = dev_get_drvdata(dev);
 
 670         unsigned long sm501_freq; /* the frequency achiveable by the 501 */
 
 671         struct sm501_clock to;
 
 674         case SM501_CLOCK_P2XCLK:
 
 675                 if (sm->rev >= 0xC0) {
 
 676                         /* SM502 -> use the programmable PLL */
 
 677                         sm501_freq = (sm501_calc_pll(2 * req_freq,
 
 680                         sm501_freq = (sm501_select_clock(2 * req_freq,
 
 685         case SM501_CLOCK_V2XCLK:
 
 686                 sm501_freq = (sm501_select_clock(2 * req_freq, &to, 3) / 2);
 
 689         case SM501_CLOCK_MCLK:
 
 690         case SM501_CLOCK_M1XCLK:
 
 691                 sm501_freq = sm501_select_clock(req_freq, &to, 3);
 
 695                 sm501_freq = 0;         /* error */
 
 701 EXPORT_SYMBOL_GPL(sm501_find_clock);
 
 703 static struct sm501_device *to_sm_device(struct platform_device *pdev)
 
 705         return container_of(pdev, struct sm501_device, pdev);
 
 708 /* sm501_device_release
 
 710  * A release function for the platform devices we create to allow us to
 
 711  * free any items we allocated
 
 714 static void sm501_device_release(struct device *dev)
 
 716         kfree(to_sm_device(to_platform_device(dev)));
 
 719 /* sm501_create_subdev
 
 721  * Create a skeleton platform device with resources for passing to a
 
 725 static struct platform_device *
 
 726 sm501_create_subdev(struct sm501_devdata *sm,
 
 727                     char *name, unsigned int res_count)
 
 729         struct sm501_device *smdev;
 
 731         smdev = kzalloc(sizeof(struct sm501_device) +
 
 732                         sizeof(struct resource) * res_count, GFP_KERNEL);
 
 736         smdev->pdev.dev.release = sm501_device_release;
 
 738         smdev->pdev.name = name;
 
 739         smdev->pdev.id = sm->pdev_id;
 
 740         smdev->pdev.resource = (struct resource *)(smdev+1);
 
 741         smdev->pdev.num_resources = res_count;
 
 743         smdev->pdev.dev.parent = sm->dev;
 
 748 /* sm501_register_device
 
 750  * Register a platform device created with sm501_create_subdev()
 
 753 static int sm501_register_device(struct sm501_devdata *sm,
 
 754                                  struct platform_device *pdev)
 
 756         struct sm501_device *smdev = to_sm_device(pdev);
 
 760         for (ptr = 0; ptr < pdev->num_resources; ptr++) {
 
 761                 printk("%s[%d] flags %08lx: %08llx..%08llx\n",
 
 763                        pdev->resource[ptr].flags,
 
 764                        (unsigned long long)pdev->resource[ptr].start,
 
 765                        (unsigned long long)pdev->resource[ptr].end);
 
 768         ret = platform_device_register(pdev);
 
 771                 dev_dbg(sm->dev, "registered %s\n", pdev->name);
 
 772                 list_add_tail(&smdev->list, &sm->devices);
 
 774                 dev_err(sm->dev, "error registering %s (%d)\n",
 
 780 /* sm501_create_subio
 
 782  * Fill in an IO resource for a sub device
 
 785 static void sm501_create_subio(struct sm501_devdata *sm,
 
 786                                struct resource *res,
 
 787                                resource_size_t offs,
 
 788                                resource_size_t size)
 
 790         res->flags = IORESOURCE_MEM;
 
 791         res->parent = sm->io_res;
 
 792         res->start = sm->io_res->start + offs;
 
 793         res->end = res->start + size - 1;
 
 798  * Fill in an MEM resource for a sub device
 
 801 static void sm501_create_mem(struct sm501_devdata *sm,
 
 802                              struct resource *res,
 
 803                              resource_size_t *offs,
 
 804                              resource_size_t size)
 
 806         *offs -= size;          /* adjust memory size */
 
 808         res->flags = IORESOURCE_MEM;
 
 809         res->parent = sm->mem_res;
 
 810         res->start = sm->mem_res->start + *offs;
 
 811         res->end = res->start + size - 1;
 
 816  * Fill in an IRQ resource for a sub device
 
 819 static void sm501_create_irq(struct sm501_devdata *sm,
 
 820                              struct resource *res)
 
 822         res->flags = IORESOURCE_IRQ;
 
 824         res->start = res->end = sm->irq;
 
 827 static int sm501_register_usbhost(struct sm501_devdata *sm,
 
 828                                   resource_size_t *mem_avail)
 
 830         struct platform_device *pdev;
 
 832         pdev = sm501_create_subdev(sm, "sm501-usb", 3);
 
 836         sm501_create_subio(sm, &pdev->resource[0], 0x40000, 0x20000);
 
 837         sm501_create_mem(sm, &pdev->resource[1], mem_avail, 256*1024);
 
 838         sm501_create_irq(sm, &pdev->resource[2]);
 
 840         return sm501_register_device(sm, pdev);
 
 843 static int sm501_register_display(struct sm501_devdata *sm,
 
 844                                   resource_size_t *mem_avail)
 
 846         struct platform_device *pdev;
 
 848         pdev = sm501_create_subdev(sm, "sm501-fb", 4);
 
 852         sm501_create_subio(sm, &pdev->resource[0], 0x80000, 0x10000);
 
 853         sm501_create_subio(sm, &pdev->resource[1], 0x100000, 0x50000);
 
 854         sm501_create_mem(sm, &pdev->resource[2], mem_avail, *mem_avail);
 
 855         sm501_create_irq(sm, &pdev->resource[3]);
 
 857         return sm501_register_device(sm, pdev);
 
 862  * Debug attribute to attach to parent device to show core registers
 
 865 static ssize_t sm501_dbg_regs(struct device *dev,
 
 866                               struct device_attribute *attr, char *buff)
 
 868         struct sm501_devdata *sm = dev_get_drvdata(dev) ;
 
 873         for (reg = 0x00; reg < 0x70; reg += 4) {
 
 874                 ret = sprintf(ptr, "%08x = %08x\n",
 
 875                               reg, readl(sm->regs + reg));
 
 883 static DEVICE_ATTR(dbg_regs, 0666, sm501_dbg_regs, NULL);
 
 887  * Helper function for the init code to setup a register
 
 889  * clear the bits which are set in r->mask, and then set
 
 890  * the bits set in r->set.
 
 893 static inline void sm501_init_reg(struct sm501_devdata *sm,
 
 895                                   struct sm501_reg_init *r)
 
 899         tmp = readl(sm->regs + reg);
 
 902         writel(tmp, sm->regs + reg);
 
 907  * Setup core register values
 
 910 static void sm501_init_regs(struct sm501_devdata *sm,
 
 911                             struct sm501_initdata *init)
 
 913         sm501_misc_control(sm->dev,
 
 914                            init->misc_control.set,
 
 915                            init->misc_control.mask);
 
 917         sm501_init_reg(sm, SM501_MISC_TIMING, &init->misc_timing);
 
 918         sm501_init_reg(sm, SM501_GPIO31_0_CONTROL, &init->gpio_low);
 
 919         sm501_init_reg(sm, SM501_GPIO63_32_CONTROL, &init->gpio_high);
 
 922                 dev_info(sm->dev, "setting M1XCLK to %ld\n", init->m1xclk);
 
 923                 sm501_set_clock(sm->dev, SM501_CLOCK_M1XCLK, init->m1xclk);
 
 927                 dev_info(sm->dev, "setting MCLK to %ld\n", init->mclk);
 
 928                 sm501_set_clock(sm->dev, SM501_CLOCK_MCLK, init->mclk);
 
 933 /* Check the PLL sources for the M1CLK and M1XCLK
 
 935  * If the M1CLK and M1XCLKs are not sourced from the same PLL, then
 
 936  * there is a risk (see errata AB-5) that the SM501 will cease proper
 
 937  * function. If this happens, then it is likely the SM501 will
 
 941 static int sm501_check_clocks(struct sm501_devdata *sm)
 
 943         unsigned long pwrmode = readl(sm->regs + SM501_CURRENT_CLOCK);
 
 944         unsigned long msrc = (pwrmode & SM501_POWERMODE_M_SRC);
 
 945         unsigned long m1src = (pwrmode & SM501_POWERMODE_M1_SRC);
 
 947         return ((msrc == 0 && m1src != 0) || (msrc != 0 && m1src == 0));
 
 950 static unsigned int sm501_mem_local[] = {
 
 961  * Common init code for an SM501
 
 964 static int sm501_init_dev(struct sm501_devdata *sm)
 
 966         resource_size_t mem_avail;
 
 967         unsigned long dramctrl;
 
 971         mutex_init(&sm->clock_lock);
 
 972         spin_lock_init(&sm->reg_lock);
 
 974         INIT_LIST_HEAD(&sm->devices);
 
 976         devid = readl(sm->regs + SM501_DEVICEID);
 
 978         if ((devid & SM501_DEVICEID_IDMASK) != SM501_DEVICEID_SM501) {
 
 979                 dev_err(sm->dev, "incorrect device id %08lx\n", devid);
 
 983         dramctrl = readl(sm->regs + SM501_DRAM_CONTROL);
 
 984         mem_avail = sm501_mem_local[(dramctrl >> 13) & 0x7];
 
 986         dev_info(sm->dev, "SM501 At %p: Version %08lx, %ld Mb, IRQ %d\n",
 
 987                  sm->regs, devid, (unsigned long)mem_avail >> 20, sm->irq);
 
 989         sm->rev = devid & SM501_DEVICEID_REVMASK;
 
 993         ret = device_create_file(sm->dev, &dev_attr_dbg_regs);
 
 995                 dev_err(sm->dev, "failed to create debug regs file\n");
 
 999         /* check to see if we have some device initialisation */
 
1002                 struct sm501_platdata *pdata = sm->platdata;
 
1005                         sm501_init_regs(sm, sm->platdata->init);
 
1007                         if (pdata->init->devices & SM501_USE_USB_HOST)
 
1008                                 sm501_register_usbhost(sm, &mem_avail);
 
1012         ret = sm501_check_clocks(sm);
 
1014                 dev_err(sm->dev, "M1X and M clocks sourced from different "
 
1019         /* always create a framebuffer */
 
1020         sm501_register_display(sm, &mem_avail);
 
1025 static int sm501_plat_probe(struct platform_device *dev)
 
1027         struct sm501_devdata *sm;
 
1030         sm = kzalloc(sizeof(struct sm501_devdata), GFP_KERNEL);
 
1032                 dev_err(&dev->dev, "no memory for device data\n");
 
1037         sm->dev = &dev->dev;
 
1038         sm->pdev_id = dev->id;
 
1039         sm->irq = platform_get_irq(dev, 0);
 
1040         sm->io_res = platform_get_resource(dev, IORESOURCE_MEM, 1);
 
1041         sm->mem_res = platform_get_resource(dev, IORESOURCE_MEM, 0);
 
1042         sm->platdata = dev->dev.platform_data;
 
1045                 dev_err(&dev->dev, "failed to get irq resource\n");
 
1050         if (sm->io_res == NULL || sm->mem_res == NULL) {
 
1051                 dev_err(&dev->dev, "failed to get IO resource\n");
 
1056         sm->regs_claim = request_mem_region(sm->io_res->start,
 
1059         if (sm->regs_claim == NULL) {
 
1060                 dev_err(&dev->dev, "cannot claim registers\n");
 
1065         platform_set_drvdata(dev, sm);
 
1067         sm->regs = ioremap(sm->io_res->start,
 
1068                            (sm->io_res->end - sm->io_res->start) - 1);
 
1070         if (sm->regs == NULL) {
 
1071                 dev_err(&dev->dev, "cannot remap registers\n");
 
1076         return sm501_init_dev(sm);
 
1079         release_resource(sm->regs_claim);
 
1080         kfree(sm->regs_claim);
 
1089 /* power management support */
 
1091 static int sm501_plat_suspend(struct platform_device *pdev, pm_message_t state)
 
1093         struct sm501_devdata *sm = platform_get_drvdata(pdev);
 
1096         sm->pm_misc = readl(sm->regs + SM501_MISC_CONTROL);
 
1098         sm501_dump_regs(sm);
 
1102 static int sm501_plat_resume(struct platform_device *pdev)
 
1104         struct sm501_devdata *sm = platform_get_drvdata(pdev);
 
1106         sm501_dump_regs(sm);
 
1107         sm501_dump_gate(sm);
 
1110         /* check to see if we are in the same state as when suspended */
 
1112         if (readl(sm->regs + SM501_MISC_CONTROL) != sm->pm_misc) {
 
1113                 dev_info(sm->dev, "SM501_MISC_CONTROL changed over sleep\n");
 
1114                 writel(sm->pm_misc, sm->regs + SM501_MISC_CONTROL);
 
1116                 /* our suspend causes the controller state to change,
 
1117                  * either by something attempting setup, power loss,
 
1118                  * or an external reset event on power change */
 
1120                 if (sm->platdata && sm->platdata->init) {
 
1121                         sm501_init_regs(sm, sm->platdata->init);
 
1125         /* dump our state from resume */
 
1127         sm501_dump_regs(sm);
 
1135 #define sm501_plat_suspend NULL
 
1136 #define sm501_plat_resume NULL
 
1139 /* Initialisation data for PCI devices */
 
1141 static struct sm501_initdata sm501_pci_initdata = {
 
1143                 .set    = 0x3F000000,           /* 24bit panel */
 
1147                 .set    = 0x010100,             /* SDRAM timing */
 
1151                 .set    = SM501_MISC_PNL_24BIT,
 
1155         .devices        = SM501_USE_ALL,
 
1157         /* Errata AB-3 says that 72MHz is the fastest available
 
1158          * for 33MHZ PCI with proper bus-mastering operation */
 
1161         .m1xclk         = 144 * MHZ,
 
1164 static struct sm501_platdata_fbsub sm501_pdata_fbsub = {
 
1165         .flags          = (SM501FB_FLAG_USE_INIT_MODE |
 
1166                            SM501FB_FLAG_USE_HWCURSOR |
 
1167                            SM501FB_FLAG_USE_HWACCEL |
 
1168                            SM501FB_FLAG_DISABLE_AT_EXIT),
 
1171 static struct sm501_platdata_fb sm501_fb_pdata = {
 
1172         .fb_route       = SM501_FB_OWN,
 
1173         .fb_crt         = &sm501_pdata_fbsub,
 
1174         .fb_pnl         = &sm501_pdata_fbsub,
 
1177 static struct sm501_platdata sm501_pci_platdata = {
 
1178         .init           = &sm501_pci_initdata,
 
1179         .fb             = &sm501_fb_pdata,
 
1182 static int sm501_pci_probe(struct pci_dev *dev,
 
1183                            const struct pci_device_id *id)
 
1185         struct sm501_devdata *sm;
 
1188         sm = kzalloc(sizeof(struct sm501_devdata), GFP_KERNEL);
 
1190                 dev_err(&dev->dev, "no memory for device data\n");
 
1195         /* set a default set of platform data */
 
1196         dev->dev.platform_data = sm->platdata = &sm501_pci_platdata;
 
1198         /* set a hopefully unique id for our child platform devices */
 
1199         sm->pdev_id = 32 + dev->devfn;
 
1201         pci_set_drvdata(dev, sm);
 
1203         err = pci_enable_device(dev);
 
1205                 dev_err(&dev->dev, "cannot enable device\n");
 
1209         sm->dev = &dev->dev;
 
1213         /* if the system is big-endian, we most probably have a
 
1214          * translation in the IO layer making the PCI bus little endian
 
1215          * so make the framebuffer swapped pixels */
 
1217         sm501_fb_pdata.flags |= SM501_FBPD_SWAP_FB_ENDIAN;
 
1220         /* check our resources */
 
1222         if (!(pci_resource_flags(dev, 0) & IORESOURCE_MEM)) {
 
1223                 dev_err(&dev->dev, "region #0 is not memory?\n");
 
1228         if (!(pci_resource_flags(dev, 1) & IORESOURCE_MEM)) {
 
1229                 dev_err(&dev->dev, "region #1 is not memory?\n");
 
1234         /* make our resources ready for sharing */
 
1236         sm->io_res = &dev->resource[1];
 
1237         sm->mem_res = &dev->resource[0];
 
1239         sm->regs_claim = request_mem_region(sm->io_res->start,
 
1241         if (sm->regs_claim == NULL) {
 
1242                 dev_err(&dev->dev, "cannot claim registers\n");
 
1247         sm->regs = ioremap(pci_resource_start(dev, 1),
 
1248                            pci_resource_len(dev, 1));
 
1250         if (sm->regs == NULL) {
 
1251                 dev_err(&dev->dev, "cannot remap registers\n");
 
1260         release_resource(sm->regs_claim);
 
1261         kfree(sm->regs_claim);
 
1263         pci_disable_device(dev);
 
1265         pci_set_drvdata(dev, NULL);
 
1271 static void sm501_remove_sub(struct sm501_devdata *sm,
 
1272                              struct sm501_device *smdev)
 
1274         list_del(&smdev->list);
 
1275         platform_device_unregister(&smdev->pdev);
 
1278 static void sm501_dev_remove(struct sm501_devdata *sm)
 
1280         struct sm501_device *smdev, *tmp;
 
1282         list_for_each_entry_safe(smdev, tmp, &sm->devices, list)
 
1283                 sm501_remove_sub(sm, smdev);
 
1285         device_remove_file(sm->dev, &dev_attr_dbg_regs);
 
1288 static void sm501_pci_remove(struct pci_dev *dev)
 
1290         struct sm501_devdata *sm = pci_get_drvdata(dev);
 
1292         sm501_dev_remove(sm);
 
1295         release_resource(sm->regs_claim);
 
1296         kfree(sm->regs_claim);
 
1298         pci_set_drvdata(dev, NULL);
 
1299         pci_disable_device(dev);
 
1302 static int sm501_plat_remove(struct platform_device *dev)
 
1304         struct sm501_devdata *sm = platform_get_drvdata(dev);
 
1306         sm501_dev_remove(sm);
 
1309         release_resource(sm->regs_claim);
 
1310         kfree(sm->regs_claim);
 
1315 static struct pci_device_id sm501_pci_tbl[] = {
 
1316         { 0x126f, 0x0501, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
 
1320 MODULE_DEVICE_TABLE(pci, sm501_pci_tbl);
 
1322 static struct pci_driver sm501_pci_drv = {
 
1324         .id_table       = sm501_pci_tbl,
 
1325         .probe          = sm501_pci_probe,
 
1326         .remove         = sm501_pci_remove,
 
1329 static struct platform_driver sm501_plat_drv = {
 
1332                 .owner  = THIS_MODULE,
 
1334         .probe          = sm501_plat_probe,
 
1335         .remove         = sm501_plat_remove,
 
1336         .suspend        = sm501_plat_suspend,
 
1337         .resume         = sm501_plat_resume,
 
1340 static int __init sm501_base_init(void)
 
1342         platform_driver_register(&sm501_plat_drv);
 
1343         return pci_register_driver(&sm501_pci_drv);
 
1346 static void __exit sm501_base_exit(void)
 
1348         platform_driver_unregister(&sm501_plat_drv);
 
1349         pci_unregister_driver(&sm501_pci_drv);
 
1352 module_init(sm501_base_init);
 
1353 module_exit(sm501_base_exit);
 
1355 MODULE_DESCRIPTION("SM501 Core Driver");
 
1356 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>, Vincent Sanders");
 
1357 MODULE_LICENSE("GPL v2");