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>
25 #include <linux/serial_8250.h>
30 struct list_head list;
31 struct platform_device pdev;
34 struct sm501_devdata {
36 struct mutex clock_lock;
37 struct list_head devices;
40 struct resource *io_res;
41 struct resource *mem_res;
42 struct resource *regs_claim;
43 struct sm501_platdata *platdata;
45 unsigned int in_suspend;
46 unsigned long pm_misc;
55 #define MHZ (1000 * 1000)
58 static const unsigned int div_tab[] = {
85 static unsigned long decode_div(unsigned long pll2, unsigned long val,
86 unsigned int lshft, unsigned int selbit,
92 return pll2 / div_tab[(val >> lshft) & mask];
95 #define fmt_freq(x) ((x) / MHZ), ((x) % MHZ), (x)
99 * Print out the current clock configuration for the device
102 static void sm501_dump_clk(struct sm501_devdata *sm)
104 unsigned long misct = readl(sm->regs + SM501_MISC_TIMING);
105 unsigned long pm0 = readl(sm->regs + SM501_POWER_MODE_0_CLOCK);
106 unsigned long pm1 = readl(sm->regs + SM501_POWER_MODE_1_CLOCK);
107 unsigned long pmc = readl(sm->regs + SM501_POWER_MODE_CONTROL);
108 unsigned long sdclk0, sdclk1;
109 unsigned long pll2 = 0;
111 switch (misct & 0x30) {
126 sdclk0 = (misct & (1<<12)) ? pll2 : 288 * MHZ;
127 sdclk0 /= div_tab[((misct >> 8) & 0xf)];
129 sdclk1 = (misct & (1<<20)) ? pll2 : 288 * MHZ;
130 sdclk1 /= div_tab[((misct >> 16) & 0xf)];
132 dev_dbg(sm->dev, "MISCT=%08lx, PM0=%08lx, PM1=%08lx\n",
135 dev_dbg(sm->dev, "PLL2 = %ld.%ld MHz (%ld), SDCLK0=%08lx, SDCLK1=%08lx\n",
136 fmt_freq(pll2), sdclk0, sdclk1);
138 dev_dbg(sm->dev, "SDRAM: PM0=%ld, PM1=%ld\n", sdclk0, sdclk1);
140 dev_dbg(sm->dev, "PM0[%c]: "
141 "P2 %ld.%ld MHz (%ld), V2 %ld.%ld (%ld), "
142 "M %ld.%ld (%ld), MX1 %ld.%ld (%ld)\n",
143 (pmc & 3 ) == 0 ? '*' : '-',
144 fmt_freq(decode_div(pll2, pm0, 24, 1<<29, 31)),
145 fmt_freq(decode_div(pll2, pm0, 16, 1<<20, 15)),
146 fmt_freq(decode_div(pll2, pm0, 8, 1<<12, 15)),
147 fmt_freq(decode_div(pll2, pm0, 0, 1<<4, 15)));
149 dev_dbg(sm->dev, "PM1[%c]: "
150 "P2 %ld.%ld MHz (%ld), V2 %ld.%ld (%ld), "
151 "M %ld.%ld (%ld), MX1 %ld.%ld (%ld)\n",
152 (pmc & 3 ) == 1 ? '*' : '-',
153 fmt_freq(decode_div(pll2, pm1, 24, 1<<29, 31)),
154 fmt_freq(decode_div(pll2, pm1, 16, 1<<20, 15)),
155 fmt_freq(decode_div(pll2, pm1, 8, 1<<12, 15)),
156 fmt_freq(decode_div(pll2, pm1, 0, 1<<4, 15)));
159 static void sm501_dump_regs(struct sm501_devdata *sm)
161 void __iomem *regs = sm->regs;
163 dev_info(sm->dev, "System Control %08x\n",
164 readl(regs + SM501_SYSTEM_CONTROL));
165 dev_info(sm->dev, "Misc Control %08x\n",
166 readl(regs + SM501_MISC_CONTROL));
167 dev_info(sm->dev, "GPIO Control Low %08x\n",
168 readl(regs + SM501_GPIO31_0_CONTROL));
169 dev_info(sm->dev, "GPIO Control Hi %08x\n",
170 readl(regs + SM501_GPIO63_32_CONTROL));
171 dev_info(sm->dev, "DRAM Control %08x\n",
172 readl(regs + SM501_DRAM_CONTROL));
173 dev_info(sm->dev, "Arbitration Ctrl %08x\n",
174 readl(regs + SM501_ARBTRTN_CONTROL));
175 dev_info(sm->dev, "Misc Timing %08x\n",
176 readl(regs + SM501_MISC_TIMING));
179 static void sm501_dump_gate(struct sm501_devdata *sm)
181 dev_info(sm->dev, "CurrentGate %08x\n",
182 readl(sm->regs + SM501_CURRENT_GATE));
183 dev_info(sm->dev, "CurrentClock %08x\n",
184 readl(sm->regs + SM501_CURRENT_CLOCK));
185 dev_info(sm->dev, "PowerModeControl %08x\n",
186 readl(sm->regs + SM501_POWER_MODE_CONTROL));
190 static inline void sm501_dump_gate(struct sm501_devdata *sm) { }
191 static inline void sm501_dump_regs(struct sm501_devdata *sm) { }
192 static inline void sm501_dump_clk(struct sm501_devdata *sm) { }
200 static void sm501_sync_regs(struct sm501_devdata *sm)
205 static inline void sm501_mdelay(struct sm501_devdata *sm, unsigned int delay)
207 /* during suspend/resume, we are currently not allowed to sleep,
208 * so change to using mdelay() instead of msleep() if we
209 * are in one of these paths */
217 /* sm501_misc_control
219 * alters the miscellaneous control parameters
222 int sm501_misc_control(struct device *dev,
223 unsigned long set, unsigned long clear)
225 struct sm501_devdata *sm = dev_get_drvdata(dev);
230 spin_lock_irqsave(&sm->reg_lock, save);
232 misc = readl(sm->regs + SM501_MISC_CONTROL);
233 to = (misc & ~clear) | set;
236 writel(to, sm->regs + SM501_MISC_CONTROL);
239 dev_dbg(sm->dev, "MISC_CONTROL %08lx\n", misc);
242 spin_unlock_irqrestore(&sm->reg_lock, save);
246 EXPORT_SYMBOL_GPL(sm501_misc_control);
250 * Modify a register in the SM501 which may be shared with other
254 unsigned long sm501_modify_reg(struct device *dev,
259 struct sm501_devdata *sm = dev_get_drvdata(dev);
263 spin_lock_irqsave(&sm->reg_lock, save);
265 data = readl(sm->regs + reg);
269 writel(data, sm->regs + reg);
272 spin_unlock_irqrestore(&sm->reg_lock, save);
277 EXPORT_SYMBOL_GPL(sm501_modify_reg);
279 unsigned long sm501_gpio_get(struct device *dev,
282 struct sm501_devdata *sm = dev_get_drvdata(dev);
283 unsigned long result;
286 reg = (gpio > 32) ? SM501_GPIO_DATA_HIGH : SM501_GPIO_DATA_LOW;
287 result = readl(sm->regs + reg);
289 result >>= (gpio & 31);
293 EXPORT_SYMBOL_GPL(sm501_gpio_get);
295 void sm501_gpio_set(struct device *dev,
300 struct sm501_devdata *sm = dev_get_drvdata(dev);
302 unsigned long bit = 1 << (gpio & 31);
307 base = (gpio > 32) ? SM501_GPIO_DATA_HIGH : SM501_GPIO_DATA_LOW;
310 spin_lock_irqsave(&sm->reg_lock, save);
312 val = readl(sm->regs + base) & ~bit;
315 writel(val, sm->regs + base);
317 val = readl(sm->regs + SM501_GPIO_DDR_LOW) & ~bit;
321 writel(val, sm->regs + SM501_GPIO_DDR_LOW);
324 spin_unlock_irqrestore(&sm->reg_lock, save);
328 EXPORT_SYMBOL_GPL(sm501_gpio_set);
333 * alters the power active gate to set specific units on or off
336 int sm501_unit_power(struct device *dev, unsigned int unit, unsigned int to)
338 struct sm501_devdata *sm = dev_get_drvdata(dev);
343 mutex_lock(&sm->clock_lock);
345 mode = readl(sm->regs + SM501_POWER_MODE_CONTROL);
346 gate = readl(sm->regs + SM501_CURRENT_GATE);
347 clock = readl(sm->regs + SM501_CURRENT_CLOCK);
349 mode &= 3; /* get current power mode */
351 if (unit >= ARRAY_SIZE(sm->unit_power)) {
352 dev_err(dev, "%s: bad unit %d\n", __func__, unit);
356 dev_dbg(sm->dev, "%s: unit %d, cur %d, to %d\n", __func__, unit,
357 sm->unit_power[unit], to);
359 if (to == 0 && sm->unit_power[unit] == 0) {
360 dev_err(sm->dev, "unit %d is already shutdown\n", unit);
364 sm->unit_power[unit] += to ? 1 : -1;
365 to = sm->unit_power[unit] ? 1 : 0;
368 if (gate & (1 << unit))
372 if (!(gate & (1 << unit)))
374 gate &= ~(1 << unit);
379 writel(gate, sm->regs + SM501_POWER_MODE_0_GATE);
380 writel(clock, sm->regs + SM501_POWER_MODE_0_CLOCK);
385 writel(gate, sm->regs + SM501_POWER_MODE_1_GATE);
386 writel(clock, sm->regs + SM501_POWER_MODE_1_CLOCK);
394 writel(mode, sm->regs + SM501_POWER_MODE_CONTROL);
397 dev_dbg(sm->dev, "gate %08lx, clock %08lx, mode %08lx\n",
400 sm501_mdelay(sm, 16);
403 mutex_unlock(&sm->clock_lock);
407 EXPORT_SYMBOL_GPL(sm501_unit_power);
410 /* Perform a rounded division. */
411 static long sm501fb_round_div(long num, long denom)
413 /* n / d + 1 / 2 = (2n + d) / 2d */
414 return (2 * num + denom) / (2 * denom);
417 /* clock value structure. */
422 unsigned int m, n, k;
427 * Calculates the nearest discrete clock frequency that
428 * can be achieved with the specified input clock.
429 * the maximum divisor is 3 or 5
432 static int sm501_calc_clock(unsigned long freq,
433 struct sm501_clock *clock,
443 /* try dividers 1 and 3 for CRT and for panel,
444 try divider 5 for panel only.*/
446 for (divider = 1; divider <= max_div; divider += 2) {
447 /* try all 8 shift values.*/
448 for (shift = 0; shift < 8; shift++) {
449 /* Calculate difference to requested clock */
450 diff = sm501fb_round_div(mclk, divider << shift) - freq;
454 /* If it is less than the current, use it */
455 if (diff < *best_diff) {
459 clock->divider = divider;
460 clock->shift = shift;
471 * Calculates the nearest discrete clock frequency that can be
472 * achieved using the programmable PLL.
473 * the maximum divisor is 3 or 5
476 static unsigned long sm501_calc_pll(unsigned long freq,
477 struct sm501_clock *clock,
481 unsigned int m, n, k;
482 long best_diff = 999999999;
485 * The SM502 datasheet doesn't specify the min/max values for M and N.
486 * N = 1 at least doesn't work in practice.
488 for (m = 2; m <= 255; m++) {
489 for (n = 2; n <= 127; n++) {
490 for (k = 0; k <= 1; k++) {
491 mclk = (24000000UL * m / n) >> k;
493 if (sm501_calc_clock(freq, clock, max_div,
503 /* Return best clock. */
504 return clock->mclk / (clock->divider << clock->shift);
507 /* sm501_select_clock
509 * Calculates the nearest discrete clock frequency that can be
510 * achieved using the 288MHz and 336MHz PLLs.
511 * the maximum divisor is 3 or 5
514 static unsigned long sm501_select_clock(unsigned long freq,
515 struct sm501_clock *clock,
519 long best_diff = 999999999;
521 /* Try 288MHz and 336MHz clocks. */
522 for (mclk = 288000000; mclk <= 336000000; mclk += 48000000) {
523 sm501_calc_clock(freq, clock, max_div, mclk, &best_diff);
526 /* Return best clock. */
527 return clock->mclk / (clock->divider << clock->shift);
532 * set one of the four clock sources to the closest available frequency to
536 unsigned long sm501_set_clock(struct device *dev,
538 unsigned long req_freq)
540 struct sm501_devdata *sm = dev_get_drvdata(dev);
541 unsigned long mode = readl(sm->regs + SM501_POWER_MODE_CONTROL);
542 unsigned long gate = readl(sm->regs + SM501_CURRENT_GATE);
543 unsigned long clock = readl(sm->regs + SM501_CURRENT_CLOCK);
545 unsigned int pll_reg = 0;
546 unsigned long sm501_freq; /* the actual frequency acheived */
548 struct sm501_clock to;
550 /* find achivable discrete frequency and setup register value
551 * accordingly, V2XCLK, MCLK and M1XCLK are the same P2XCLK
552 * has an extra bit for the divider */
555 case SM501_CLOCK_P2XCLK:
556 /* This clock is divided in half so to achive the
557 * requested frequency the value must be multiplied by
558 * 2. This clock also has an additional pre divisor */
560 if (sm->rev >= 0xC0) {
561 /* SM502 -> use the programmable PLL */
562 sm501_freq = (sm501_calc_pll(2 * req_freq,
564 reg = to.shift & 0x07;/* bottom 3 bits are shift */
566 reg |= 0x08; /* /3 divider required */
567 else if (to.divider == 5)
568 reg |= 0x10; /* /5 divider required */
569 reg |= 0x40; /* select the programmable PLL */
570 pll_reg = 0x20000 | (to.k << 15) | (to.n << 8) | to.m;
572 sm501_freq = (sm501_select_clock(2 * req_freq,
574 reg = to.shift & 0x07;/* bottom 3 bits are shift */
576 reg |= 0x08; /* /3 divider required */
577 else if (to.divider == 5)
578 reg |= 0x10; /* /5 divider required */
579 if (to.mclk != 288000000)
580 reg |= 0x20; /* which mclk pll is source */
584 case SM501_CLOCK_V2XCLK:
585 /* This clock is divided in half so to achive the
586 * requested frequency the value must be multiplied by 2. */
588 sm501_freq = (sm501_select_clock(2 * req_freq, &to, 3) / 2);
589 reg=to.shift & 0x07; /* bottom 3 bits are shift */
591 reg |= 0x08; /* /3 divider required */
592 if (to.mclk != 288000000)
593 reg |= 0x10; /* which mclk pll is source */
596 case SM501_CLOCK_MCLK:
597 case SM501_CLOCK_M1XCLK:
598 /* These clocks are the same and not further divided */
600 sm501_freq = sm501_select_clock( req_freq, &to, 3);
601 reg=to.shift & 0x07; /* bottom 3 bits are shift */
603 reg |= 0x08; /* /3 divider required */
604 if (to.mclk != 288000000)
605 reg |= 0x10; /* which mclk pll is source */
609 return 0; /* this is bad */
612 mutex_lock(&sm->clock_lock);
614 mode = readl(sm->regs + SM501_POWER_MODE_CONTROL);
615 gate = readl(sm->regs + SM501_CURRENT_GATE);
616 clock = readl(sm->regs + SM501_CURRENT_CLOCK);
618 clock = clock & ~(0xFF << clksrc);
619 clock |= reg<<clksrc;
621 mode &= 3; /* find current mode */
625 writel(gate, sm->regs + SM501_POWER_MODE_0_GATE);
626 writel(clock, sm->regs + SM501_POWER_MODE_0_CLOCK);
631 writel(gate, sm->regs + SM501_POWER_MODE_1_GATE);
632 writel(clock, sm->regs + SM501_POWER_MODE_1_CLOCK);
637 mutex_unlock(&sm->clock_lock);
641 writel(mode, sm->regs + SM501_POWER_MODE_CONTROL);
644 writel(pll_reg, sm->regs + SM501_PROGRAMMABLE_PLL_CONTROL);
648 dev_info(sm->dev, "gate %08lx, clock %08lx, mode %08lx\n",
651 sm501_mdelay(sm, 16);
652 mutex_unlock(&sm->clock_lock);
659 EXPORT_SYMBOL_GPL(sm501_set_clock);
663 * finds the closest available frequency for a given clock
666 unsigned long sm501_find_clock(struct device *dev,
668 unsigned long req_freq)
670 struct sm501_devdata *sm = dev_get_drvdata(dev);
671 unsigned long sm501_freq; /* the frequency achiveable by the 501 */
672 struct sm501_clock to;
675 case SM501_CLOCK_P2XCLK:
676 if (sm->rev >= 0xC0) {
677 /* SM502 -> use the programmable PLL */
678 sm501_freq = (sm501_calc_pll(2 * req_freq,
681 sm501_freq = (sm501_select_clock(2 * req_freq,
686 case SM501_CLOCK_V2XCLK:
687 sm501_freq = (sm501_select_clock(2 * req_freq, &to, 3) / 2);
690 case SM501_CLOCK_MCLK:
691 case SM501_CLOCK_M1XCLK:
692 sm501_freq = sm501_select_clock(req_freq, &to, 3);
696 sm501_freq = 0; /* error */
702 EXPORT_SYMBOL_GPL(sm501_find_clock);
704 static struct sm501_device *to_sm_device(struct platform_device *pdev)
706 return container_of(pdev, struct sm501_device, pdev);
709 /* sm501_device_release
711 * A release function for the platform devices we create to allow us to
712 * free any items we allocated
715 static void sm501_device_release(struct device *dev)
717 kfree(to_sm_device(to_platform_device(dev)));
720 /* sm501_create_subdev
722 * Create a skeleton platform device with resources for passing to a
726 static struct platform_device *
727 sm501_create_subdev(struct sm501_devdata *sm, char *name,
728 unsigned int res_count, unsigned int platform_data_size)
730 struct sm501_device *smdev;
732 smdev = kzalloc(sizeof(struct sm501_device) +
733 (sizeof(struct resource) * res_count) +
734 platform_data_size, GFP_KERNEL);
738 smdev->pdev.dev.release = sm501_device_release;
740 smdev->pdev.name = name;
741 smdev->pdev.id = sm->pdev_id;
742 smdev->pdev.dev.parent = sm->dev;
745 smdev->pdev.resource = (struct resource *)(smdev+1);
746 smdev->pdev.num_resources = res_count;
748 if (platform_data_size)
749 smdev->pdev.dev.platform_data = (void *)(smdev+1);
754 /* sm501_register_device
756 * Register a platform device created with sm501_create_subdev()
759 static int sm501_register_device(struct sm501_devdata *sm,
760 struct platform_device *pdev)
762 struct sm501_device *smdev = to_sm_device(pdev);
766 for (ptr = 0; ptr < pdev->num_resources; ptr++) {
767 printk("%s[%d] flags %08lx: %08llx..%08llx\n",
769 pdev->resource[ptr].flags,
770 (unsigned long long)pdev->resource[ptr].start,
771 (unsigned long long)pdev->resource[ptr].end);
774 ret = platform_device_register(pdev);
777 dev_dbg(sm->dev, "registered %s\n", pdev->name);
778 list_add_tail(&smdev->list, &sm->devices);
780 dev_err(sm->dev, "error registering %s (%d)\n",
786 /* sm501_create_subio
788 * Fill in an IO resource for a sub device
791 static void sm501_create_subio(struct sm501_devdata *sm,
792 struct resource *res,
793 resource_size_t offs,
794 resource_size_t size)
796 res->flags = IORESOURCE_MEM;
797 res->parent = sm->io_res;
798 res->start = sm->io_res->start + offs;
799 res->end = res->start + size - 1;
804 * Fill in an MEM resource for a sub device
807 static void sm501_create_mem(struct sm501_devdata *sm,
808 struct resource *res,
809 resource_size_t *offs,
810 resource_size_t size)
812 *offs -= size; /* adjust memory size */
814 res->flags = IORESOURCE_MEM;
815 res->parent = sm->mem_res;
816 res->start = sm->mem_res->start + *offs;
817 res->end = res->start + size - 1;
822 * Fill in an IRQ resource for a sub device
825 static void sm501_create_irq(struct sm501_devdata *sm,
826 struct resource *res)
828 res->flags = IORESOURCE_IRQ;
830 res->start = res->end = sm->irq;
833 static int sm501_register_usbhost(struct sm501_devdata *sm,
834 resource_size_t *mem_avail)
836 struct platform_device *pdev;
838 pdev = sm501_create_subdev(sm, "sm501-usb", 3, 0);
842 sm501_create_subio(sm, &pdev->resource[0], 0x40000, 0x20000);
843 sm501_create_mem(sm, &pdev->resource[1], mem_avail, 256*1024);
844 sm501_create_irq(sm, &pdev->resource[2]);
846 return sm501_register_device(sm, pdev);
849 static void sm501_setup_uart_data(struct sm501_devdata *sm,
850 struct plat_serial8250_port *uart_data,
853 uart_data->membase = sm->regs + offset;
854 uart_data->mapbase = sm->io_res->start + offset;
855 uart_data->iotype = UPIO_MEM;
856 uart_data->irq = sm->irq;
857 uart_data->flags = UPF_BOOT_AUTOCONF | UPF_SKIP_TEST | UPF_SHARE_IRQ;
858 uart_data->regshift = 2;
859 uart_data->uartclk = (9600 * 16);
862 static int sm501_register_uart(struct sm501_devdata *sm, int devices)
864 struct platform_device *pdev;
865 struct plat_serial8250_port *uart_data;
867 pdev = sm501_create_subdev(sm, "serial8250", 0,
868 sizeof(struct plat_serial8250_port) * 3);
872 uart_data = pdev->dev.platform_data;
874 if (devices & SM501_USE_UART0) {
875 sm501_setup_uart_data(sm, uart_data++, 0x30000);
876 sm501_unit_power(sm->dev, SM501_GATE_UART0, 1);
877 sm501_modify_reg(sm->dev, SM501_IRQ_MASK, 1 << 12, 0);
878 sm501_modify_reg(sm->dev, SM501_GPIO63_32_CONTROL, 0x01e0, 0);
880 if (devices & SM501_USE_UART1) {
881 sm501_setup_uart_data(sm, uart_data++, 0x30020);
882 sm501_unit_power(sm->dev, SM501_GATE_UART1, 1);
883 sm501_modify_reg(sm->dev, SM501_IRQ_MASK, 1 << 13, 0);
884 sm501_modify_reg(sm->dev, SM501_GPIO63_32_CONTROL, 0x1e00, 0);
887 pdev->id = PLAT8250_DEV_SM501;
889 return sm501_register_device(sm, pdev);
892 static int sm501_register_display(struct sm501_devdata *sm,
893 resource_size_t *mem_avail)
895 struct platform_device *pdev;
897 pdev = sm501_create_subdev(sm, "sm501-fb", 4, 0);
901 sm501_create_subio(sm, &pdev->resource[0], 0x80000, 0x10000);
902 sm501_create_subio(sm, &pdev->resource[1], 0x100000, 0x50000);
903 sm501_create_mem(sm, &pdev->resource[2], mem_avail, *mem_avail);
904 sm501_create_irq(sm, &pdev->resource[3]);
906 return sm501_register_device(sm, pdev);
911 * Debug attribute to attach to parent device to show core registers
914 static ssize_t sm501_dbg_regs(struct device *dev,
915 struct device_attribute *attr, char *buff)
917 struct sm501_devdata *sm = dev_get_drvdata(dev) ;
922 for (reg = 0x00; reg < 0x70; reg += 4) {
923 ret = sprintf(ptr, "%08x = %08x\n",
924 reg, readl(sm->regs + reg));
932 static DEVICE_ATTR(dbg_regs, 0666, sm501_dbg_regs, NULL);
936 * Helper function for the init code to setup a register
938 * clear the bits which are set in r->mask, and then set
939 * the bits set in r->set.
942 static inline void sm501_init_reg(struct sm501_devdata *sm,
944 struct sm501_reg_init *r)
948 tmp = readl(sm->regs + reg);
951 writel(tmp, sm->regs + reg);
956 * Setup core register values
959 static void sm501_init_regs(struct sm501_devdata *sm,
960 struct sm501_initdata *init)
962 sm501_misc_control(sm->dev,
963 init->misc_control.set,
964 init->misc_control.mask);
966 sm501_init_reg(sm, SM501_MISC_TIMING, &init->misc_timing);
967 sm501_init_reg(sm, SM501_GPIO31_0_CONTROL, &init->gpio_low);
968 sm501_init_reg(sm, SM501_GPIO63_32_CONTROL, &init->gpio_high);
971 dev_info(sm->dev, "setting M1XCLK to %ld\n", init->m1xclk);
972 sm501_set_clock(sm->dev, SM501_CLOCK_M1XCLK, init->m1xclk);
976 dev_info(sm->dev, "setting MCLK to %ld\n", init->mclk);
977 sm501_set_clock(sm->dev, SM501_CLOCK_MCLK, init->mclk);
982 /* Check the PLL sources for the M1CLK and M1XCLK
984 * If the M1CLK and M1XCLKs are not sourced from the same PLL, then
985 * there is a risk (see errata AB-5) that the SM501 will cease proper
986 * function. If this happens, then it is likely the SM501 will
990 static int sm501_check_clocks(struct sm501_devdata *sm)
992 unsigned long pwrmode = readl(sm->regs + SM501_CURRENT_CLOCK);
993 unsigned long msrc = (pwrmode & SM501_POWERMODE_M_SRC);
994 unsigned long m1src = (pwrmode & SM501_POWERMODE_M1_SRC);
996 return ((msrc == 0 && m1src != 0) || (msrc != 0 && m1src == 0));
999 static unsigned int sm501_mem_local[] = {
1010 * Common init code for an SM501
1013 static int sm501_init_dev(struct sm501_devdata *sm)
1015 struct sm501_initdata *idata;
1016 resource_size_t mem_avail;
1017 unsigned long dramctrl;
1018 unsigned long devid;
1021 mutex_init(&sm->clock_lock);
1022 spin_lock_init(&sm->reg_lock);
1024 INIT_LIST_HEAD(&sm->devices);
1026 devid = readl(sm->regs + SM501_DEVICEID);
1028 if ((devid & SM501_DEVICEID_IDMASK) != SM501_DEVICEID_SM501) {
1029 dev_err(sm->dev, "incorrect device id %08lx\n", devid);
1034 writel(0, sm->regs + SM501_IRQ_MASK);
1036 dramctrl = readl(sm->regs + SM501_DRAM_CONTROL);
1037 mem_avail = sm501_mem_local[(dramctrl >> 13) & 0x7];
1039 dev_info(sm->dev, "SM501 At %p: Version %08lx, %ld Mb, IRQ %d\n",
1040 sm->regs, devid, (unsigned long)mem_avail >> 20, sm->irq);
1042 sm->rev = devid & SM501_DEVICEID_REVMASK;
1044 sm501_dump_gate(sm);
1046 ret = device_create_file(sm->dev, &dev_attr_dbg_regs);
1048 dev_err(sm->dev, "failed to create debug regs file\n");
1052 /* check to see if we have some device initialisation */
1054 idata = sm->platdata ? sm->platdata->init : NULL;
1056 sm501_init_regs(sm, idata);
1058 if (idata->devices & SM501_USE_USB_HOST)
1059 sm501_register_usbhost(sm, &mem_avail);
1060 if (idata->devices & (SM501_USE_UART0 | SM501_USE_UART1))
1061 sm501_register_uart(sm, idata->devices);
1064 ret = sm501_check_clocks(sm);
1066 dev_err(sm->dev, "M1X and M clocks sourced from different "
1071 /* always create a framebuffer */
1072 sm501_register_display(sm, &mem_avail);
1077 static int sm501_plat_probe(struct platform_device *dev)
1079 struct sm501_devdata *sm;
1082 sm = kzalloc(sizeof(struct sm501_devdata), GFP_KERNEL);
1084 dev_err(&dev->dev, "no memory for device data\n");
1089 sm->dev = &dev->dev;
1090 sm->pdev_id = dev->id;
1091 sm->irq = platform_get_irq(dev, 0);
1092 sm->io_res = platform_get_resource(dev, IORESOURCE_MEM, 1);
1093 sm->mem_res = platform_get_resource(dev, IORESOURCE_MEM, 0);
1094 sm->platdata = dev->dev.platform_data;
1097 dev_err(&dev->dev, "failed to get irq resource\n");
1102 if (sm->io_res == NULL || sm->mem_res == NULL) {
1103 dev_err(&dev->dev, "failed to get IO resource\n");
1108 sm->regs_claim = request_mem_region(sm->io_res->start,
1111 if (sm->regs_claim == NULL) {
1112 dev_err(&dev->dev, "cannot claim registers\n");
1117 platform_set_drvdata(dev, sm);
1119 sm->regs = ioremap(sm->io_res->start,
1120 (sm->io_res->end - sm->io_res->start) - 1);
1122 if (sm->regs == NULL) {
1123 dev_err(&dev->dev, "cannot remap registers\n");
1128 return sm501_init_dev(sm);
1131 release_resource(sm->regs_claim);
1132 kfree(sm->regs_claim);
1141 /* power management support */
1143 static int sm501_plat_suspend(struct platform_device *pdev, pm_message_t state)
1145 struct sm501_devdata *sm = platform_get_drvdata(pdev);
1148 sm->pm_misc = readl(sm->regs + SM501_MISC_CONTROL);
1150 sm501_dump_regs(sm);
1154 static int sm501_plat_resume(struct platform_device *pdev)
1156 struct sm501_devdata *sm = platform_get_drvdata(pdev);
1158 sm501_dump_regs(sm);
1159 sm501_dump_gate(sm);
1162 /* check to see if we are in the same state as when suspended */
1164 if (readl(sm->regs + SM501_MISC_CONTROL) != sm->pm_misc) {
1165 dev_info(sm->dev, "SM501_MISC_CONTROL changed over sleep\n");
1166 writel(sm->pm_misc, sm->regs + SM501_MISC_CONTROL);
1168 /* our suspend causes the controller state to change,
1169 * either by something attempting setup, power loss,
1170 * or an external reset event on power change */
1172 if (sm->platdata && sm->platdata->init) {
1173 sm501_init_regs(sm, sm->platdata->init);
1177 /* dump our state from resume */
1179 sm501_dump_regs(sm);
1187 #define sm501_plat_suspend NULL
1188 #define sm501_plat_resume NULL
1191 /* Initialisation data for PCI devices */
1193 static struct sm501_initdata sm501_pci_initdata = {
1195 .set = 0x3F000000, /* 24bit panel */
1199 .set = 0x010100, /* SDRAM timing */
1203 .set = SM501_MISC_PNL_24BIT,
1207 .devices = SM501_USE_ALL,
1209 /* Errata AB-3 says that 72MHz is the fastest available
1210 * for 33MHZ PCI with proper bus-mastering operation */
1213 .m1xclk = 144 * MHZ,
1216 static struct sm501_platdata_fbsub sm501_pdata_fbsub = {
1217 .flags = (SM501FB_FLAG_USE_INIT_MODE |
1218 SM501FB_FLAG_USE_HWCURSOR |
1219 SM501FB_FLAG_USE_HWACCEL |
1220 SM501FB_FLAG_DISABLE_AT_EXIT),
1223 static struct sm501_platdata_fb sm501_fb_pdata = {
1224 .fb_route = SM501_FB_OWN,
1225 .fb_crt = &sm501_pdata_fbsub,
1226 .fb_pnl = &sm501_pdata_fbsub,
1229 static struct sm501_platdata sm501_pci_platdata = {
1230 .init = &sm501_pci_initdata,
1231 .fb = &sm501_fb_pdata,
1234 static int sm501_pci_probe(struct pci_dev *dev,
1235 const struct pci_device_id *id)
1237 struct sm501_devdata *sm;
1240 sm = kzalloc(sizeof(struct sm501_devdata), GFP_KERNEL);
1242 dev_err(&dev->dev, "no memory for device data\n");
1247 /* set a default set of platform data */
1248 dev->dev.platform_data = sm->platdata = &sm501_pci_platdata;
1250 /* set a hopefully unique id for our child platform devices */
1251 sm->pdev_id = 32 + dev->devfn;
1253 pci_set_drvdata(dev, sm);
1255 err = pci_enable_device(dev);
1257 dev_err(&dev->dev, "cannot enable device\n");
1261 sm->dev = &dev->dev;
1265 /* if the system is big-endian, we most probably have a
1266 * translation in the IO layer making the PCI bus little endian
1267 * so make the framebuffer swapped pixels */
1269 sm501_fb_pdata.flags |= SM501_FBPD_SWAP_FB_ENDIAN;
1272 /* check our resources */
1274 if (!(pci_resource_flags(dev, 0) & IORESOURCE_MEM)) {
1275 dev_err(&dev->dev, "region #0 is not memory?\n");
1280 if (!(pci_resource_flags(dev, 1) & IORESOURCE_MEM)) {
1281 dev_err(&dev->dev, "region #1 is not memory?\n");
1286 /* make our resources ready for sharing */
1288 sm->io_res = &dev->resource[1];
1289 sm->mem_res = &dev->resource[0];
1291 sm->regs_claim = request_mem_region(sm->io_res->start,
1293 if (sm->regs_claim == NULL) {
1294 dev_err(&dev->dev, "cannot claim registers\n");
1299 sm->regs = ioremap(pci_resource_start(dev, 1),
1300 pci_resource_len(dev, 1));
1302 if (sm->regs == NULL) {
1303 dev_err(&dev->dev, "cannot remap registers\n");
1312 release_resource(sm->regs_claim);
1313 kfree(sm->regs_claim);
1315 pci_disable_device(dev);
1317 pci_set_drvdata(dev, NULL);
1323 static void sm501_remove_sub(struct sm501_devdata *sm,
1324 struct sm501_device *smdev)
1326 list_del(&smdev->list);
1327 platform_device_unregister(&smdev->pdev);
1330 static void sm501_dev_remove(struct sm501_devdata *sm)
1332 struct sm501_device *smdev, *tmp;
1334 list_for_each_entry_safe(smdev, tmp, &sm->devices, list)
1335 sm501_remove_sub(sm, smdev);
1337 device_remove_file(sm->dev, &dev_attr_dbg_regs);
1340 static void sm501_pci_remove(struct pci_dev *dev)
1342 struct sm501_devdata *sm = pci_get_drvdata(dev);
1344 sm501_dev_remove(sm);
1347 release_resource(sm->regs_claim);
1348 kfree(sm->regs_claim);
1350 pci_set_drvdata(dev, NULL);
1351 pci_disable_device(dev);
1354 static int sm501_plat_remove(struct platform_device *dev)
1356 struct sm501_devdata *sm = platform_get_drvdata(dev);
1358 sm501_dev_remove(sm);
1361 release_resource(sm->regs_claim);
1362 kfree(sm->regs_claim);
1367 static struct pci_device_id sm501_pci_tbl[] = {
1368 { 0x126f, 0x0501, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
1372 MODULE_DEVICE_TABLE(pci, sm501_pci_tbl);
1374 static struct pci_driver sm501_pci_drv = {
1376 .id_table = sm501_pci_tbl,
1377 .probe = sm501_pci_probe,
1378 .remove = sm501_pci_remove,
1381 static struct platform_driver sm501_plat_drv = {
1384 .owner = THIS_MODULE,
1386 .probe = sm501_plat_probe,
1387 .remove = sm501_plat_remove,
1388 .suspend = sm501_plat_suspend,
1389 .resume = sm501_plat_resume,
1392 static int __init sm501_base_init(void)
1394 platform_driver_register(&sm501_plat_drv);
1395 return pci_register_driver(&sm501_pci_drv);
1398 static void __exit sm501_base_exit(void)
1400 platform_driver_unregister(&sm501_plat_drv);
1401 pci_unregister_driver(&sm501_pci_drv);
1404 module_init(sm501_base_init);
1405 module_exit(sm501_base_exit);
1407 MODULE_DESCRIPTION("SM501 Core Driver");
1408 MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>, Vincent Sanders");
1409 MODULE_LICENSE("GPL v2");