fwh_lock.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef FWH_LOCK_H
  3. #define FWH_LOCK_H
  4. enum fwh_lock_state {
  5. FWH_UNLOCKED = 0,
  6. FWH_DENY_WRITE = 1,
  7. FWH_IMMUTABLE = 2,
  8. FWH_DENY_READ = 4,
  9. };
  10. struct fwh_xxlock_thunk {
  11. enum fwh_lock_state val;
  12. flstate_t state;
  13. };
  14. #define FWH_XXLOCK_ONEBLOCK_LOCK ((struct fwh_xxlock_thunk){ FWH_DENY_WRITE, FL_LOCKING})
  15. #define FWH_XXLOCK_ONEBLOCK_UNLOCK ((struct fwh_xxlock_thunk){ FWH_UNLOCKED, FL_UNLOCKING})
  16. /*
  17. * This locking/unlock is specific to firmware hub parts. Only one
  18. * is known that supports the Intel command set. Firmware
  19. * hub parts cannot be interleaved as they are on the LPC bus
  20. * so this code has not been tested with interleaved chips,
  21. * and will likely fail in that context.
  22. */
  23. static int fwh_xxlock_oneblock(struct map_info *map, struct flchip *chip,
  24. unsigned long adr, int len, void *thunk)
  25. {
  26. struct cfi_private *cfi = map->fldrv_priv;
  27. struct fwh_xxlock_thunk *xxlt = (struct fwh_xxlock_thunk *)thunk;
  28. int ret;
  29. /* Refuse the operation if the we cannot look behind the chip */
  30. if (chip->start < 0x400000) {
  31. pr_debug( "MTD %s(): chip->start: %lx wanted >= 0x400000\n",
  32. __func__, chip->start );
  33. return -EIO;
  34. }
  35. /*
  36. * lock block registers:
  37. * - on 64k boundariesand
  38. * - bit 1 set high
  39. * - block lock registers are 4MiB lower - overflow subtract (danger)
  40. *
  41. * The address manipulation is first done on the logical address
  42. * which is 0 at the start of the chip, and then the offset of
  43. * the individual chip is addted to it. Any other order a weird
  44. * map offset could cause problems.
  45. */
  46. adr = (adr & ~0xffffUL) | 0x2;
  47. adr += chip->start - 0x400000;
  48. /*
  49. * This is easy because these are writes to registers and not writes
  50. * to flash memory - that means that we don't have to check status
  51. * and timeout.
  52. */
  53. mutex_lock(&chip->mutex);
  54. ret = get_chip(map, chip, adr, FL_LOCKING);
  55. if (ret) {
  56. mutex_unlock(&chip->mutex);
  57. return ret;
  58. }
  59. chip->oldstate = chip->state;
  60. chip->state = xxlt->state;
  61. map_write(map, CMD(xxlt->val), adr);
  62. /* Done and happy. */
  63. chip->state = chip->oldstate;
  64. put_chip(map, chip, adr);
  65. mutex_unlock(&chip->mutex);
  66. return 0;
  67. }
  68. static int fwh_lock_varsize(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  69. {
  70. int ret;
  71. ret = cfi_varsize_frob(mtd, fwh_xxlock_oneblock, ofs, len,
  72. (void *)&FWH_XXLOCK_ONEBLOCK_LOCK);
  73. return ret;
  74. }
  75. static int fwh_unlock_varsize(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  76. {
  77. int ret;
  78. ret = cfi_varsize_frob(mtd, fwh_xxlock_oneblock, ofs, len,
  79. (void *)&FWH_XXLOCK_ONEBLOCK_UNLOCK);
  80. return ret;
  81. }
  82. static void fixup_use_fwh_lock(struct mtd_info *mtd)
  83. {
  84. printk(KERN_NOTICE "using fwh lock/unlock method\n");
  85. /* Setup for the chips with the fwh lock method */
  86. mtd->_lock = fwh_lock_varsize;
  87. mtd->_unlock = fwh_unlock_varsize;
  88. }
  89. #endif /* FWH_LOCK_H */