| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  *  arch/arm/mach-imx/generic.c | 
 | 3 |  * | 
 | 4 |  *  author: Sascha Hauer | 
 | 5 |  *  Created: april 20th, 2004 | 
 | 6 |  *  Copyright: Synertronixx GmbH | 
 | 7 |  * | 
 | 8 |  *  Common code for i.MX machines | 
 | 9 |  * | 
 | 10 |  * This program is free software; you can redistribute it and/or modify | 
 | 11 |  * it under the terms of the GNU General Public License as published by | 
 | 12 |  * the Free Software Foundation; either version 2 of the License, or | 
 | 13 |  * (at your option) any later version. | 
 | 14 |  * | 
 | 15 |  * This program is distributed in the hope that it will be useful, | 
 | 16 |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
 | 17 |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
 | 18 |  * GNU General Public License for more details. | 
 | 19 |  * | 
 | 20 |  * You should have received a copy of the GNU General Public License | 
 | 21 |  * along with this program; if not, write to the Free Software | 
 | 22 |  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA | 
 | 23 |  * | 
 | 24 |  */ | 
| Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 25 | #include <linux/platform_device.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/init.h> | 
 | 27 | #include <linux/kernel.h> | 
 | 28 | #include <linux/module.h> | 
| Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame] | 29 | #include <linux/string.h> | 
 | 30 |  | 
| Pavel Pisa | b3e6a50 | 2007-05-12 14:31:17 +0100 | [diff] [blame] | 31 | #include <asm/errno.h> | 
| Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 32 | #include <mach/imxfb.h> | 
 | 33 | #include <mach/hardware.h> | 
 | 34 | #include <mach/imx-regs.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 |  | 
 | 36 | #include <asm/mach/map.h> | 
| Russell King | a09e64f | 2008-08-05 16:14:15 +0100 | [diff] [blame] | 37 | #include <mach/mmc.h> | 
 | 38 | #include <mach/gpio.h> | 
