| 1442 | ****************************************************************************/ |
| 1443 | |
| 1444 | FAR struct mtd_dev_s *w25_initialize(FAR struct spi_dev_s *spi) |
| 1445 | { |
| 1446 | FAR struct w25_dev_s *priv; |
| 1447 | int ret; |
| 1448 | |
| 1449 | w25_finfo("spi: %p\n", spi); |
| 1450 | |
| 1451 | /* Allocate a state structure (we allocate the structure instead of using |
| 1452 | * a fixed, static allocation so that we can handle multiple FLASH devices. |
| 1453 | * The current implementation would handle only one FLASH part per SPI |
| 1454 | * device (only because of the SPIDEV_FLASH(0) definition) and so would |
| 1455 | * have to be extended to handle multiple FLASH parts on the same SPI bus. |
| 1456 | */ |
| 1457 | |
| 1458 | priv = kmm_zalloc(sizeof(struct w25_dev_s)); |
| 1459 | if (priv) |
| 1460 | { |
| 1461 | /* Initialize the allocated structure (unsupported methods were |
| 1462 | * nullified by kmm_zalloc). |
| 1463 | */ |
| 1464 | |
| 1465 | priv->mtd.erase = w25_erase; |
| 1466 | priv->mtd.bread = w25_bread; |
| 1467 | priv->mtd.bwrite = w25_bwrite; |
| 1468 | priv->mtd.read = w25_read; |
| 1469 | priv->mtd.ioctl = w25_ioctl; |
| 1470 | #if defined(CONFIG_MTD_BYTE_WRITE) && !defined(CONFIG_W25_READONLY) |
| 1471 | priv->mtd.write = w25_write; |
| 1472 | #endif |
| 1473 | priv->mtd.name = "w25"; |
| 1474 | priv->spi = spi; |
| 1475 | |
| 1476 | /* Deselect the FLASH */ |
| 1477 | |
| 1478 | SPI_SELECT(spi, SPIDEV_FLASH(0), false); |
| 1479 | |
| 1480 | /* Identify the FLASH chip and get its capacity */ |
| 1481 | |
| 1482 | ret = w25_readid(priv); |
| 1483 | if (ret != OK) |
| 1484 | { |
| 1485 | /* Unrecognized! Discard all of that work we just did and |
| 1486 | * return NULL |
| 1487 | */ |
| 1488 | |
| 1489 | w25_ferr("ERROR: Unrecognized\n"); |
| 1490 | kmm_free(priv); |
| 1491 | return NULL; |
| 1492 | } |
| 1493 | else |
| 1494 | { |
| 1495 | /* Make sure that the FLASH is unprotected so that we can write |
| 1496 | * into it. |
| 1497 | */ |
| 1498 | |
| 1499 | #ifndef CONFIG_W25_READONLY |
| 1500 | w25_unprotect(priv); |
| 1501 | #endif |
no test coverage detected