2010-08-13 14:46:38 +03:00
|
|
|
/*
|
2010-10-21 16:03:42 +03:00
|
|
|
* f32x/c2-om.c - Basic C2 messages, driver for Openmoko GTA01/GTA02+DebugV3
|
2010-08-13 14:46:38 +03:00
|
|
|
*
|
2010-10-21 16:03:42 +03:00
|
|
|
* Written 2008, 2010 by Werner Almesberger
|
|
|
|
* Copyright 2008, 2010 Werner Almesberger
|
2010-08-13 14:46:38 +03:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "gpio.h"
|
2010-10-21 16:03:42 +03:00
|
|
|
#include "c2-drv.h"
|
2010-08-13 14:46:38 +03:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* SPI GPIOs are the same on 2410 and 2442, so this should work on GTA01 and
|
|
|
|
* on GTA02.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define C2CK 4, 11 /* E13 = SPI_MISO0 */
|
|
|
|
#define C2D 4, 13 /* E12 = SPI_CLK0 */
|
|
|
|
|
|
|
|
|
|
|
|
/* ----- Bit-level operations ---------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
static void c2_pulse(void)
|
|
|
|
{
|
2010-10-21 16:05:26 +03:00
|
|
|
gpio_low(C2CK);
|
|
|
|
gpio_high(C2CK);
|
2010-08-13 14:46:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void c2_send(uint32_t value, int bits)
|
|
|
|
{
|
2010-10-21 16:05:26 +03:00
|
|
|
int i;
|
2010-08-13 14:46:38 +03:00
|
|
|
|
2010-10-21 16:05:26 +03:00
|
|
|
for (i = 0; i != bits; i++) {
|
|
|
|
gpio_set(C2D, (value >> i) & 1);
|
|
|
|
c2_pulse();
|
|
|
|
}
|
2010-08-13 14:46:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static uint32_t c2_recv(int bits)
|
|
|
|
{
|
2010-10-21 16:05:26 +03:00
|
|
|
uint32_t v = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i != bits; i++) {
|
|
|
|
v |= gpio_get(C2D) << i;
|
|
|
|
c2_pulse();
|
|
|
|
}
|
|
|
|
return v;
|
2010-08-13 14:46:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ----- C2 Register read/write -------------------------------------------- */
|
|
|
|
|
|
|
|
|
2010-10-21 16:03:42 +03:00
|
|
|
static void om_addr_write(uint8_t addr)
|
2010-08-13 14:46:38 +03:00
|
|
|
{
|
2010-10-21 16:05:26 +03:00
|
|
|
c2_pulse();
|
|
|
|
gpio_output(C2D);
|
|
|
|
c2_send(C2_ADDR_WRITE, 2);
|
|
|
|
c2_send(addr, 8);
|
|
|
|
gpio_input(C2D);
|
|
|
|
c2_pulse();
|
2010-08-13 14:46:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-21 16:03:42 +03:00
|
|
|
static uint8_t om_addr_read(void)
|
2010-08-13 14:46:38 +03:00
|
|
|
{
|
2010-10-21 16:05:26 +03:00
|
|
|
c2_pulse();
|
|
|
|
gpio_output(C2D);
|
|
|
|
c2_send(C2_ADDR_READ, 2);
|
|
|
|
gpio_input(C2D);
|
|
|
|
c2_pulse();
|
|
|
|
return c2_recv(8);
|
2010-08-13 14:46:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-21 16:03:42 +03:00
|
|
|
static void om_data_write(uint32_t data, int bytes)
|
2010-08-13 14:46:38 +03:00
|
|
|
{
|
2010-10-21 16:05:26 +03:00
|
|
|
c2_pulse();
|
|
|
|
gpio_output(C2D);
|
|
|
|
c2_send(C2_DATA_WRITE, 2);
|
|
|
|
c2_send(bytes-1, 2);
|
|
|
|
c2_send(data, 8*bytes);
|
|
|
|
gpio_input(C2D);
|
|
|
|
c2_pulse();
|
|
|
|
while (!c2_recv(1));
|
2010-08-13 14:46:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-21 16:03:42 +03:00
|
|
|
static uint32_t om_data_read(int bytes)
|
2010-08-13 14:46:38 +03:00
|
|
|
{
|
2010-10-21 16:05:26 +03:00
|
|
|
c2_pulse();
|
|
|
|
gpio_output(C2D);
|
|
|
|
c2_send(C2_DATA_READ, 2);
|
|
|
|
c2_send(bytes-1, 2);
|
|
|
|
gpio_input(C2D);
|
|
|
|
c2_pulse();
|
|
|
|
while (!c2_recv(1));
|
|
|
|
return c2_recv(8*bytes);
|
2010-08-13 14:46:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* ----- C2 initialization ------------------------------------------------- */
|
|
|
|
|
|
|
|
|
2010-10-21 16:03:42 +03:00
|
|
|
static void om_init(void)
|
2010-08-13 14:46:38 +03:00
|
|
|
{
|
2010-10-21 16:05:26 +03:00
|
|
|
gpio_init();
|
|
|
|
gpio_input(C2D);
|
|
|
|
gpio_output(C2CK);
|
|
|
|
gpio_low(C2CK);
|
|
|
|
usleep(20);
|
|
|
|
gpio_high(C2CK);
|
|
|
|
usleep(2);
|
2010-08-13 14:46:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-21 16:03:42 +03:00
|
|
|
static void om_reset(void)
|
2010-08-13 14:46:38 +03:00
|
|
|
{
|
2010-10-21 16:05:26 +03:00
|
|
|
gpio_input(C2D);
|
|
|
|
gpio_low(C2CK);
|
|
|
|
usleep(20);
|
|
|
|
// gpio_input(C2CK);
|
|
|
|
gpio_output(C2CK);
|
|
|
|
gpio_high(C2CK);
|
2010-08-13 14:46:38 +03:00
|
|
|
}
|
2010-10-21 16:03:42 +03:00
|
|
|
|
|
|
|
|
|
|
|
/* ----- Operations -------------------------------------------------------- */
|
|
|
|
|
|
|
|
|
|
|
|
struct c2_ops c2_om = {
|
|
|
|
.init = om_init,
|
|
|
|
.reset = om_reset,
|
|
|
|
.addr_write = om_addr_write,
|
|
|
|
.addr_read = om_addr_read,
|
|
|
|
.data_write = om_data_write,
|
|
|
|
.data_read = om_data_read,
|
|
|
|
};
|