| Pavel Pisa | b3e6a50 | 2007-05-12 14:31:17 +0100 | [diff] [blame] | 39 |  | 
 | 40 | unsigned long imx_gpio_alloc_map[(GPIO_PORT_MAX + 1) * 32 / BITS_PER_LONG]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 |  | 
 | 42 | void imx_gpio_mode(int gpio_mode) | 
 | 43 | { | 
 | 44 | 	unsigned int pin = gpio_mode & GPIO_PIN_MASK; | 
| Sascha Hauer | 0a5b0aa | 2005-10-04 23:17:52 +0100 | [diff] [blame] | 45 | 	unsigned int port = (gpio_mode & GPIO_PORT_MASK) >> GPIO_PORT_SHIFT; | 
 | 46 | 	unsigned int ocr = (gpio_mode & GPIO_OCR_MASK) >> GPIO_OCR_SHIFT; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | 	unsigned int tmp; | 
 | 48 |  | 
 | 49 | 	/* Pullup enable */ | 
 | 50 | 	if(gpio_mode & GPIO_PUEN) | 
 | 51 | 		PUEN(port) |= (1<<pin); | 
 | 52 | 	else | 
 | 53 | 		PUEN(port) &= ~(1<<pin); | 
 | 54 |  | 
 | 55 | 	/* Data direction */ | 
 | 56 | 	if(gpio_mode & GPIO_OUT) | 
 | 57 | 		DDIR(port) |= 1<<pin; | 
 | 58 | 	else | 
 | 59 | 		DDIR(port) &= ~(1<<pin); | 
 | 60 |  | 
 | 61 | 	/* Primary / alternate function */ | 
 | 62 | 	if(gpio_mode & GPIO_AF) | 
 | 63 | 		GPR(port) |= (1<<pin); | 
 | 64 | 	else | 
 | 65 | 		GPR(port) &= ~(1<<pin); | 
 | 66 |  | 
 | 67 | 	/* use as gpio? */ | 
| Sascha Hauer | 0a5b0aa | 2005-10-04 23:17:52 +0100 | [diff] [blame] | 68 | 	if(gpio_mode &  GPIO_GIUS) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | 		GIUS(port) |= (1<<pin); | 
 | 70 | 	else | 
 | 71 | 		GIUS(port) &= ~(1<<pin); | 
 | 72 |  | 
 | 73 | 	/* Output / input configuration */ | 
 | 74 | 	/* FIXME: I'm not very sure about OCR and ICONF, someone | 
 | 75 | 	 * should have a look over it | 
 | 76 | 	 */ | 
 | 77 | 	if(pin<16) { | 
 | 78 | 		tmp = OCR1(port); | 
 | 79 | 		tmp &= ~( 3<<(pin*2)); | 
 | 80 | 		tmp |= (ocr << (pin*2)); | 
 | 81 | 		OCR1(port) = tmp; | 
 | 82 |  | 
| Sascha Hauer | 0a5b0aa | 2005-10-04 23:17:52 +0100 | [diff] [blame] | 83 | 		ICONFA1(port) &= ~( 3<<(pin*2)); | 
 | 84 | 		ICONFA1(port) |= ((gpio_mode >> GPIO_AOUT_SHIFT) & 3) << (pin * 2); | 
 | 85 | 		ICONFB1(port) &= ~( 3<<(pin*2)); | 
 | 86 | 		ICONFB1(port) |= ((gpio_mode >> GPIO_BOUT_SHIFT) & 3) << (pin * 2); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | 	} else { | 
 | 88 | 		tmp = OCR2(port); | 
 | 89 | 		tmp &= ~( 3<<((pin-16)*2)); | 
 | 90 | 		tmp |= (ocr << ((pin-16)*2)); | 
 | 91 | 		OCR2(port) = tmp; | 
 | 92 |  | 
| Sascha Hauer | 0a5b0aa | 2005-10-04 23:17:52 +0100 | [diff] [blame] | 93 | 		ICONFA2(port) &= ~( 3<<((pin-16)*2)); | 
 | 94 | 		ICONFA2(port) |= ((gpio_mode >> GPIO_AOUT_SHIFT) & 3) << ((pin-16) * 2); | 
 | 95 | 		ICONFB2(port) &= ~( 3<<((pin-16)*2)); | 
 | 96 | 		ICONFB2(port) |= ((gpio_mode >> GPIO_BOUT_SHIFT) & 3) << ((pin-16) * 2); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | 	} | 
 | 98 | } | 
 | 99 |  | 
 | 100 | EXPORT_SYMBOL(imx_gpio_mode); | 
 | 101 |  | 
