static void imx_ocotp_init_dt(struct device_d *dev, void __iomem *base) { char mac[6]; const __be32 *prop; struct device_node *node = dev->device_node; int len; if (!node) return; prop = of_get_property(node, "barebox,provide-mac-address", &len); if (!prop) return; while (len >= MAC_ADDRESS_PROPLEN) { struct device_node *rnode; uint32_t phandle, offset; phandle = be32_to_cpup(prop++); rnode = of_find_node_by_phandle(phandle); offset = be32_to_cpup(prop++); mac[5] = readb(base + offset); offset = inc_offset(offset); mac[4] = readb(base + offset); offset = inc_offset(offset); mac[3] = readb(base + offset); offset = inc_offset(offset); mac[2] = readb(base + offset); offset = inc_offset(offset); mac[1] = readb(base + offset); offset = inc_offset(offset); mac[0] = readb(base + offset); of_eth_register_ethaddr(rnode, mac); len -= MAC_ADDRESS_PROPLEN; } }
/* FUNCTION: int write_EEPROM(const void *buf, int count) * DESCRIPTION: Writes a sequence of count pages to an EEPROM * device starting from the current page position * of the EEPROM. The page position is then * advanced by count and, if reaching the end, * of pages, wrapped around to the beginning of * the EEPROM. * RETURN: 0 if succeed * -1 if failure * PROTOCOL: Address High|Address Low|Data 0|Data etc * 8 7 8 8 */ int write_EEPROM(const void *buf, int count) { //allocate memory for buffer char *tBuf = malloc(PAGE_BYTES + 2); int i = 0; //loop count int address; int res; int buffPtr = 2; //first check and make sure the number of pages //to write it valid if (count < 0 || count > NUM_OF_PAG) return -1; //error i = 0; for (i; i < count; i++) { memcpy(tBuf + 2, (char *)(buf + (i * PAGE_BYTES)), PAGE_BYTES); //which page to write to. Increment the //page offset as well //Address Low address = (currPage & 0x00ff); //memcpy((char *)tBuf + 1, &address, 1); memcpy(tBuf + 1, &address, 1); //Address high address = (currPage & 0xff00) >> 8; //memcpy((char *)tBuf, &address, 1); memcpy(tBuf, &address, 1); inc_offset((currPage / PAGE_BYTES) + 1); //attempt to write everything res = write(fd, tBuf, PAGE_BYTES + 2); if(res < 0) { //failure printf("\nSomething happened when writing!\n"); return -1; } //my program is too awesome and fast that //it needs to sleep usleep(5000); } free(tBuf); return 0; //SUCCESS!! }
/* FUNCTION: int read_EEPROM(void *buf, int count) * DESCRIPTION: Reads a sequence of count pages from the EEPROM * device into user memory pointed by buf. The * pages to be read start from the current page * poisition of the EEPROM. The page position is * then advanced by count and, if reaching the end * of pages, wrapped around to the beginning of * the EEPROM. * RETURN: 0 is succeed * -1 if failure */ int read_EEPROM(void *buf, int count) { //allocate some memory for the read //PAGE_BYTES -1 since char is of size 1 //and we don't care about the page //offset. That is within the device char *tBuf = malloc(PAGE_BYTES); int buffPtr = 0; int address; int i = 0; //control for the for loop int res; //loop through the number of pages that need to //be read for (i; i < count; i++) { seek_EEPROM(currPage / PAGE_BYTES); //read from the //TODO This doesn't return correctly when there are //spaces. Due to printf res = read(fd, buf + (i * PAGE_BYTES), PAGE_BYTES); if (res < 0) { printf("\nReading from the page failed!\n"); return -1; } inc_offset((currPage / PAGE_BYTES) + 1); /* if (read(fd, tbuf, PAGE_BYTES) != PAGE_BYTES) { printf("\nI2C Read Failed!\n"); return -1; } */ } return 0; //SUCCESS!! }