14 #include <linux/kernel.h>
15 #include <linux/module.h>
18 #include <linux/list.h>
19 #include <linux/device.h>
21 #include <linux/pci.h>
23 #include <linux/slab.h>
38 #ifdef CONFIG_MFD_SM501_GPIO
41 struct sm501_gpio_chip {
42 struct gpio_chip
gpio;
49 struct sm501_gpio_chip
low;
50 struct sm501_gpio_chip
high;
53 unsigned int registered : 1;
87 #define MHZ (1000 * 1000)
90 static const unsigned int div_tab[] = {
117 static unsigned long decode_div(
unsigned long pll2,
unsigned long val,
118 unsigned int lshft,
unsigned int selbit,
124 return pll2 / div_tab[(val >> lshft) & mask];
127 #define fmt_freq(x) ((x) / MHZ), ((x) % MHZ), (x)
140 unsigned long sdclk0, sdclk1;
141 unsigned long pll2 = 0;
143 switch (misct & 0x30) {
158 sdclk0 = (misct & (1<<12)) ? pll2 : 288 *
MHZ;
159 sdclk0 /= div_tab[((misct >> 8) & 0xf)];
161 sdclk1 = (misct & (1<<20)) ? pll2 : 288 *
MHZ;
162 sdclk1 /= div_tab[((misct >> 16) & 0xf)];
164 dev_dbg(
sm->dev,
"MISCT=%08lx, PM0=%08lx, PM1=%08lx\n",
167 dev_dbg(
sm->dev,
"PLL2 = %ld.%ld MHz (%ld), SDCLK0=%08lx, SDCLK1=%08lx\n",
168 fmt_freq(pll2), sdclk0, sdclk1);
170 dev_dbg(
sm->dev,
"SDRAM: PM0=%ld, PM1=%ld\n", sdclk0, sdclk1);
173 "P2 %ld.%ld MHz (%ld), V2 %ld.%ld (%ld), "
174 "M %ld.%ld (%ld), MX1 %ld.%ld (%ld)\n",
175 (pmc & 3 ) == 0 ?
'*' :
'-',
176 fmt_freq(decode_div(pll2, pm0, 24, 1<<29, 31)),
177 fmt_freq(decode_div(pll2, pm0, 16, 1<<20, 15)),
178 fmt_freq(decode_div(pll2, pm0, 8, 1<<12, 15)),
179 fmt_freq(decode_div(pll2, pm0, 0, 1<<4, 15)));
182 "P2 %ld.%ld MHz (%ld), V2 %ld.%ld (%ld), "
183 "M %ld.%ld (%ld), MX1 %ld.%ld (%ld)\n",
184 (pmc & 3 ) == 1 ?
'*' :
'-',
185 fmt_freq(decode_div(pll2, pm1, 24, 1<<29, 31)),
186 fmt_freq(decode_div(pll2, pm1, 16, 1<<20, 15)),
187 fmt_freq(decode_div(pll2, pm1, 8, 1<<12, 15)),
188 fmt_freq(decode_div(pll2, pm1, 0, 1<<4, 15)));
222 static inline void sm501_dump_gate(
struct sm501_devdata *sm) { }
223 static inline void sm501_dump_regs(
struct sm501_devdata *sm) { }
224 static inline void sm501_dump_clk(
struct sm501_devdata *sm) { }
255 unsigned long set,
unsigned long clear)
265 to = (misc & ~clear) |
set;
271 dev_dbg(sm->
dev,
"MISC_CONTROL %08lx\n", misc);
274 spin_unlock_irqrestore(&sm->
reg_lock, save);
304 spin_unlock_irqrestore(&sm->
reg_lock, save);
332 dev_err(dev,
"%s: bad unit %d\n", __func__, unit);
336 dev_dbg(sm->
dev,
"%s: unit %d, cur %d, to %d\n", __func__, unit,
340 dev_err(sm->
dev,
"unit %d is already shutdown\n", unit);
348 if (gate & (1 << unit))
352 if (!(gate & (1 << unit)))
354 gate &= ~(1 <<
unit);
378 dev_dbg(sm->
dev,
"gate %08lx, clock %08lx, mode %08lx\n",
381 sm501_mdelay(sm, 16);
405 static int sm501_calc_clock(
unsigned long freq,
419 for (divider = 1; divider <= max_div; divider += 2) {
421 for (shift = 0; shift < 8; shift++) {
428 if (diff < *best_diff) {
433 clock->
shift = shift;
449 static unsigned long sm501_calc_pll(
unsigned long freq,
454 unsigned int m,
n,
k;
455 long best_diff = 999999999;
461 for (m = 2; m <= 255; m++) {
462 for (n = 2; n <= 127; n++) {
463 for (k = 0; k <= 1; k++) {
464 mclk = (24000000
UL * m /
n) >> k;
466 if (sm501_calc_clock(freq, clock, max_div,
487 static unsigned long sm501_select_clock(
unsigned long freq,
492 long best_diff = 999999999;
495 for (mclk = 288000000; mclk <= 336000000; mclk += 48000000) {
496 sm501_calc_clock(freq, clock, max_div, mclk, &best_diff);
518 unsigned int pll_reg = 0;
519 unsigned long sm501_freq;
533 if (sm->
rev >= 0xC0) {
535 sm501_freq = (sm501_calc_pll(2 * req_freq,
537 reg = to.
shift & 0x07;
543 pll_reg = 0x20000 | (to.
k << 15) | (to.
n << 8) | to.
m;
545 sm501_freq = (sm501_select_clock(2 * req_freq,
547 reg = to.
shift & 0x07;
552 if (to.
mclk != 288000000)
561 sm501_freq = (sm501_select_clock(2 * req_freq, &to, 3) / 2);
565 if (to.
mclk != 288000000)
573 sm501_freq = sm501_select_clock( req_freq, &to, 3);
577 if (to.
mclk != 288000000)
591 clock = clock & ~(0xFF << clksrc);
592 clock |= reg<<clksrc;
622 dev_dbg(sm->
dev,
"gate %08lx, clock %08lx, mode %08lx\n",
625 sm501_mdelay(sm, 16);
645 unsigned long sm501_freq;
650 if (sm->
rev >= 0xC0) {
652 sm501_freq = (sm501_calc_pll(2 * req_freq,
655 sm501_freq = (sm501_select_clock(2 * req_freq,
661 sm501_freq = (sm501_select_clock(2 * req_freq, &to, 3) / 2);
666 sm501_freq = sm501_select_clock(req_freq, &to, 3);
689 static void sm501_device_release(
struct device *
dev)
702 unsigned int res_count,
unsigned int platform_data_size)
707 (
sizeof(
struct resource) * res_count) +
712 smdev->
pdev.dev.release = sm501_device_release;
716 smdev->
pdev.dev.parent = sm->
dev;
722 if (platform_data_size)
723 smdev->
pdev.dev.platform_data = (
void *)(smdev+1);
751 dev_err(sm->
dev,
"error registering %s (%d)\n",
809 pdev = sm501_create_subdev(sm,
"sm501-usb", 3, 0);
813 sm501_create_subio(sm, &pdev->
resource[0], 0x40000, 0x20000);
814 sm501_create_mem(sm, &pdev->
resource[1], mem_avail, 256*1024);
815 sm501_create_irq(sm, &pdev->
resource[2]);
817 return sm501_register_device(sm, pdev);
830 uart_data->
uartclk = (9600 * 16);
833 static int sm501_register_uart(
struct sm501_devdata *sm,
int devices)
838 pdev = sm501_create_subdev(sm,
"serial8250", 0,
843 uart_data = pdev->
dev.platform_data;
846 sm501_setup_uart_data(sm, uart_data++, 0x30000);
852 sm501_setup_uart_data(sm, uart_data++, 0x30020);
860 return sm501_register_device(sm, pdev);
868 pdev = sm501_create_subdev(sm,
"sm501-fb", 4, 0);
872 sm501_create_subio(sm, &pdev->
resource[0], 0x80000, 0x10000);
873 sm501_create_subio(sm, &pdev->
resource[1], 0x100000, 0x50000);
874 sm501_create_mem(sm, &pdev->
resource[2], mem_avail, *mem_avail);
875 sm501_create_irq(sm, &pdev->
resource[3]);
877 return sm501_register_device(sm, pdev);
880 #ifdef CONFIG_MFD_SM501_GPIO
882 static inline struct sm501_gpio_chip *to_sm501_gpio(
struct gpio_chip *
gc)
892 static int sm501_gpio_get(
struct gpio_chip *
chip,
unsigned offset)
895 struct sm501_gpio_chip *smgpio = to_sm501_gpio(chip);
904 static void sm501_gpio_ensure_gpio(
struct sm501_gpio_chip *smchip,
912 dev_info(sm501_gpio_to_dev(smchip->ourgpio)->dev,
913 "changing mode of gpio, bit %08lx\n", bit);
919 sm501_sync_regs(sm501_gpio_to_dev(smchip->ourgpio));
923 static void sm501_gpio_set(
struct gpio_chip *
chip,
unsigned offset,
int value)
926 struct sm501_gpio_chip *smchip = to_sm501_gpio(chip);
928 unsigned long bit = 1 <<
offset;
929 void __iomem *regs = smchip->regbase;
933 dev_dbg(sm501_gpio_to_dev(smgpio)->dev,
"%s(%p,%d)\n",
934 __func__, chip, offset);
943 sm501_sync_regs(sm501_gpio_to_dev(smgpio));
944 sm501_gpio_ensure_gpio(smchip, bit);
946 spin_unlock_irqrestore(&smgpio->lock, save);
949 static int sm501_gpio_input(
struct gpio_chip *chip,
unsigned offset)
951 struct sm501_gpio_chip *smchip = to_sm501_gpio(chip);
953 void __iomem *regs = smchip->regbase;
954 unsigned long bit = 1 <<
offset;
958 dev_dbg(sm501_gpio_to_dev(smgpio)->dev,
"%s(%p,%d)\n",
959 __func__, chip, offset);
966 sm501_sync_regs(sm501_gpio_to_dev(smgpio));
967 sm501_gpio_ensure_gpio(smchip, bit);
969 spin_unlock_irqrestore(&smgpio->lock, save);
974 static int sm501_gpio_output(
struct gpio_chip *chip,
975 unsigned offset,
int value)
977 struct sm501_gpio_chip *smchip = to_sm501_gpio(chip);
979 unsigned long bit = 1 <<
offset;
980 void __iomem *regs = smchip->regbase;
985 dev_dbg(sm501_gpio_to_dev(smgpio)->dev,
"%s(%p,%d,%d)\n",
986 __func__, chip, offset, value);
1000 sm501_sync_regs(sm501_gpio_to_dev(smgpio));
1003 sm501_sync_regs(sm501_gpio_to_dev(smgpio));
1004 spin_unlock_irqrestore(&smgpio->lock, save);
1009 static struct gpio_chip gpio_chip_template = {
1011 .direction_input = sm501_gpio_input,
1012 .direction_output = sm501_gpio_output,
1013 .set = sm501_gpio_set,
1014 .get = sm501_gpio_get,
1019 struct sm501_gpio_chip *chip)
1022 struct gpio_chip *gchip = &chip->gpio;
1025 chip->gpio = gpio_chip_template;
1027 if (chip == &gpio->high) {
1032 gchip->label =
"SM501-HIGH";
1036 gchip->label =
"SM501-LOW";
1040 chip->ourgpio =
gpio;
1052 dev_dbg(sm->
dev,
"registering gpio block %08llx\n",
1053 (
unsigned long long)iobase);
1058 if (gpio->regs_res ==
NULL) {
1059 dev_err(sm->
dev,
"gpio: failed to request region\n");
1063 gpio->regs =
ioremap(iobase, 0x20);
1064 if (gpio->regs ==
NULL) {
1065 dev_err(sm->
dev,
"gpio: failed to remap registers\n");
1072 ret = sm501_gpio_register_chip(sm, gpio, &gpio->low);
1074 dev_err(sm->
dev,
"failed to add low chip\n");
1078 ret = sm501_gpio_register_chip(sm, gpio, &gpio->high);
1080 dev_err(sm->
dev,
"failed to add high chip\n");
1084 gpio->registered = 1;
1091 dev_err(sm->
dev,
"cannot remove low chip, cannot tidy up\n");
1100 kfree(gpio->regs_res);
1110 if (!sm->
gpio.registered)
1115 dev_err(sm->
dev,
"cannot remove low chip, cannot tidy up\n");
1119 dev_err(sm->
dev,
"cannot remove high chip, cannot tidy up\n");
1123 kfree(gpio->regs_res);
1126 static inline int sm501_gpio_pin2nr(
struct sm501_devdata *sm,
unsigned int pin)
1129 int base = (pin < 32) ? gpio->low.gpio.base : gpio->high.gpio.base;
1131 return (pin % 32) + base;
1134 static inline int sm501_gpio_isregistered(
struct sm501_devdata *sm)
1136 return sm->
gpio.registered;
1139 static inline int sm501_register_gpio(
struct sm501_devdata *sm)
1144 static inline void sm501_gpio_remove(
struct sm501_devdata *sm)
1148 static inline int sm501_gpio_pin2nr(
struct sm501_devdata *sm,
unsigned int pin)
1153 static inline int sm501_gpio_isregistered(
struct sm501_devdata *sm)
1159 static int sm501_register_gpio_i2c_instance(
struct sm501_devdata *sm,
1165 pdev = sm501_create_subdev(sm,
"i2c-gpio", 0,
1170 icd = pdev->
dev.platform_data;
1189 dev_info(sm->
dev,
"registering i2c-%d: sda=%d (%d), scl=%d (%d)\n",
1193 return sm501_register_device(sm, pdev);
1196 static int sm501_register_gpio_i2c(
struct sm501_devdata *sm,
1203 for (index = 0; index < pdata->
gpio_i2c_nr; index++, iic++) {
1204 ret = sm501_register_gpio_i2c_instance(sm, iic);
1225 for (reg = 0x00; reg < 0x70; reg += 4) {
1226 ret =
sprintf(ptr,
"%08x = %08x\n",
1299 return ((msrc == 0 && m1src != 0) || (msrc != 0 && m1src == 0));
1302 static unsigned int sm501_mem_local[] = {
1321 unsigned long dramctrl;
1322 unsigned long devid;
1333 dev_err(sm->
dev,
"incorrect device id %08lx\n", devid);
1341 mem_avail = sm501_mem_local[(dramctrl >> 13) & 0x7];
1343 dev_info(sm->
dev,
"SM501 At %p: Version %08lx, %ld Mb, IRQ %d\n",
1344 sm->
regs, devid, (
unsigned long)mem_avail >> 20, sm->
irq);
1348 sm501_dump_gate(sm);
1352 dev_err(sm->
dev,
"failed to create debug regs file\n");
1359 idata = pdata ? pdata->
init :
NULL;
1362 sm501_init_regs(sm, idata);
1365 sm501_register_usbhost(sm, &mem_avail);
1367 sm501_register_uart(sm, idata->
devices);
1369 sm501_register_gpio(sm);
1373 if (!sm501_gpio_isregistered(sm))
1374 dev_err(sm->
dev,
"no gpio available for i2c gpio.\n");
1376 sm501_register_gpio_i2c(sm, pdata);
1379 ret = sm501_check_clocks(sm);
1381 dev_err(sm->
dev,
"M1X and M clocks sourced from different "
1387 sm501_register_display(sm, &mem_avail);
1399 dev_err(&dev->
dev,
"no memory for device data\n");
1410 dev_err(&dev->
dev,
"failed to get irq resource\n");
1419 dev_err(&dev->
dev,
"failed to get IO resource\n");
1428 dev_err(&dev->
dev,
"cannot claim registers\n");
1433 platform_set_drvdata(dev, sm);
1438 dev_err(&dev->
dev,
"cannot remap registers\n");
1443 return sm501_init_dev(sm);
1459 static void sm501_set_power(
struct sm501_devdata *sm,
int on)
1474 dev_dbg(sm->
dev,
"setting power to %d\n", on);
1477 sm501_mdelay(sm, 10);
1488 sm501_dump_regs(sm);
1492 sm501_set_power(sm, 0);
1502 sm501_set_power(sm, 1);
1504 sm501_dump_regs(sm);
1505 sm501_dump_gate(sm);
1511 dev_info(sm->
dev,
"SM501_MISC_CONTROL changed over sleep\n");
1519 sm501_init_regs(sm, sm->
platdata->init);
1525 sm501_dump_regs(sm);
1533 #define sm501_plat_suspend NULL
1534 #define sm501_plat_resume NULL
1559 .m1xclk = 144 *
MHZ,
1571 .fb_crt = &sm501_pdata_fbsub,
1572 .fb_pnl = &sm501_pdata_fbsub,
1576 .init = &sm501_pci_initdata,
1577 .fb = &sm501_fb_pdata,
1589 dev_err(&dev->
dev,
"no memory for device data\n");
1595 dev->
dev.platform_data = sm->
platdata = &sm501_pci_platdata;
1600 pci_set_drvdata(dev, sm);
1604 dev_err(&dev->
dev,
"cannot enable device\n");
1622 dev_err(&dev->
dev,
"region #0 is not memory?\n");
1628 dev_err(&dev->
dev,
"region #1 is not memory?\n");
1641 dev_err(&dev->
dev,
"cannot claim registers\n");
1649 dev_err(&dev->
dev,
"cannot remap registers\n");
1663 pci_set_drvdata(dev,
NULL);
1681 sm501_remove_sub(sm, smdev);
1685 sm501_gpio_remove(sm);
1692 sm501_dev_remove(sm);
1698 pci_set_drvdata(dev,
NULL);
1706 sm501_dev_remove(sm);
1722 static struct pci_driver sm501_pci_driver = {
1724 .id_table = sm501_pci_tbl,
1725 .probe = sm501_pci_probe,
1732 { .compatible =
"smi,sm501", },
1740 .of_match_table = of_sm501_match_tbl,
1742 .probe = sm501_plat_probe,
1743 .remove = sm501_plat_remove,
1748 static int __init sm501_base_init(
void)
1751 return pci_register_driver(&sm501_pci_driver);
1754 static void __exit sm501_base_exit(
void)