| Pavel Pisa | b3e6a50 | 2007-05-12 14:31:17 +0100 | [diff] [blame] | 102 | int imx_gpio_request(unsigned gpio, const char *label) | 
 | 103 | { | 
| Pavel Pisa | a98b38b | 2007-08-29 23:23:38 +0100 | [diff] [blame] | 104 | 	if(gpio >= (GPIO_PORT_MAX + 1) * 32) { | 
| Pavel Pisa | b3e6a50 | 2007-05-12 14:31:17 +0100 | [diff] [blame] | 105 | 		printk(KERN_ERR "imx_gpio: Attempt to request nonexistent GPIO %d for \"%s\"\n", | 
 | 106 | 			gpio, label ? label : "?"); | 
 | 107 | 		return -EINVAL; | 
| Pavel Pisa | a98b38b | 2007-08-29 23:23:38 +0100 | [diff] [blame] | 108 | 	} | 
| Pavel Pisa | b3e6a50 | 2007-05-12 14:31:17 +0100 | [diff] [blame] | 109 |  | 
 | 110 | 	if(test_and_set_bit(gpio, imx_gpio_alloc_map)) { | 
 | 111 | 		printk(KERN_ERR "imx_gpio: GPIO %d already used. Allocation for \"%s\" failed\n", | 
 | 112 | 			gpio, label ? label : "?"); | 
 | 113 | 		return -EBUSY; | 
 | 114 | 	} | 
 | 115 |  | 
 | 116 | 	return 0; | 
 | 117 | } | 
 | 118 |  | 
 | 119 | EXPORT_SYMBOL(imx_gpio_request); | 
 | 120 |  | 
 | 121 | void imx_gpio_free(unsigned gpio) | 
 | 122 | { | 
 | 123 | 	if(gpio >= (GPIO_PORT_MAX + 1) * 32) | 
 | 124 | 		return; | 
 | 125 |  | 
 | 126 | 	clear_bit(gpio, imx_gpio_alloc_map); | 
 | 127 | } | 
 | 128 |  | 
 | 129 | EXPORT_SYMBOL(imx_gpio_free); | 
 | 130 |  | 
 | 131 | int imx_gpio_direction_input(unsigned gpio) | 
 | 132 | { | 
| Pavel Pisa | a98b38b | 2007-08-29 23:23:38 +0100 | [diff] [blame] | 133 | 	imx_gpio_mode(gpio | GPIO_IN | GPIO_GIUS | GPIO_DR); | 
| Pavel Pisa | b3e6a50 | 2007-05-12 14:31:17 +0100 | [diff] [blame] | 134 | 	return 0; | 
 | 135 | } | 
 | 136 |  | 
 | 137 | EXPORT_SYMBOL(imx_gpio_direction_input); | 
 | 138 |  | 
 | 139 | int imx_gpio_direction_output(unsigned gpio, int value) | 
 | 140 | { | 
 | 141 | 	imx_gpio_set_value(gpio, value); | 
| Pavel Pisa | a228d6e | 2007-08-20 21:39:41 +0100 | [diff] [blame] | 142 | 	imx_gpio_mode(gpio | GPIO_OUT | GPIO_GIUS | GPIO_DR); | 
| Pavel Pisa | b3e6a50 | 2007-05-12 14:31:17 +0100 | [diff] [blame] | 143 | 	return 0; | 
 | 144 | } | 
 | 145 |  | 
 | 146 | EXPORT_SYMBOL(imx_gpio_direction_output); | 
 | 147 |  | 
 | 148 | int imx_gpio_setup_multiple_pins(const int *pin_list, unsigned count, | 
 | 149 | 				int alloc_mode, const char *label) | 
 | 150 | { | 
 | 151 | 	const int *p = pin_list; | 
 | 152 | 	int i; | 
 | 153 | 	unsigned gpio; | 
 | 154 | 	unsigned mode; | 
 | 155 |  | 
 | 156 | 	for (i = 0; i < count; i++) { | 
 | 157 | 		gpio = *p & (GPIO_PIN_MASK | GPIO_PORT_MASK); | 
 | 158 | 		mode = *p & ~(GPIO_PIN_MASK | GPIO_PORT_MASK); | 
 | 159 |  | 
 | 160 | 		if (gpio >= (GPIO_PORT_MAX + 1) * 32) | 
 | 161 | 			goto setup_error; | 
 | 162 |  | 
 | 163 | 		if (alloc_mode & IMX_GPIO_ALLOC_MODE_RELEASE) | 
 | 164 | 			imx_gpio_free(gpio); | 
 | 165 | 		else if (!(alloc_mode & IMX_GPIO_ALLOC_MODE_NO_ALLOC)) | 
 | 166 | 			if (imx_gpio_request(gpio, label)) | 
 | 167 | 				if (!(alloc_mode & IMX_GPIO_ALLOC_MODE_TRY_ALLOC)) | 
 | 168 | 					goto setup_error; | 
 | 169 |  | 
 | 170 | 		if (!(alloc_mode & (IMX_GPIO_ALLOC_MODE_ALLOC_ONLY | | 
 | 171 | 				    IMX_GPIO_ALLOC_MODE_RELEASE))) | 
 | 172 | 			imx_gpio_mode(gpio | mode); | 
 | 173 |  | 
 | 174 | 		p++; | 
 | 175 | 	} | 
 | 176 | 	return 0; | 
 | 177 |  | 
 | 178 | setup_error: | 
 | 179 | 	if(alloc_mode & (IMX_GPIO_ALLOC_MODE_NO_ALLOC | | 
 | 180 | 		         IMX_GPIO_ALLOC_MODE_TRY_ALLOC)) | 
 | 181 | 		return -EINVAL; | 
 | 182 |  | 
 | 183 | 	while (p != pin_list) { | 
 | 184 | 		p--; | 
 | 185 | 		gpio = *p & (GPIO_PIN_MASK | GPIO_PORT_MASK); | 
 | 186 | 		imx_gpio_free(gpio); | 
 | 187 | 	} | 
 | 188 |  | 
 | 189 | 	return -EINVAL; | 
 | 190 | } | 
 | 191 |  | 
 | 192 | EXPORT_SYMBOL(imx_gpio_setup_multiple_pins); | 
 | 193 |  | 
 | 194 | void __imx_gpio_set_value(unsigned gpio, int value) | 
 | 195 | { | 
 | 196 | 	imx_gpio_set_value_inline(gpio, value); | 
 | 197 | } | 
 | 198 |  | 
 | 199 | EXPORT_SYMBOL(__imx_gpio_set_value); | 
 | 200 |  | 
 | 201 | int imx_gpio_to_irq(unsigned gpio) | 
 | 202 | { | 
 | 203 | 	return IRQ_GPIOA(0) + gpio; | 
 | 204 | } | 
 | 205 |  | 
 | 206 | EXPORT_SYMBOL(imx_gpio_to_irq); | 
 | 207 |  | 
 | 208 | int imx_irq_to_gpio(unsigned irq) | 
 | 209 | { | 
 | 210 | 	if (irq < IRQ_GPIOA(0)) | 
 | 211 | 		return -EINVAL; | 
 | 212 | 	return irq - IRQ_GPIOA(0); | 
 | 213 | } | 
 | 214 |  | 
 | 215 | EXPORT_SYMBOL(imx_irq_to_gpio); | 
 | 216 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | static struct resource imx_mmc_resources[] = { | 
 | 218 | 	[0] = { | 
 | 219 | 		.start	= 0x00214000, | 
 | 220 | 		.end	= 0x002140FF, | 
 | 221 | 		.flags	= IORESOURCE_MEM, | 
 | 222 | 	}, | 
 | 223 | 	[1] = { | 
 | 224 | 		.start	= (SDHC_INT), | 
 | 225 | 		.end	= (SDHC_INT), | 
 | 226 | 		.flags	= IORESOURCE_IRQ, | 
 | 227 | 	}, | 
 | 228 | }; | 
 | 229 |  | 
