#ifndef NANDBUS_H_
#define NANDBUS_H_

#include <pthread.h>

#include <system.h>
#include <req.h>

#define RB_BUSY  0
#define RB_READY 1

struct {
  struct {
    pthread_mutex_t mutex;
    pthread_cond_t  cond;
    pthread_t       thread;

    req_t* queued;
    req_t* current;

    int counter;
    uint8_t buf[NAND_TOTAL_SIZE]; /* for accesses that also require spare area */
    bool_t rb;
  } chip[NAND_CHIPS_PER_BUS];

  pthread_mutex_t mutex;
  pthread_cond_t  cond;

  int current_dma_transfer;
} nandbus[NAND_BUSES];

typedef struct nandbus_specific_st{
    uint32_t cmd;
    uint32_t bus;
    uint32_t chip;
    uint32_t page;
    uint32_t offset;
    uint32_t len;
    uint8_t* buf;
    uint8_t* spare;
} nandbus_specific_t;

error_t nandbusCmd(req_t *req, uint8_t* buf,
									 uint32_t bus, uint32_t chip,
									 uint32_t cmd,  uint32_t page, uint32_t offset, uint32_t len,
									 uint8_t* spare_buf, bool_t with_spare);

void* nandbusThread(void* argument);

void nandbusAlloc(req_t* caller_req);

void nandbusHandler(req_t* req, void* response);

void nandbusInit(void);


#endif
