vfio/pci: Fix integer overflows, bitmask check
[linux/fpc-iii.git] / arch / mips / alchemy / devboards / db1000.c
blob433c4b9a9f0a92af20e31f1f16f9be5cde955b12
1 /*
2 * DBAu1000/1500/1100 PBAu1100/1500 board support
4 * Copyright 2000, 2008 MontaVista Software Inc.
5 * Author: MontaVista Software, Inc. <source@mvista.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include <linux/clk.h>
23 #include <linux/dma-mapping.h>
24 #include <linux/gpio.h>
25 #include <linux/init.h>
26 #include <linux/interrupt.h>
27 #include <linux/leds.h>
28 #include <linux/mmc/host.h>
29 #include <linux/module.h>
30 #include <linux/platform_device.h>
31 #include <linux/pm.h>
32 #include <linux/spi/spi.h>
33 #include <linux/spi/spi_gpio.h>
34 #include <linux/spi/ads7846.h>
35 #include <asm/mach-au1x00/au1000.h>
36 #include <asm/mach-au1x00/gpio-au1000.h>
37 #include <asm/mach-au1x00/au1000_dma.h>
38 #include <asm/mach-au1x00/au1100_mmc.h>
39 #include <asm/mach-db1x00/bcsr.h>
40 #include <asm/reboot.h>
41 #include <prom.h>
42 #include "platform.h"
44 #define F_SWAPPED (bcsr_read(BCSR_STATUS) & BCSR_STATUS_DB1000_SWAPBOOT)
46 const char *get_system_type(void);
48 int __init db1000_board_setup(void)
50 /* initialize board register space */
51 bcsr_init(DB1000_BCSR_PHYS_ADDR,
52 DB1000_BCSR_PHYS_ADDR + DB1000_BCSR_HEXLED_OFS);
54 switch (BCSR_WHOAMI_BOARD(bcsr_read(BCSR_WHOAMI))) {
55 case BCSR_WHOAMI_DB1000:
56 case BCSR_WHOAMI_DB1500:
57 case BCSR_WHOAMI_DB1100:
58 case BCSR_WHOAMI_PB1500:
59 case BCSR_WHOAMI_PB1500R2:
60 case BCSR_WHOAMI_PB1100:
61 pr_info("AMD Alchemy %s Board\n", get_system_type());
62 return 0;
64 return -ENODEV;
67 static int db1500_map_pci_irq(const struct pci_dev *d, u8 slot, u8 pin)
69 if ((slot < 12) || (slot > 13) || pin == 0)
70 return -1;
71 if (slot == 12)
72 return (pin == 1) ? AU1500_PCI_INTA : 0xff;
73 if (slot == 13) {
74 switch (pin) {
75 case 1: return AU1500_PCI_INTA;
76 case 2: return AU1500_PCI_INTB;
77 case 3: return AU1500_PCI_INTC;
78 case 4: return AU1500_PCI_INTD;
81 return -1;
84 static struct resource alchemy_pci_host_res[] = {
85 [0] = {
86 .start = AU1500_PCI_PHYS_ADDR,
87 .end = AU1500_PCI_PHYS_ADDR + 0xfff,
88 .flags = IORESOURCE_MEM,
92 static struct alchemy_pci_platdata db1500_pci_pd = {
93 .board_map_irq = db1500_map_pci_irq,
96 static struct platform_device db1500_pci_host_dev = {
97 .dev.platform_data = &db1500_pci_pd,
98 .name = "alchemy-pci",
99 .id = 0,
100 .num_resources = ARRAY_SIZE(alchemy_pci_host_res),
101 .resource = alchemy_pci_host_res,
104 int __init db1500_pci_setup(void)
106 return platform_device_register(&db1500_pci_host_dev);
109 static struct resource au1100_lcd_resources[] = {
110 [0] = {
111 .start = AU1100_LCD_PHYS_ADDR,
112 .end = AU1100_LCD_PHYS_ADDR + 0x800 - 1,
113 .flags = IORESOURCE_MEM,
115 [1] = {
116 .start = AU1100_LCD_INT,
117 .end = AU1100_LCD_INT,
118 .flags = IORESOURCE_IRQ,
122 static u64 au1100_lcd_dmamask = DMA_BIT_MASK(32);
124 static struct platform_device au1100_lcd_device = {
125 .name = "au1100-lcd",
126 .id = 0,
127 .dev = {
128 .dma_mask = &au1100_lcd_dmamask,
129 .coherent_dma_mask = DMA_BIT_MASK(32),
131 .num_resources = ARRAY_SIZE(au1100_lcd_resources),
132 .resource = au1100_lcd_resources,
135 static struct resource alchemy_ac97c_res[] = {
136 [0] = {
137 .start = AU1000_AC97_PHYS_ADDR,
138 .end = AU1000_AC97_PHYS_ADDR + 0xfff,
139 .flags = IORESOURCE_MEM,
141 [1] = {
142 .start = DMA_ID_AC97C_TX,
143 .end = DMA_ID_AC97C_TX,
144 .flags = IORESOURCE_DMA,
146 [2] = {
147 .start = DMA_ID_AC97C_RX,
148 .end = DMA_ID_AC97C_RX,
149 .flags = IORESOURCE_DMA,
153 static struct platform_device alchemy_ac97c_dev = {
154 .name = "alchemy-ac97c",
155 .id = -1,
156 .resource = alchemy_ac97c_res,
157 .num_resources = ARRAY_SIZE(alchemy_ac97c_res),
160 static struct platform_device alchemy_ac97c_dma_dev = {
161 .name = "alchemy-pcm-dma",
162 .id = 0,
165 static struct platform_device db1x00_codec_dev = {
166 .name = "ac97-codec",
167 .id = -1,
170 static struct platform_device db1x00_audio_dev = {
171 .name = "db1000-audio",
174 /******************************************************************************/
176 static irqreturn_t db1100_mmc_cd(int irq, void *ptr)
178 void (*mmc_cd)(struct mmc_host *, unsigned long);
179 /* link against CONFIG_MMC=m */
180 mmc_cd = symbol_get(mmc_detect_change);
181 mmc_cd(ptr, msecs_to_jiffies(500));
182 symbol_put(mmc_detect_change);
184 return IRQ_HANDLED;
187 static int db1100_mmc_cd_setup(void *mmc_host, int en)
189 int ret = 0, irq;
191 if (BCSR_WHOAMI_BOARD(bcsr_read(BCSR_WHOAMI)) == BCSR_WHOAMI_DB1100)
192 irq = AU1100_GPIO19_INT;
193 else
194 irq = AU1100_GPIO14_INT; /* PB1100 SD0 CD# */
196 if (en) {
197 irq_set_irq_type(irq, IRQ_TYPE_EDGE_BOTH);
198 ret = request_irq(irq, db1100_mmc_cd, 0,
199 "sd0_cd", mmc_host);
200 } else
201 free_irq(irq, mmc_host);
202 return ret;
205 static int db1100_mmc1_cd_setup(void *mmc_host, int en)
207 int ret = 0, irq;
209 if (BCSR_WHOAMI_BOARD(bcsr_read(BCSR_WHOAMI)) == BCSR_WHOAMI_DB1100)
210 irq = AU1100_GPIO20_INT;
211 else
212 irq = AU1100_GPIO15_INT; /* PB1100 SD1 CD# */
214 if (en) {
215 irq_set_irq_type(irq, IRQ_TYPE_EDGE_BOTH);
216 ret = request_irq(irq, db1100_mmc_cd, 0,
217 "sd1_cd", mmc_host);
218 } else
219 free_irq(irq, mmc_host);
220 return ret;
223 static int db1100_mmc_card_readonly(void *mmc_host)
225 /* testing suggests that this bit is inverted */
226 return (bcsr_read(BCSR_STATUS) & BCSR_STATUS_SD0WP) ? 0 : 1;
229 static int db1100_mmc_card_inserted(void *mmc_host)
231 return !alchemy_gpio_get_value(19);
234 static void db1100_mmc_set_power(void *mmc_host, int state)
236 int bit;
238 if (BCSR_WHOAMI_BOARD(bcsr_read(BCSR_WHOAMI)) == BCSR_WHOAMI_DB1100)
239 bit = BCSR_BOARD_SD0PWR;
240 else
241 bit = BCSR_BOARD_PB1100_SD0PWR;
243 if (state) {
244 bcsr_mod(BCSR_BOARD, 0, bit);
245 msleep(400); /* stabilization time */
246 } else
247 bcsr_mod(BCSR_BOARD, bit, 0);
250 static void db1100_mmcled_set(struct led_classdev *led, enum led_brightness b)
252 if (b != LED_OFF)
253 bcsr_mod(BCSR_LEDS, BCSR_LEDS_LED0, 0);
254 else
255 bcsr_mod(BCSR_LEDS, 0, BCSR_LEDS_LED0);
258 static struct led_classdev db1100_mmc_led = {
259 .brightness_set = db1100_mmcled_set,
262 static int db1100_mmc1_card_readonly(void *mmc_host)
264 return (bcsr_read(BCSR_BOARD) & BCSR_BOARD_SD1WP) ? 1 : 0;
267 static int db1100_mmc1_card_inserted(void *mmc_host)
269 return !alchemy_gpio_get_value(20);
272 static void db1100_mmc1_set_power(void *mmc_host, int state)
274 int bit;
276 if (BCSR_WHOAMI_BOARD(bcsr_read(BCSR_WHOAMI)) == BCSR_WHOAMI_DB1100)
277 bit = BCSR_BOARD_SD1PWR;
278 else
279 bit = BCSR_BOARD_PB1100_SD1PWR;
281 if (state) {
282 bcsr_mod(BCSR_BOARD, 0, bit);
283 msleep(400); /* stabilization time */
284 } else
285 bcsr_mod(BCSR_BOARD, bit, 0);
288 static void db1100_mmc1led_set(struct led_classdev *led, enum led_brightness b)
290 if (b != LED_OFF)
291 bcsr_mod(BCSR_LEDS, BCSR_LEDS_LED1, 0);
292 else
293 bcsr_mod(BCSR_LEDS, 0, BCSR_LEDS_LED1);
296 static struct led_classdev db1100_mmc1_led = {
297 .brightness_set = db1100_mmc1led_set,
300 static struct au1xmmc_platform_data db1100_mmc_platdata[2] = {
301 [0] = {
302 .cd_setup = db1100_mmc_cd_setup,
303 .set_power = db1100_mmc_set_power,
304 .card_inserted = db1100_mmc_card_inserted,
305 .card_readonly = db1100_mmc_card_readonly,
306 .led = &db1100_mmc_led,
308 [1] = {
309 .cd_setup = db1100_mmc1_cd_setup,
310 .set_power = db1100_mmc1_set_power,
311 .card_inserted = db1100_mmc1_card_inserted,
312 .card_readonly = db1100_mmc1_card_readonly,
313 .led = &db1100_mmc1_led,
317 static struct resource au1100_mmc0_resources[] = {
318 [0] = {
319 .start = AU1100_SD0_PHYS_ADDR,
320 .end = AU1100_SD0_PHYS_ADDR + 0xfff,
321 .flags = IORESOURCE_MEM,
323 [1] = {
324 .start = AU1100_SD_INT,
325 .end = AU1100_SD_INT,
326 .flags = IORESOURCE_IRQ,
328 [2] = {
329 .start = DMA_ID_SD0_TX,
330 .end = DMA_ID_SD0_TX,
331 .flags = IORESOURCE_DMA,
333 [3] = {
334 .start = DMA_ID_SD0_RX,
335 .end = DMA_ID_SD0_RX,
336 .flags = IORESOURCE_DMA,
340 static u64 au1xxx_mmc_dmamask = DMA_BIT_MASK(32);
342 static struct platform_device db1100_mmc0_dev = {
343 .name = "au1xxx-mmc",
344 .id = 0,
345 .dev = {
346 .dma_mask = &au1xxx_mmc_dmamask,
347 .coherent_dma_mask = DMA_BIT_MASK(32),
348 .platform_data = &db1100_mmc_platdata[0],
350 .num_resources = ARRAY_SIZE(au1100_mmc0_resources),
351 .resource = au1100_mmc0_resources,
354 static struct resource au1100_mmc1_res[] = {
355 [0] = {
356 .start = AU1100_SD1_PHYS_ADDR,
357 .end = AU1100_SD1_PHYS_ADDR + 0xfff,
358 .flags = IORESOURCE_MEM,
360 [1] = {
361 .start = AU1100_SD_INT,
362 .end = AU1100_SD_INT,
363 .flags = IORESOURCE_IRQ,
365 [2] = {
366 .start = DMA_ID_SD1_TX,
367 .end = DMA_ID_SD1_TX,
368 .flags = IORESOURCE_DMA,
370 [3] = {
371 .start = DMA_ID_SD1_RX,
372 .end = DMA_ID_SD1_RX,
373 .flags = IORESOURCE_DMA,
377 static struct platform_device db1100_mmc1_dev = {
378 .name = "au1xxx-mmc",
379 .id = 1,
380 .dev = {
381 .dma_mask = &au1xxx_mmc_dmamask,
382 .coherent_dma_mask = DMA_BIT_MASK(32),
383 .platform_data = &db1100_mmc_platdata[1],
385 .num_resources = ARRAY_SIZE(au1100_mmc1_res),
386 .resource = au1100_mmc1_res,
389 /******************************************************************************/
391 static void db1000_irda_set_phy_mode(int mode)
393 unsigned short mask = BCSR_RESETS_IRDA_MODE_MASK | BCSR_RESETS_FIR_SEL;
395 switch (mode) {
396 case AU1000_IRDA_PHY_MODE_OFF:
397 bcsr_mod(BCSR_RESETS, mask, BCSR_RESETS_IRDA_MODE_OFF);
398 break;
399 case AU1000_IRDA_PHY_MODE_SIR:
400 bcsr_mod(BCSR_RESETS, mask, BCSR_RESETS_IRDA_MODE_FULL);
401 break;
402 case AU1000_IRDA_PHY_MODE_FIR:
403 bcsr_mod(BCSR_RESETS, mask, BCSR_RESETS_IRDA_MODE_FULL |
404 BCSR_RESETS_FIR_SEL);
405 break;
409 static struct au1k_irda_platform_data db1000_irda_platdata = {
410 .set_phy_mode = db1000_irda_set_phy_mode,
413 static struct resource au1000_irda_res[] = {
414 [0] = {
415 .start = AU1000_IRDA_PHYS_ADDR,
416 .end = AU1000_IRDA_PHYS_ADDR + 0x0fff,
417 .flags = IORESOURCE_MEM,
419 [1] = {
420 .start = AU1000_IRDA_TX_INT,
421 .end = AU1000_IRDA_TX_INT,
422 .flags = IORESOURCE_IRQ,
424 [2] = {
425 .start = AU1000_IRDA_RX_INT,
426 .end = AU1000_IRDA_RX_INT,
427 .flags = IORESOURCE_IRQ,
431 static struct platform_device db1000_irda_dev = {
432 .name = "au1000-irda",
433 .id = -1,
434 .dev = {
435 .platform_data = &db1000_irda_platdata,
437 .resource = au1000_irda_res,
438 .num_resources = ARRAY_SIZE(au1000_irda_res),
441 /******************************************************************************/
443 static struct ads7846_platform_data db1100_touch_pd = {
444 .model = 7846,
445 .vref_mv = 3300,
446 .gpio_pendown = 21,
449 static struct spi_gpio_platform_data db1100_spictl_pd = {
450 .sck = 209,
451 .mosi = 208,
452 .miso = 207,
453 .num_chipselect = 1,
456 static struct spi_board_info db1100_spi_info[] __initdata = {
457 [0] = {
458 .modalias = "ads7846",
459 .max_speed_hz = 3250000,
460 .bus_num = 0,
461 .chip_select = 0,
462 .mode = 0,
463 .irq = AU1100_GPIO21_INT,
464 .platform_data = &db1100_touch_pd,
465 .controller_data = (void *)210, /* for spi_gpio: CS# GPIO210 */
469 static struct platform_device db1100_spi_dev = {
470 .name = "spi_gpio",
471 .id = 0,
472 .dev = {
473 .platform_data = &db1100_spictl_pd,
478 static struct platform_device *db1x00_devs[] = {
479 &db1x00_codec_dev,
480 &alchemy_ac97c_dma_dev,
481 &alchemy_ac97c_dev,
482 &db1x00_audio_dev,
485 static struct platform_device *db1000_devs[] = {
486 &db1000_irda_dev,
489 static struct platform_device *db1100_devs[] = {
490 &au1100_lcd_device,
491 &db1100_mmc0_dev,
492 &db1100_mmc1_dev,
493 &db1000_irda_dev,
496 int __init db1000_dev_setup(void)
498 int board = BCSR_WHOAMI_BOARD(bcsr_read(BCSR_WHOAMI));
499 int c0, c1, d0, d1, s0, s1, flashsize = 32, twosocks = 1;
500 unsigned long pfc;
501 struct clk *c, *p;
503 if (board == BCSR_WHOAMI_DB1500) {
504 c0 = AU1500_GPIO2_INT;
505 c1 = AU1500_GPIO5_INT;
506 d0 = 0; /* GPIO number, NOT irq! */
507 d1 = 3; /* GPIO number, NOT irq! */
508 s0 = AU1500_GPIO1_INT;
509 s1 = AU1500_GPIO4_INT;
510 } else if (board == BCSR_WHOAMI_DB1100) {
511 c0 = AU1100_GPIO2_INT;
512 c1 = AU1100_GPIO5_INT;
513 d0 = 0; /* GPIO number, NOT irq! */
514 d1 = 3; /* GPIO number, NOT irq! */
515 s0 = AU1100_GPIO1_INT;
516 s1 = AU1100_GPIO4_INT;
518 gpio_request(19, "sd0_cd");
519 gpio_request(20, "sd1_cd");
520 gpio_direction_input(19); /* sd0 cd# */
521 gpio_direction_input(20); /* sd1 cd# */
523 /* spi_gpio on SSI0 pins */
524 pfc = alchemy_rdsys(AU1000_SYS_PINFUNC);
525 pfc |= (1 << 0); /* SSI0 pins as GPIOs */
526 alchemy_wrsys(pfc, AU1000_SYS_PINFUNC);
528 spi_register_board_info(db1100_spi_info,
529 ARRAY_SIZE(db1100_spi_info));
531 /* link LCD clock to AUXPLL */
532 p = clk_get(NULL, "auxpll_clk");
533 c = clk_get(NULL, "lcd_intclk");
534 if (!IS_ERR(c) && !IS_ERR(p)) {
535 clk_set_parent(c, p);
536 clk_set_rate(c, clk_get_rate(p));
538 if (!IS_ERR(c))
539 clk_put(c);
540 if (!IS_ERR(p))
541 clk_put(p);
543 platform_add_devices(db1100_devs, ARRAY_SIZE(db1100_devs));
544 platform_device_register(&db1100_spi_dev);
545 } else if (board == BCSR_WHOAMI_DB1000) {
546 c0 = AU1000_GPIO2_INT;
547 c1 = AU1000_GPIO5_INT;
548 d0 = 0; /* GPIO number, NOT irq! */
549 d1 = 3; /* GPIO number, NOT irq! */
550 s0 = AU1000_GPIO1_INT;
551 s1 = AU1000_GPIO4_INT;
552 platform_add_devices(db1000_devs, ARRAY_SIZE(db1000_devs));
553 } else if ((board == BCSR_WHOAMI_PB1500) ||
554 (board == BCSR_WHOAMI_PB1500R2)) {
555 c0 = AU1500_GPIO203_INT;
556 d0 = 1; /* GPIO number, NOT irq! */
557 s0 = AU1500_GPIO202_INT;
558 twosocks = 0;
559 flashsize = 64;
560 /* RTC and daughtercard irqs */
561 irq_set_irq_type(AU1500_GPIO204_INT, IRQ_TYPE_LEVEL_LOW);
562 irq_set_irq_type(AU1500_GPIO205_INT, IRQ_TYPE_LEVEL_LOW);
563 /* EPSON S1D13806 0x1b000000
564 * SRAM 1MB/2MB 0x1a000000
565 * DS1693 RTC 0x0c000000
567 } else if (board == BCSR_WHOAMI_PB1100) {
568 c0 = AU1100_GPIO11_INT;
569 d0 = 9; /* GPIO number, NOT irq! */
570 s0 = AU1100_GPIO10_INT;
571 twosocks = 0;
572 flashsize = 64;
573 /* pendown, rtc, daughtercard irqs */
574 irq_set_irq_type(AU1100_GPIO8_INT, IRQ_TYPE_LEVEL_LOW);
575 irq_set_irq_type(AU1100_GPIO12_INT, IRQ_TYPE_LEVEL_LOW);
576 irq_set_irq_type(AU1100_GPIO13_INT, IRQ_TYPE_LEVEL_LOW);
577 /* EPSON S1D13806 0x1b000000
578 * SRAM 1MB/2MB 0x1a000000
579 * DiskOnChip 0x0d000000
580 * DS1693 RTC 0x0c000000
582 platform_add_devices(db1100_devs, ARRAY_SIZE(db1100_devs));
583 } else
584 return 0; /* unknown board, no further dev setup to do */
586 irq_set_irq_type(c0, IRQ_TYPE_LEVEL_LOW);
587 irq_set_irq_type(s0, IRQ_TYPE_LEVEL_LOW);
589 db1x_register_pcmcia_socket(
590 AU1000_PCMCIA_ATTR_PHYS_ADDR,
591 AU1000_PCMCIA_ATTR_PHYS_ADDR + 0x000400000 - 1,
592 AU1000_PCMCIA_MEM_PHYS_ADDR,
593 AU1000_PCMCIA_MEM_PHYS_ADDR + 0x000400000 - 1,
594 AU1000_PCMCIA_IO_PHYS_ADDR,
595 AU1000_PCMCIA_IO_PHYS_ADDR + 0x000010000 - 1,
596 c0, d0, /*s0*/0, 0, 0);
598 if (twosocks) {
599 irq_set_irq_type(c1, IRQ_TYPE_LEVEL_LOW);
600 irq_set_irq_type(s1, IRQ_TYPE_LEVEL_LOW);
602 db1x_register_pcmcia_socket(
603 AU1000_PCMCIA_ATTR_PHYS_ADDR + 0x004000000,
604 AU1000_PCMCIA_ATTR_PHYS_ADDR + 0x004400000 - 1,
605 AU1000_PCMCIA_MEM_PHYS_ADDR + 0x004000000,
606 AU1000_PCMCIA_MEM_PHYS_ADDR + 0x004400000 - 1,
607 AU1000_PCMCIA_IO_PHYS_ADDR + 0x004000000,
608 AU1000_PCMCIA_IO_PHYS_ADDR + 0x004010000 - 1,
609 c1, d1, /*s1*/0, 0, 1);
612 platform_add_devices(db1x00_devs, ARRAY_SIZE(db1x00_devs));
613 db1x_register_norflash(flashsize << 20, 4 /* 32bit */, F_SWAPPED);
614 return 0;