| Pavel Pisa | 56ca904 | 2006-04-02 19:27:07 +0100 | [diff] [blame] | 230 | static u64 imxmmmc_dmamask = 0xffffffffUL; | 
 | 231 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | static struct platform_device imx_mmc_device = { | 
 | 233 | 	.name		= "imx-mmc", | 
 | 234 | 	.id		= 0, | 
| Pavel Pisa | 56ca904 | 2006-04-02 19:27:07 +0100 | [diff] [blame] | 235 | 	.dev		= { | 
 | 236 | 		.dma_mask = &imxmmmc_dmamask, | 
 | 237 | 		.coherent_dma_mask = 0xffffffff, | 
 | 238 | 	}, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | 	.num_resources	= ARRAY_SIZE(imx_mmc_resources), | 
 | 240 | 	.resource	= imx_mmc_resources, | 
 | 241 | }; | 
 | 242 |  | 
| Pavel Pisa | 56ca904 | 2006-04-02 19:27:07 +0100 | [diff] [blame] | 243 | void __init imx_set_mmc_info(struct imxmmc_platform_data *info) | 
 | 244 | { | 
 | 245 | 	imx_mmc_device.dev.platform_data = info; | 
 | 246 | } | 
| Pavel Pisa | 56ca904 | 2006-04-02 19:27:07 +0100 | [diff] [blame] | 247 |  | 
| Sascha Hauer | a493820 | 2005-05-03 22:57:56 +0100 | [diff] [blame] | 248 | static struct imxfb_mach_info imx_fb_info; | 
 | 249 |  | 
 | 250 | void __init set_imx_fb_info(struct imxfb_mach_info *hard_imx_fb_info) | 
 | 251 | { | 
 | 252 | 	memcpy(&imx_fb_info,hard_imx_fb_info,sizeof(struct imxfb_mach_info)); | 
 | 253 | } | 
