docg3.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * Handles the M-Systems DiskOnChip G3 chip
  4. *
  5. * Copyright (C) 2011 Robert Jarzmik
  6. */
  7. #ifndef _MTD_DOCG3_H
  8. #define _MTD_DOCG3_H
  9. #include <linux/mtd/mtd.h>
  10. /*
  11. * Flash memory areas :
  12. * - 0x0000 .. 0x07ff : IPL
  13. * - 0x0800 .. 0x0fff : Data area
  14. * - 0x1000 .. 0x17ff : Registers
  15. * - 0x1800 .. 0x1fff : Unknown
  16. */
  17. #define DOC_IOSPACE_IPL 0x0000
  18. #define DOC_IOSPACE_DATA 0x0800
  19. #define DOC_IOSPACE_SIZE 0x2000
  20. /*
  21. * DOC G3 layout and adressing scheme
  22. * A page address for the block "b", plane "P" and page "p":
  23. * address = [bbbb bPpp pppp]
  24. */
  25. #define DOC_ADDR_PAGE_MASK 0x3f
  26. #define DOC_ADDR_BLOCK_SHIFT 6
  27. #define DOC_LAYOUT_NBPLANES 2
  28. #define DOC_LAYOUT_PAGES_PER_BLOCK 64
  29. #define DOC_LAYOUT_PAGE_SIZE 512
  30. #define DOC_LAYOUT_OOB_SIZE 16
  31. #define DOC_LAYOUT_WEAR_SIZE 8
  32. #define DOC_LAYOUT_PAGE_OOB_SIZE \
  33. (DOC_LAYOUT_PAGE_SIZE + DOC_LAYOUT_OOB_SIZE)
  34. #define DOC_LAYOUT_WEAR_OFFSET (DOC_LAYOUT_PAGE_OOB_SIZE * 2)
  35. #define DOC_LAYOUT_BLOCK_SIZE \
  36. (DOC_LAYOUT_PAGES_PER_BLOCK * DOC_LAYOUT_PAGE_SIZE)
  37. /*
  38. * ECC related constants
  39. */
  40. #define DOC_ECC_BCH_M 14
  41. #define DOC_ECC_BCH_T 4
  42. #define DOC_ECC_BCH_PRIMPOLY 0x4443
  43. #define DOC_ECC_BCH_SIZE 7
  44. #define DOC_ECC_BCH_COVERED_BYTES \
  45. (DOC_LAYOUT_PAGE_SIZE + DOC_LAYOUT_OOB_PAGEINFO_SZ + \
  46. DOC_LAYOUT_OOB_HAMMING_SZ)
  47. #define DOC_ECC_BCH_TOTAL_BYTES \
  48. (DOC_ECC_BCH_COVERED_BYTES + DOC_LAYOUT_OOB_BCH_SZ)
  49. /*
  50. * Blocks distribution
  51. */
  52. #define DOC_LAYOUT_BLOCK_BBT 0
  53. #define DOC_LAYOUT_BLOCK_OTP 0
  54. #define DOC_LAYOUT_BLOCK_FIRST_DATA 6
  55. #define DOC_LAYOUT_PAGE_BBT 4
  56. /*
  57. * Extra page OOB (16 bytes wide) layout
  58. */
  59. #define DOC_LAYOUT_OOB_PAGEINFO_OFS 0
  60. #define DOC_LAYOUT_OOB_HAMMING_OFS 7
  61. #define DOC_LAYOUT_OOB_BCH_OFS 8
  62. #define DOC_LAYOUT_OOB_UNUSED_OFS 15
  63. #define DOC_LAYOUT_OOB_PAGEINFO_SZ 7
  64. #define DOC_LAYOUT_OOB_HAMMING_SZ 1
  65. #define DOC_LAYOUT_OOB_BCH_SZ 7
  66. #define DOC_LAYOUT_OOB_UNUSED_SZ 1
  67. #define DOC_CHIPID_G3 0x200
  68. #define DOC_ERASE_MARK 0xaa
  69. #define DOC_MAX_NBFLOORS 4
  70. /*
  71. * Flash registers
  72. */
  73. #define DOC_CHIPID 0x1000
  74. #define DOC_TEST 0x1004
  75. #define DOC_BUSLOCK 0x1006
  76. #define DOC_ENDIANCONTROL 0x1008
  77. #define DOC_DEVICESELECT 0x100a
  78. #define DOC_ASICMODE 0x100c
  79. #define DOC_CONFIGURATION 0x100e
  80. #define DOC_INTERRUPTCONTROL 0x1010
  81. #define DOC_READADDRESS 0x101a
  82. #define DOC_DATAEND 0x101e
  83. #define DOC_INTERRUPTSTATUS 0x1020
  84. #define DOC_FLASHSEQUENCE 0x1032
  85. #define DOC_FLASHCOMMAND 0x1034
  86. #define DOC_FLASHADDRESS 0x1036
  87. #define DOC_FLASHCONTROL 0x1038
  88. #define DOC_NOP 0x103e
  89. #define DOC_ECCCONF0 0x1040
  90. #define DOC_ECCCONF1 0x1042
  91. #define DOC_ECCPRESET 0x1044
  92. #define DOC_HAMMINGPARITY 0x1046
  93. #define DOC_BCH_HW_ECC(idx) (0x1048 + idx)
  94. #define DOC_PROTECTION 0x1056
  95. #define DOC_DPS0_KEY 0x105c
  96. #define DOC_DPS1_KEY 0x105e
  97. #define DOC_DPS0_ADDRLOW 0x1060
  98. #define DOC_DPS0_ADDRHIGH 0x1062
  99. #define DOC_DPS1_ADDRLOW 0x1064
  100. #define DOC_DPS1_ADDRHIGH 0x1066
  101. #define DOC_DPS0_STATUS 0x106c
  102. #define DOC_DPS1_STATUS 0x106e
  103. #define DOC_ASICMODECONFIRM 0x1072
  104. #define DOC_CHIPID_INV 0x1074
  105. #define DOC_POWERMODE 0x107c
  106. /*
  107. * Flash sequences
  108. * A sequence is preset before one or more commands are input to the chip.
  109. */
  110. #define DOC_SEQ_RESET 0x00
  111. #define DOC_SEQ_PAGE_SIZE_532 0x03
  112. #define DOC_SEQ_SET_FASTMODE 0x05
  113. #define DOC_SEQ_SET_RELIABLEMODE 0x09
  114. #define DOC_SEQ_READ 0x12
  115. #define DOC_SEQ_SET_PLANE1 0x0e
  116. #define DOC_SEQ_SET_PLANE2 0x10
  117. #define DOC_SEQ_PAGE_SETUP 0x1d
  118. #define DOC_SEQ_ERASE 0x27
  119. #define DOC_SEQ_PLANES_STATUS 0x31
  120. /*
  121. * Flash commands
  122. */
  123. #define DOC_CMD_READ_PLANE1 0x00
  124. #define DOC_CMD_SET_ADDR_READ 0x05
  125. #define DOC_CMD_READ_ALL_PLANES 0x30
  126. #define DOC_CMD_READ_PLANE2 0x50
  127. #define DOC_CMD_READ_FLASH 0xe0
  128. #define DOC_CMD_PAGE_SIZE_532 0x3c
  129. #define DOC_CMD_PROG_BLOCK_ADDR 0x60
  130. #define DOC_CMD_PROG_CYCLE1 0x80
  131. #define DOC_CMD_PROG_CYCLE2 0x10
  132. #define DOC_CMD_PROG_CYCLE3 0x11
  133. #define DOC_CMD_ERASECYCLE2 0xd0
  134. #define DOC_CMD_READ_STATUS 0x70
  135. #define DOC_CMD_PLANES_STATUS 0x71
  136. #define DOC_CMD_RELIABLE_MODE 0x22
  137. #define DOC_CMD_FAST_MODE 0xa2
  138. #define DOC_CMD_RESET 0xff
  139. /*
  140. * Flash register : DOC_FLASHCONTROL
  141. */
  142. #define DOC_CTRL_VIOLATION 0x20
  143. #define DOC_CTRL_CE 0x10
  144. #define DOC_CTRL_UNKNOWN_BITS 0x08
  145. #define DOC_CTRL_PROTECTION_ERROR 0x04
  146. #define DOC_CTRL_SEQUENCE_ERROR 0x02
  147. #define DOC_CTRL_FLASHREADY 0x01
  148. /*
  149. * Flash register : DOC_ASICMODE
  150. */
  151. #define DOC_ASICMODE_RESET 0x00
  152. #define DOC_ASICMODE_NORMAL 0x01
  153. #define DOC_ASICMODE_POWERDOWN 0x02
  154. #define DOC_ASICMODE_MDWREN 0x04
  155. #define DOC_ASICMODE_BDETCT_RESET 0x08
  156. #define DOC_ASICMODE_RSTIN_RESET 0x10
  157. #define DOC_ASICMODE_RAM_WE 0x20
  158. /*
  159. * Flash register : DOC_ECCCONF0
  160. */
  161. #define DOC_ECCCONF0_WRITE_MODE 0x0000
  162. #define DOC_ECCCONF0_READ_MODE 0x8000
  163. #define DOC_ECCCONF0_AUTO_ECC_ENABLE 0x4000
  164. #define DOC_ECCCONF0_HAMMING_ENABLE 0x1000
  165. #define DOC_ECCCONF0_BCH_ENABLE 0x0800
  166. #define DOC_ECCCONF0_DATA_BYTES_MASK 0x07ff
  167. /*
  168. * Flash register : DOC_ECCCONF1
  169. */
  170. #define DOC_ECCCONF1_BCH_SYNDROM_ERR 0x80
  171. #define DOC_ECCCONF1_UNKOWN1 0x40
  172. #define DOC_ECCCONF1_PAGE_IS_WRITTEN 0x20
  173. #define DOC_ECCCONF1_UNKOWN3 0x10
  174. #define DOC_ECCCONF1_HAMMING_BITS_MASK 0x0f
  175. /*
  176. * Flash register : DOC_PROTECTION
  177. */
  178. #define DOC_PROTECT_FOUNDRY_OTP_LOCK 0x01
  179. #define DOC_PROTECT_CUSTOMER_OTP_LOCK 0x02
  180. #define DOC_PROTECT_LOCK_INPUT 0x04
  181. #define DOC_PROTECT_STICKY_LOCK 0x08
  182. #define DOC_PROTECT_PROTECTION_ENABLED 0x10
  183. #define DOC_PROTECT_IPL_DOWNLOAD_LOCK 0x20
  184. #define DOC_PROTECT_PROTECTION_ERROR 0x80
  185. /*
  186. * Flash register : DOC_DPS0_STATUS and DOC_DPS1_STATUS
  187. */
  188. #define DOC_DPS_OTP_PROTECTED 0x01
  189. #define DOC_DPS_READ_PROTECTED 0x02
  190. #define DOC_DPS_WRITE_PROTECTED 0x04
  191. #define DOC_DPS_HW_LOCK_ENABLED 0x08
  192. #define DOC_DPS_KEY_OK 0x80
  193. /*
  194. * Flash register : DOC_CONFIGURATION
  195. */
  196. #define DOC_CONF_IF_CFG 0x80
  197. #define DOC_CONF_MAX_ID_MASK 0x30
  198. #define DOC_CONF_VCCQ_3V 0x01
  199. /*
  200. * Flash register : DOC_READADDRESS
  201. */
  202. #define DOC_READADDR_INC 0x8000
  203. #define DOC_READADDR_ONE_BYTE 0x4000
  204. #define DOC_READADDR_ADDR_MASK 0x1fff
  205. /*
  206. * Flash register : DOC_POWERMODE
  207. */
  208. #define DOC_POWERDOWN_READY 0x80
  209. /*
  210. * Status of erase and write operation
  211. */
  212. #define DOC_PLANES_STATUS_FAIL 0x01
  213. #define DOC_PLANES_STATUS_PLANE0_KO 0x02
  214. #define DOC_PLANES_STATUS_PLANE1_KO 0x04
  215. /*
  216. * DPS key management
  217. *
  218. * Each floor of docg3 has 2 protection areas: DPS0 and DPS1. These areas span
  219. * across block boundaries, and define whether these blocks can be read or
  220. * written.
  221. * The definition is dynamically stored in page 0 of blocks (2,3) for DPS0, and
  222. * page 0 of blocks (4,5) for DPS1.
  223. */
  224. #define DOC_LAYOUT_DPS_KEY_LENGTH 8
  225. /**
  226. * struct docg3_cascade - Cascade of 1 to 4 docg3 chips
  227. * @floors: floors (ie. one physical docg3 chip is one floor)
  228. * @base: IO space to access all chips in the cascade
  229. * @bch: the BCH correcting control structure
  230. * @lock: lock to protect docg3 IO space from concurrent accesses
  231. */
  232. struct docg3_cascade {
  233. struct mtd_info *floors[DOC_MAX_NBFLOORS];
  234. void __iomem *base;
  235. struct bch_control *bch;
  236. struct mutex lock;
  237. };
  238. /**
  239. * struct docg3 - DiskOnChip driver private data
  240. * @dev: the device currently under control
  241. * @cascade: the cascade this device belongs to
  242. * @device_id: number of the cascaded DoCG3 device (0, 1, 2 or 3)
  243. * @if_cfg: if true, reads are on 16bits, else reads are on 8bits
  244. * @reliable: if 0, docg3 in normal mode, if 1 docg3 in fast mode, if 2 in
  245. * reliable mode
  246. * Fast mode implies more errors than normal mode.
  247. * Reliable mode implies that page 2*n and 2*n+1 are clones.
  248. * @bbt: bad block table cache
  249. * @oob_write_ofs: offset of the MTD where this OOB should belong (ie. in next
  250. * page_write)
  251. * @oob_autoecc: if 1, use only bytes 0-7, 15, and fill the others with HW ECC
  252. * if 0, use all the 16 bytes.
  253. * @oob_write_buf: prepared OOB for next page_write
  254. */
  255. struct docg3 {
  256. struct device *dev;
  257. struct docg3_cascade *cascade;
  258. unsigned int device_id:4;
  259. unsigned int if_cfg:1;
  260. unsigned int reliable:2;
  261. int max_block;
  262. u8 *bbt;
  263. loff_t oob_write_ofs;
  264. int oob_autoecc;
  265. u8 oob_write_buf[DOC_LAYOUT_OOB_SIZE];
  266. };
  267. #define doc_err(fmt, arg...) dev_err(docg3->dev, (fmt), ## arg)
  268. #define doc_info(fmt, arg...) dev_info(docg3->dev, (fmt), ## arg)
  269. #define doc_dbg(fmt, arg...) dev_dbg(docg3->dev, (fmt), ## arg)
  270. #define doc_vdbg(fmt, arg...) dev_vdbg(docg3->dev, (fmt), ## arg)
  271. #endif
  272. /*
  273. * Trace events part
  274. */
  275. #undef TRACE_SYSTEM
  276. #define TRACE_SYSTEM docg3
  277. #if !defined(_MTD_DOCG3_TRACE) || defined(TRACE_HEADER_MULTI_READ)
  278. #define _MTD_DOCG3_TRACE
  279. #include <linux/tracepoint.h>
  280. TRACE_EVENT(docg3_io,
  281. TP_PROTO(int op, int width, u16 reg, int val),
  282. TP_ARGS(op, width, reg, val),
  283. TP_STRUCT__entry(
  284. __field(int, op)
  285. __field(unsigned char, width)
  286. __field(u16, reg)
  287. __field(int, val)),
  288. TP_fast_assign(
  289. __entry->op = op;
  290. __entry->width = width;
  291. __entry->reg = reg;
  292. __entry->val = val;),
  293. TP_printk("docg3: %s%02d reg=%04x, val=%04x",
  294. __entry->op ? "write" : "read", __entry->width,
  295. __entry->reg, __entry->val)
  296. );
  297. #endif
  298. /* This part must be outside protection */
  299. #undef TRACE_INCLUDE_PATH
  300. #undef TRACE_INCLUDE_FILE
  301. #define TRACE_INCLUDE_PATH .
  302. #define TRACE_INCLUDE_FILE docg3
  303. #include <trace/define_trace.h>