Changeset 462054a in mainline for uspace/drv/bus/usb/ar9271/hw.c


Ignore:
Timestamp:
2015-01-29T17:46:19Z (9 years ago)
Author:
Jan Kolarik <kolarik@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
ab365c4
Parents:
01784d2
Message:

Finished HW initialization (but skipped several things, will finish them later if they will be necessary) and finally bringed up device led light :)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/bus/usb/ar9271/hw.c

    r01784d2 r462054a  
    3535#include <usb/debug.h>
    3636#include <unistd.h>
     37#include <nic.h>
    3738
    3839#include "hw.h"
     
    179180}
    180181
     182static int hw_addr_init(ar9271_t *ar9271)
     183{
     184        int rc;
     185        uint32_t value;
     186        nic_address_t ar9271_address;
     187       
     188        for(int i = 0; i < 3; i++) {
     189                rc = wmi_reg_read(ar9271->htc_device,
     190                        AR9271_EEPROM_MAC_ADDR_START + i*4,
     191                        &value);
     192               
     193                if(rc != EOK) {
     194                        usb_log_error("Failed to read %d. byte of MAC address."
     195                                "\n", i);
     196                        return rc;
     197                }
     198               
     199                uint16_t two_bytes = uint16_t_be2host(value);
     200                ar9271_address.address[2*i] = two_bytes >> 8;
     201                ar9271_address.address[2*i+1] = two_bytes & 0xff;
     202        }
     203       
     204        nic_t *nic = nic_get_from_ddf_dev(ar9271->ddf_device);
     205       
     206        rc = nic_report_address(nic, &ar9271_address);
     207        if(rc != EOK) {
     208                usb_log_error("Failed to report NIC HW address.\n");
     209                        return rc;
     210        }
     211       
     212        return EOK;
     213}
     214
     215static int hw_gpio_set_output(ar9271_t *ar9271, uint32_t gpio, uint32_t type)
     216{
     217        uint32_t address, gpio_shift, temp;
     218       
     219        if(gpio > 11) {
     220                address = AR9271_GPIO_OUT_MUX3;
     221        } else if(gpio > 5) {
     222                address = AR9271_GPIO_OUT_MUX2;
     223        } else {
     224                address = AR9271_GPIO_OUT_MUX1;
     225        }
     226       
     227        gpio_shift = (gpio % 6) * 5;
     228       
     229        int rc = wmi_reg_read(ar9271->htc_device, address, &temp);
     230        if(rc != EOK) {
     231                usb_log_error("Failed to read GPIO output mux.\n");
     232                return rc;
     233        }
     234       
     235        temp = ((temp & 0x1F0) << 1) | (temp & ~0x1F0);
     236        temp &= ~(0x1f << gpio_shift);
     237        temp |= (type << gpio_shift);
     238       
     239        rc = wmi_reg_write(ar9271->htc_device, address, temp);
     240        if(rc != EOK) {
     241                usb_log_error("Failed to write GPIO output mux.\n");
     242                return rc;
     243        }
     244       
     245        gpio_shift = 2 * gpio;
     246       
     247        rc = wmi_reg_clear_set_bit(ar9271->htc_device, AR9271_GPIO_OE_OUT,
     248                AR9271_GPIO_OE_OUT_ALWAYS << gpio_shift,
     249                AR9271_GPIO_OE_OUT_ALWAYS << gpio_shift);
     250        if(rc != EOK) {
     251                usb_log_error("Failed to config GPIO as output.\n");
     252                return rc;
     253        }
     254       
     255        return EOK;
     256}
     257
     258static int hw_gpio_set_value(ar9271_t *ar9271, uint32_t gpio, uint32_t value)
     259{
     260        int rc = wmi_reg_clear_set_bit(ar9271->htc_device, AR9271_GPIO_IN_OUT,
     261                (~value & 1) << gpio, 1 << gpio);
     262        if(rc != EOK) {
     263                usb_log_error("Failed to set GPIO.\n");
     264                return rc;
     265        }
     266       
     267        return EOK;
     268}
     269
    181270/**
    182271 * Hardware reset of AR9271 device.
     
    186275 * @return EOK if succeed, negative error code otherwise.
    187276 */
    188 int hw_reset(ar9271_t *ar9271)
     277static int hw_reset(ar9271_t *ar9271)
    189278{
    190279        int rc = hw_reset_power_on(ar9271);
     
    200289        }
    201290       
    202         /* TODO: Finish HW init (EEPROM init, MAC ADDR init). */
     291        rc = hw_addr_init(ar9271);
     292        if(rc != EOK) {
     293                usb_log_error("Failed to init HW addr.\n");
     294                return rc;
     295        }
     296       
     297        return EOK;
     298}
     299
     300static int hw_init_led(ar9271_t *ar9271)
     301{
     302        int rc = hw_gpio_set_output(ar9271, AR9271_LED_PIN,
     303                AR9271_GPIO_OUT_MUX_AS_OUT);
     304        if(rc != EOK) {
     305                usb_log_error("Failed to set led GPIO to output.\n");
     306                return rc;
     307        }
     308       
     309        rc = hw_gpio_set_value(ar9271, AR9271_LED_PIN, 0);
     310        if(rc != EOK) {
     311                usb_log_error("Failed to init bring up GPIO led.\n");
     312                return rc;
     313        }
    203314       
    204315        return EOK;
     
    220331        }
    221332       
     333        rc = hw_init_led(ar9271);
     334        if(rc != EOK) {
     335                usb_log_error("Failed to HW init led.\n");
     336                return rc;
     337        }
     338       
    222339        usb_log_info("HW initialization finished successfully.\n");
    223340       
Note: See TracChangeset for help on using the changeset viewer.