| Sascha Hauer | a493820 | 2005-05-03 22:57:56 +0100 | [diff] [blame] | 254 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | static struct resource imxfb_resources[] = { | 
 | 256 | 	[0] = { | 
 | 257 | 		.start	= 0x00205000, | 
 | 258 | 		.end	= 0x002050FF, | 
 | 259 | 		.flags	= IORESOURCE_MEM, | 
 | 260 | 	}, | 
 | 261 | 	[1] = { | 
 | 262 | 		.start	= LCDC_INT, | 
 | 263 | 		.end	= LCDC_INT, | 
 | 264 | 		.flags	= IORESOURCE_IRQ, | 
 | 265 | 	}, | 
 | 266 | }; | 
 | 267 |  | 
| Sascha Hauer | a493820 | 2005-05-03 22:57:56 +0100 | [diff] [blame] | 268 | static u64 fb_dma_mask = ~(u64)0; | 
 | 269 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | static struct platform_device imxfb_device = { | 
 | 271 | 	.name		= "imx-fb", | 
 | 272 | 	.id		= 0, | 
| Sascha Hauer | a493820 | 2005-05-03 22:57:56 +0100 | [diff] [blame] | 273 | 	.dev		= { | 
 | 274 |  		.platform_data	= &imx_fb_info, | 
 | 275 | 		.dma_mask	= &fb_dma_mask, | 
 | 276 | 		.coherent_dma_mask = 0xffffffff, | 
 | 277 | 	}, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | 	.num_resources	= ARRAY_SIZE(imxfb_resources), | 
 | 279 | 	.resource	= imxfb_resources, | 
 | 280 | }; | 
 | 281 |  | 
 | 282 | static struct platform_device *devices[] __initdata = { | 
 | 283 | 	&imx_mmc_device, | 
 | 284 | 	&imxfb_device, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | }; | 
 | 286 |  | 
 | 287 | static struct map_desc imx_io_desc[] __initdata = { | 
| Deepak Saxena | adecef7 | 2005-10-28 15:19:10 +0100 | [diff] [blame] | 288 | 	{ | 
 | 289 | 		.virtual	= IMX_IO_BASE, | 
 | 290 | 		.pfn		= __phys_to_pfn(IMX_IO_PHYS), | 
 | 291 | 		.length		= IMX_IO_SIZE, | 
 | 292 | 		.type		= MT_DEVICE | 
 | 293 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | }; | 
 | 295 |  | 
 | 296 | void __init | 
 | 297 | imx_map_io(void) | 
 | 298 | { | 
 | 299 | 	iotable_init(imx_io_desc, ARRAY_SIZE(imx_io_desc)); | 
 | 300 | } | 
 | 301 |  | 
 | 302 | static int __init imx_init(void) | 
 | 303 | { | 
 | 304 | 	return platform_add_devices(devices, ARRAY_SIZE(devices)); | 
 | 305 | } | 
 | 306 |  | 
 | 307 | subsys_initcall(imx_init); |