gpio.c (1315B)
1 /* gpio.c 2 * 3 * Copyright 2011 Brian Swetland <swetland@frotz.net> 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 #include <fw/types.h> 19 #include <fw/lib.h> 20 #include <fw/io.h> 21 22 #include <arch/hardware.h> 23 24 static unsigned gpio_cr[4] = { 25 GPIOA_BASE + GPIO_CRL, 26 GPIOA_BASE + GPIO_CRH, 27 GPIOB_BASE + GPIO_CRL, 28 GPIOB_BASE + GPIO_CRH, 29 }; 30 31 void gpio_config(unsigned n, unsigned cfg) 32 { 33 unsigned addr = gpio_cr[n >> 3]; 34 unsigned shift = (n & 7) * 4; 35 unsigned val = readl(addr); 36 val = (val & (~(0xF << shift))) | (cfg << shift); 37 writel(val, addr); 38 } 39 40 void gpio_set(unsigned n) 41 { 42 unsigned addr = (n > 15) ? GPIOB_BASE : GPIOA_BASE; 43 writel(1 << (n & 15), addr + GPIO_BSR); 44 } 45 46 void gpio_clr(unsigned n) 47 { 48 unsigned addr = (n > 15) ? GPIOB_BASE : GPIOA_BASE; 49 writel(1 << (n & 15), addr + GPIO_BRR); 50 } 51