st_spi_fsm.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080
  1. /*
  2. * st_spi_fsm.c - ST Fast Sequence Mode (FSM) Serial Flash Controller
  3. *
  4. * Author: Angus Clark <angus.clark@st.com>
  5. *
  6. * Copyright (C) 2010-2014 STMicroelectronics Limited
  7. *
  8. * JEDEC probe based on drivers/mtd/devices/m25p80.c
  9. *
  10. * This code is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/regmap.h>
  18. #include <linux/platform_device.h>
  19. #include <linux/mfd/syscon.h>
  20. #include <linux/mtd/mtd.h>
  21. #include <linux/mtd/partitions.h>
  22. #include <linux/mtd/spi-nor.h>
  23. #include <linux/sched.h>
  24. #include <linux/delay.h>
  25. #include <linux/io.h>
  26. #include <linux/of.h>
  27. #include "serial_flash_cmds.h"
  28. /*
  29. * FSM SPI Controller Registers
  30. */
  31. #define SPI_CLOCKDIV 0x0010
  32. #define SPI_MODESELECT 0x0018
  33. #define SPI_CONFIGDATA 0x0020
  34. #define SPI_STA_MODE_CHANGE 0x0028
  35. #define SPI_FAST_SEQ_TRANSFER_SIZE 0x0100
  36. #define SPI_FAST_SEQ_ADD1 0x0104
  37. #define SPI_FAST_SEQ_ADD2 0x0108
  38. #define SPI_FAST_SEQ_ADD_CFG 0x010c
  39. #define SPI_FAST_SEQ_OPC1 0x0110
  40. #define SPI_FAST_SEQ_OPC2 0x0114
  41. #define SPI_FAST_SEQ_OPC3 0x0118
  42. #define SPI_FAST_SEQ_OPC4 0x011c
  43. #define SPI_FAST_SEQ_OPC5 0x0120
  44. #define SPI_MODE_BITS 0x0124
  45. #define SPI_DUMMY_BITS 0x0128
  46. #define SPI_FAST_SEQ_FLASH_STA_DATA 0x012c
  47. #define SPI_FAST_SEQ_1 0x0130
  48. #define SPI_FAST_SEQ_2 0x0134
  49. #define SPI_FAST_SEQ_3 0x0138
  50. #define SPI_FAST_SEQ_4 0x013c
  51. #define SPI_FAST_SEQ_CFG 0x0140
  52. #define SPI_FAST_SEQ_STA 0x0144
  53. #define SPI_QUAD_BOOT_SEQ_INIT_1 0x0148
  54. #define SPI_QUAD_BOOT_SEQ_INIT_2 0x014c
  55. #define SPI_QUAD_BOOT_READ_SEQ_1 0x0150
  56. #define SPI_QUAD_BOOT_READ_SEQ_2 0x0154
  57. #define SPI_PROGRAM_ERASE_TIME 0x0158
  58. #define SPI_MULT_PAGE_REPEAT_SEQ_1 0x015c
  59. #define SPI_MULT_PAGE_REPEAT_SEQ_2 0x0160
  60. #define SPI_STATUS_WR_TIME_REG 0x0164
  61. #define SPI_FAST_SEQ_DATA_REG 0x0300
  62. /*
  63. * Register: SPI_MODESELECT
  64. */
  65. #define SPI_MODESELECT_CONTIG 0x01
  66. #define SPI_MODESELECT_FASTREAD 0x02
  67. #define SPI_MODESELECT_DUALIO 0x04
  68. #define SPI_MODESELECT_FSM 0x08
  69. #define SPI_MODESELECT_QUADBOOT 0x10
  70. /*
  71. * Register: SPI_CONFIGDATA
  72. */
  73. #define SPI_CFG_DEVICE_ST 0x1
  74. #define SPI_CFG_DEVICE_ATMEL 0x4
  75. #define SPI_CFG_MIN_CS_HIGH(x) (((x) & 0xfff) << 4)
  76. #define SPI_CFG_CS_SETUPHOLD(x) (((x) & 0xff) << 16)
  77. #define SPI_CFG_DATA_HOLD(x) (((x) & 0xff) << 24)
  78. #define SPI_CFG_DEFAULT_MIN_CS_HIGH SPI_CFG_MIN_CS_HIGH(0x0AA)
  79. #define SPI_CFG_DEFAULT_CS_SETUPHOLD SPI_CFG_CS_SETUPHOLD(0xA0)
  80. #define SPI_CFG_DEFAULT_DATA_HOLD SPI_CFG_DATA_HOLD(0x00)
  81. /*
  82. * Register: SPI_FAST_SEQ_TRANSFER_SIZE
  83. */
  84. #define TRANSFER_SIZE(x) ((x) * 8)
  85. /*
  86. * Register: SPI_FAST_SEQ_ADD_CFG
  87. */
  88. #define ADR_CFG_CYCLES_ADD1(x) ((x) << 0)
  89. #define ADR_CFG_PADS_1_ADD1 (0x0 << 6)
  90. #define ADR_CFG_PADS_2_ADD1 (0x1 << 6)
  91. #define ADR_CFG_PADS_4_ADD1 (0x3 << 6)
  92. #define ADR_CFG_CSDEASSERT_ADD1 (1 << 8)
  93. #define ADR_CFG_CYCLES_ADD2(x) ((x) << (0+16))
  94. #define ADR_CFG_PADS_1_ADD2 (0x0 << (6+16))
  95. #define ADR_CFG_PADS_2_ADD2 (0x1 << (6+16))
  96. #define ADR_CFG_PADS_4_ADD2 (0x3 << (6+16))
  97. #define ADR_CFG_CSDEASSERT_ADD2 (1 << (8+16))
  98. /*
  99. * Register: SPI_FAST_SEQ_n
  100. */
  101. #define SEQ_OPC_OPCODE(x) ((x) << 0)
  102. #define SEQ_OPC_CYCLES(x) ((x) << 8)
  103. #define SEQ_OPC_PADS_1 (0x0 << 14)
  104. #define SEQ_OPC_PADS_2 (0x1 << 14)
  105. #define SEQ_OPC_PADS_4 (0x3 << 14)
  106. #define SEQ_OPC_CSDEASSERT (1 << 16)
  107. /*
  108. * Register: SPI_FAST_SEQ_CFG
  109. */
  110. #define SEQ_CFG_STARTSEQ (1 << 0)
  111. #define SEQ_CFG_SWRESET (1 << 5)
  112. #define SEQ_CFG_CSDEASSERT (1 << 6)
  113. #define SEQ_CFG_READNOTWRITE (1 << 7)
  114. #define SEQ_CFG_ERASE (1 << 8)
  115. #define SEQ_CFG_PADS_1 (0x0 << 16)
  116. #define SEQ_CFG_PADS_2 (0x1 << 16)
  117. #define SEQ_CFG_PADS_4 (0x3 << 16)
  118. /*
  119. * Register: SPI_MODE_BITS
  120. */
  121. #define MODE_DATA(x) (x & 0xff)
  122. #define MODE_CYCLES(x) ((x & 0x3f) << 16)
  123. #define MODE_PADS_1 (0x0 << 22)
  124. #define MODE_PADS_2 (0x1 << 22)
  125. #define MODE_PADS_4 (0x3 << 22)
  126. #define DUMMY_CSDEASSERT (1 << 24)
  127. /*
  128. * Register: SPI_DUMMY_BITS
  129. */
  130. #define DUMMY_CYCLES(x) ((x & 0x3f) << 16)
  131. #define DUMMY_PADS_1 (0x0 << 22)
  132. #define DUMMY_PADS_2 (0x1 << 22)
  133. #define DUMMY_PADS_4 (0x3 << 22)
  134. #define DUMMY_CSDEASSERT (1 << 24)
  135. /*
  136. * Register: SPI_FAST_SEQ_FLASH_STA_DATA
  137. */
  138. #define STA_DATA_BYTE1(x) ((x & 0xff) << 0)
  139. #define STA_DATA_BYTE2(x) ((x & 0xff) << 8)
  140. #define STA_PADS_1 (0x0 << 16)
  141. #define STA_PADS_2 (0x1 << 16)
  142. #define STA_PADS_4 (0x3 << 16)
  143. #define STA_CSDEASSERT (0x1 << 20)
  144. #define STA_RDNOTWR (0x1 << 21)
  145. /*
  146. * FSM SPI Instruction Opcodes
  147. */
  148. #define STFSM_OPC_CMD 0x1
  149. #define STFSM_OPC_ADD 0x2
  150. #define STFSM_OPC_STA 0x3
  151. #define STFSM_OPC_MODE 0x4
  152. #define STFSM_OPC_DUMMY 0x5
  153. #define STFSM_OPC_DATA 0x6
  154. #define STFSM_OPC_WAIT 0x7
  155. #define STFSM_OPC_JUMP 0x8
  156. #define STFSM_OPC_GOTO 0x9
  157. #define STFSM_OPC_STOP 0xF
  158. /*
  159. * FSM SPI Instructions (== opcode + operand).
  160. */
  161. #define STFSM_INSTR(cmd, op) ((cmd) | ((op) << 4))
  162. #define STFSM_INST_CMD1 STFSM_INSTR(STFSM_OPC_CMD, 1)
  163. #define STFSM_INST_CMD2 STFSM_INSTR(STFSM_OPC_CMD, 2)
  164. #define STFSM_INST_CMD3 STFSM_INSTR(STFSM_OPC_CMD, 3)
  165. #define STFSM_INST_CMD4 STFSM_INSTR(STFSM_OPC_CMD, 4)
  166. #define STFSM_INST_CMD5 STFSM_INSTR(STFSM_OPC_CMD, 5)
  167. #define STFSM_INST_ADD1 STFSM_INSTR(STFSM_OPC_ADD, 1)
  168. #define STFSM_INST_ADD2 STFSM_INSTR(STFSM_OPC_ADD, 2)
  169. #define STFSM_INST_DATA_WRITE STFSM_INSTR(STFSM_OPC_DATA, 1)
  170. #define STFSM_INST_DATA_READ STFSM_INSTR(STFSM_OPC_DATA, 2)
  171. #define STFSM_INST_STA_RD1 STFSM_INSTR(STFSM_OPC_STA, 0x1)
  172. #define STFSM_INST_STA_WR1 STFSM_INSTR(STFSM_OPC_STA, 0x1)
  173. #define STFSM_INST_STA_RD2 STFSM_INSTR(STFSM_OPC_STA, 0x2)
  174. #define STFSM_INST_STA_WR1_2 STFSM_INSTR(STFSM_OPC_STA, 0x3)
  175. #define STFSM_INST_MODE STFSM_INSTR(STFSM_OPC_MODE, 0)
  176. #define STFSM_INST_DUMMY STFSM_INSTR(STFSM_OPC_DUMMY, 0)
  177. #define STFSM_INST_WAIT STFSM_INSTR(STFSM_OPC_WAIT, 0)
  178. #define STFSM_INST_STOP STFSM_INSTR(STFSM_OPC_STOP, 0)
  179. #define STFSM_DEFAULT_EMI_FREQ 100000000UL /* 100 MHz */
  180. #define STFSM_DEFAULT_WR_TIME (STFSM_DEFAULT_EMI_FREQ * (15/1000)) /* 15ms */
  181. #define STFSM_FLASH_SAFE_FREQ 10000000UL /* 10 MHz */
  182. #define STFSM_MAX_WAIT_SEQ_MS 1000 /* FSM execution time */
  183. /* S25FLxxxS commands */
  184. #define S25FL_CMD_WRITE4_1_1_4 0x34
  185. #define S25FL_CMD_SE4 0xdc
  186. #define S25FL_CMD_CLSR 0x30
  187. #define S25FL_CMD_DYBWR 0xe1
  188. #define S25FL_CMD_DYBRD 0xe0
  189. #define S25FL_CMD_WRITE4 0x12 /* Note, opcode clashes with
  190. * 'SPINOR_OP_WRITE_1_4_4'
  191. * as found on N25Qxxx devices! */
  192. /* Status register */
  193. #define FLASH_STATUS_BUSY 0x01
  194. #define FLASH_STATUS_WEL 0x02
  195. #define FLASH_STATUS_BP0 0x04
  196. #define FLASH_STATUS_BP1 0x08
  197. #define FLASH_STATUS_BP2 0x10
  198. #define FLASH_STATUS_SRWP0 0x80
  199. #define FLASH_STATUS_TIMEOUT 0xff
  200. /* S25FL Error Flags */
  201. #define S25FL_STATUS_E_ERR 0x20
  202. #define S25FL_STATUS_P_ERR 0x40
  203. #define N25Q_CMD_WRVCR 0x81
  204. #define N25Q_CMD_RDVCR 0x85
  205. #define N25Q_CMD_RDVECR 0x65
  206. #define N25Q_CMD_RDNVCR 0xb5
  207. #define N25Q_CMD_WRNVCR 0xb1
  208. #define FLASH_PAGESIZE 256 /* In Bytes */
  209. #define FLASH_PAGESIZE_32 (FLASH_PAGESIZE / 4) /* In uint32_t */
  210. #define FLASH_MAX_BUSY_WAIT (300 * HZ) /* Maximum 'CHIPERASE' time */
  211. /*
  212. * Flags to tweak operation of default read/write/erase routines
  213. */
  214. #define CFG_READ_TOGGLE_32BIT_ADDR 0x00000001
  215. #define CFG_WRITE_TOGGLE_32BIT_ADDR 0x00000002
  216. #define CFG_ERASESEC_TOGGLE_32BIT_ADDR 0x00000008
  217. #define CFG_S25FL_CHECK_ERROR_FLAGS 0x00000010
  218. struct stfsm_seq {
  219. uint32_t data_size;
  220. uint32_t addr1;
  221. uint32_t addr2;
  222. uint32_t addr_cfg;
  223. uint32_t seq_opc[5];
  224. uint32_t mode;
  225. uint32_t dummy;
  226. uint32_t status;
  227. uint8_t seq[16];
  228. uint32_t seq_cfg;
  229. } __packed __aligned(4);
  230. struct stfsm {
  231. struct device *dev;
  232. void __iomem *base;
  233. struct resource *region;
  234. struct mtd_info mtd;
  235. struct mutex lock;
  236. struct flash_info *info;
  237. uint32_t configuration;
  238. uint32_t fifo_dir_delay;
  239. bool booted_from_spi;
  240. bool reset_signal;
  241. bool reset_por;
  242. struct stfsm_seq stfsm_seq_read;
  243. struct stfsm_seq stfsm_seq_write;
  244. struct stfsm_seq stfsm_seq_en_32bit_addr;
  245. };
  246. /* Parameters to configure a READ or WRITE FSM sequence */
  247. struct seq_rw_config {
  248. uint32_t flags; /* flags to support config */
  249. uint8_t cmd; /* FLASH command */
  250. int write; /* Write Sequence */
  251. uint8_t addr_pads; /* No. of addr pads (MODE & DUMMY) */
  252. uint8_t data_pads; /* No. of data pads */
  253. uint8_t mode_data; /* MODE data */
  254. uint8_t mode_cycles; /* No. of MODE cycles */
  255. uint8_t dummy_cycles; /* No. of DUMMY cycles */
  256. };
  257. /* SPI Flash Device Table */
  258. struct flash_info {
  259. char *name;
  260. /*
  261. * JEDEC id zero means "no ID" (most older chips); otherwise it has
  262. * a high byte of zero plus three data bytes: the manufacturer id,
  263. * then a two byte device id.
  264. */
  265. u32 jedec_id;
  266. u16 ext_id;
  267. /*
  268. * The size listed here is what works with SPINOR_OP_SE, which isn't
  269. * necessarily called a "sector" by the vendor.
  270. */
  271. unsigned sector_size;
  272. u16 n_sectors;
  273. u32 flags;
  274. /*
  275. * Note, where FAST_READ is supported, freq_max specifies the
  276. * FAST_READ frequency, not the READ frequency.
  277. */
  278. u32 max_freq;
  279. int (*config)(struct stfsm *);
  280. };
  281. static int stfsm_n25q_config(struct stfsm *fsm);
  282. static int stfsm_mx25_config(struct stfsm *fsm);
  283. static int stfsm_s25fl_config(struct stfsm *fsm);
  284. static int stfsm_w25q_config(struct stfsm *fsm);
  285. static struct flash_info flash_types[] = {
  286. /*
  287. * ST Microelectronics/Numonyx --
  288. * (newer production versions may have feature updates
  289. * (eg faster operating frequency)
  290. */
  291. #define M25P_FLAG (FLASH_FLAG_READ_WRITE | FLASH_FLAG_READ_FAST)
  292. { "m25p40", 0x202013, 0, 64 * 1024, 8, M25P_FLAG, 25, NULL },
  293. { "m25p80", 0x202014, 0, 64 * 1024, 16, M25P_FLAG, 25, NULL },
  294. { "m25p16", 0x202015, 0, 64 * 1024, 32, M25P_FLAG, 25, NULL },
  295. { "m25p32", 0x202016, 0, 64 * 1024, 64, M25P_FLAG, 50, NULL },
  296. { "m25p64", 0x202017, 0, 64 * 1024, 128, M25P_FLAG, 50, NULL },
  297. { "m25p128", 0x202018, 0, 256 * 1024, 64, M25P_FLAG, 50, NULL },
  298. #define M25PX_FLAG (FLASH_FLAG_READ_WRITE | \
  299. FLASH_FLAG_READ_FAST | \
  300. FLASH_FLAG_READ_1_1_2 | \
  301. FLASH_FLAG_WRITE_1_1_2)
  302. { "m25px32", 0x207116, 0, 64 * 1024, 64, M25PX_FLAG, 75, NULL },
  303. { "m25px64", 0x207117, 0, 64 * 1024, 128, M25PX_FLAG, 75, NULL },
  304. /* Macronix MX25xxx
  305. * - Support for 'FLASH_FLAG_WRITE_1_4_4' is omitted for devices
  306. * where operating frequency must be reduced.
  307. */
  308. #define MX25_FLAG (FLASH_FLAG_READ_WRITE | \
  309. FLASH_FLAG_READ_FAST | \
  310. FLASH_FLAG_READ_1_1_2 | \
  311. FLASH_FLAG_READ_1_2_2 | \
  312. FLASH_FLAG_READ_1_1_4 | \
  313. FLASH_FLAG_SE_4K | \
  314. FLASH_FLAG_SE_32K)
  315. { "mx25l3255e", 0xc29e16, 0, 64 * 1024, 64,
  316. (MX25_FLAG | FLASH_FLAG_WRITE_1_4_4), 86,
  317. stfsm_mx25_config},
  318. { "mx25l25635e", 0xc22019, 0, 64*1024, 512,
  319. (MX25_FLAG | FLASH_FLAG_32BIT_ADDR | FLASH_FLAG_RESET), 70,
  320. stfsm_mx25_config },
  321. { "mx25l25655e", 0xc22619, 0, 64*1024, 512,
  322. (MX25_FLAG | FLASH_FLAG_32BIT_ADDR | FLASH_FLAG_RESET), 70,
  323. stfsm_mx25_config},
  324. #define N25Q_FLAG (FLASH_FLAG_READ_WRITE | \
  325. FLASH_FLAG_READ_FAST | \
  326. FLASH_FLAG_READ_1_1_2 | \
  327. FLASH_FLAG_READ_1_2_2 | \
  328. FLASH_FLAG_READ_1_1_4 | \
  329. FLASH_FLAG_READ_1_4_4 | \
  330. FLASH_FLAG_WRITE_1_1_2 | \
  331. FLASH_FLAG_WRITE_1_2_2 | \
  332. FLASH_FLAG_WRITE_1_1_4 | \
  333. FLASH_FLAG_WRITE_1_4_4)
  334. { "n25q128", 0x20ba18, 0, 64 * 1024, 256, N25Q_FLAG, 108,
  335. stfsm_n25q_config },
  336. { "n25q256", 0x20ba19, 0, 64 * 1024, 512,
  337. N25Q_FLAG | FLASH_FLAG_32BIT_ADDR, 108, stfsm_n25q_config },
  338. /*
  339. * Spansion S25FLxxxP
  340. * - 256KiB and 64KiB sector variants (identified by ext. JEDEC)
  341. */
  342. #define S25FLXXXP_FLAG (FLASH_FLAG_READ_WRITE | \
  343. FLASH_FLAG_READ_1_1_2 | \
  344. FLASH_FLAG_READ_1_2_2 | \
  345. FLASH_FLAG_READ_1_1_4 | \
  346. FLASH_FLAG_READ_1_4_4 | \
  347. FLASH_FLAG_WRITE_1_1_4 | \
  348. FLASH_FLAG_READ_FAST)
  349. { "s25fl032p", 0x010215, 0x4d00, 64 * 1024, 64, S25FLXXXP_FLAG, 80,
  350. stfsm_s25fl_config},
  351. { "s25fl129p0", 0x012018, 0x4d00, 256 * 1024, 64, S25FLXXXP_FLAG, 80,
  352. stfsm_s25fl_config },
  353. { "s25fl129p1", 0x012018, 0x4d01, 64 * 1024, 256, S25FLXXXP_FLAG, 80,
  354. stfsm_s25fl_config },
  355. /*
  356. * Spansion S25FLxxxS
  357. * - 256KiB and 64KiB sector variants (identified by ext. JEDEC)
  358. * - RESET# signal supported by die but not bristled out on all
  359. * package types. The package type is a function of board design,
  360. * so this information is captured in the board's flags.
  361. * - Supports 'DYB' sector protection. Depending on variant, sectors
  362. * may default to locked state on power-on.
  363. */
  364. #define S25FLXXXS_FLAG (S25FLXXXP_FLAG | \
  365. FLASH_FLAG_RESET | \
  366. FLASH_FLAG_DYB_LOCKING)
  367. { "s25fl128s0", 0x012018, 0x0300, 256 * 1024, 64, S25FLXXXS_FLAG, 80,
  368. stfsm_s25fl_config },
  369. { "s25fl128s1", 0x012018, 0x0301, 64 * 1024, 256, S25FLXXXS_FLAG, 80,
  370. stfsm_s25fl_config },
  371. { "s25fl256s0", 0x010219, 0x4d00, 256 * 1024, 128,
  372. S25FLXXXS_FLAG | FLASH_FLAG_32BIT_ADDR, 80, stfsm_s25fl_config },
  373. { "s25fl256s1", 0x010219, 0x4d01, 64 * 1024, 512,
  374. S25FLXXXS_FLAG | FLASH_FLAG_32BIT_ADDR, 80, stfsm_s25fl_config },
  375. /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
  376. #define W25X_FLAG (FLASH_FLAG_READ_WRITE | \
  377. FLASH_FLAG_READ_FAST | \
  378. FLASH_FLAG_READ_1_1_2 | \
  379. FLASH_FLAG_WRITE_1_1_2)
  380. { "w25x40", 0xef3013, 0, 64 * 1024, 8, W25X_FLAG, 75, NULL },
  381. { "w25x80", 0xef3014, 0, 64 * 1024, 16, W25X_FLAG, 75, NULL },
  382. { "w25x16", 0xef3015, 0, 64 * 1024, 32, W25X_FLAG, 75, NULL },
  383. { "w25x32", 0xef3016, 0, 64 * 1024, 64, W25X_FLAG, 75, NULL },
  384. { "w25x64", 0xef3017, 0, 64 * 1024, 128, W25X_FLAG, 75, NULL },
  385. /* Winbond -- w25q "blocks" are 64K, "sectors" are 4KiB */
  386. #define W25Q_FLAG (FLASH_FLAG_READ_WRITE | \
  387. FLASH_FLAG_READ_FAST | \
  388. FLASH_FLAG_READ_1_1_2 | \
  389. FLASH_FLAG_READ_1_2_2 | \
  390. FLASH_FLAG_READ_1_1_4 | \
  391. FLASH_FLAG_READ_1_4_4 | \
  392. FLASH_FLAG_WRITE_1_1_4)
  393. { "w25q80", 0xef4014, 0, 64 * 1024, 16, W25Q_FLAG, 80,
  394. stfsm_w25q_config },
  395. { "w25q16", 0xef4015, 0, 64 * 1024, 32, W25Q_FLAG, 80,
  396. stfsm_w25q_config },
  397. { "w25q32", 0xef4016, 0, 64 * 1024, 64, W25Q_FLAG, 80,
  398. stfsm_w25q_config },
  399. { "w25q64", 0xef4017, 0, 64 * 1024, 128, W25Q_FLAG, 80,
  400. stfsm_w25q_config },
  401. /* Sentinel */
  402. { NULL, 0x000000, 0, 0, 0, 0, 0, NULL },
  403. };
  404. /*
  405. * FSM message sequence configurations:
  406. *
  407. * All configs are presented in order of preference
  408. */
  409. /* Default READ configurations, in order of preference */
  410. static struct seq_rw_config default_read_configs[] = {
  411. {FLASH_FLAG_READ_1_4_4, SPINOR_OP_READ_1_4_4, 0, 4, 4, 0x00, 2, 4},
  412. {FLASH_FLAG_READ_1_1_4, SPINOR_OP_READ_1_1_4, 0, 1, 4, 0x00, 4, 0},
  413. {FLASH_FLAG_READ_1_2_2, SPINOR_OP_READ_1_2_2, 0, 2, 2, 0x00, 4, 0},
  414. {FLASH_FLAG_READ_1_1_2, SPINOR_OP_READ_1_1_2, 0, 1, 2, 0x00, 0, 8},
  415. {FLASH_FLAG_READ_FAST, SPINOR_OP_READ_FAST, 0, 1, 1, 0x00, 0, 8},
  416. {FLASH_FLAG_READ_WRITE, SPINOR_OP_READ, 0, 1, 1, 0x00, 0, 0},
  417. {0x00, 0, 0, 0, 0, 0x00, 0, 0},
  418. };
  419. /* Default WRITE configurations */
  420. static struct seq_rw_config default_write_configs[] = {
  421. {FLASH_FLAG_WRITE_1_4_4, SPINOR_OP_WRITE_1_4_4, 1, 4, 4, 0x00, 0, 0},
  422. {FLASH_FLAG_WRITE_1_1_4, SPINOR_OP_WRITE_1_1_4, 1, 1, 4, 0x00, 0, 0},
  423. {FLASH_FLAG_WRITE_1_2_2, SPINOR_OP_WRITE_1_2_2, 1, 2, 2, 0x00, 0, 0},
  424. {FLASH_FLAG_WRITE_1_1_2, SPINOR_OP_WRITE_1_1_2, 1, 1, 2, 0x00, 0, 0},
  425. {FLASH_FLAG_READ_WRITE, SPINOR_OP_WRITE, 1, 1, 1, 0x00, 0, 0},
  426. {0x00, 0, 0, 0, 0, 0x00, 0, 0},
  427. };
  428. /*
  429. * [N25Qxxx] Configuration
  430. */
  431. #define N25Q_VCR_DUMMY_CYCLES(x) (((x) & 0xf) << 4)
  432. #define N25Q_VCR_XIP_DISABLED ((uint8_t)0x1 << 3)
  433. #define N25Q_VCR_WRAP_CONT 0x3
  434. /* N25Q 3-byte Address READ configurations
  435. * - 'FAST' variants configured for 8 dummy cycles.
  436. *
  437. * Note, the number of dummy cycles used for 'FAST' READ operations is
  438. * configurable and would normally be tuned according to the READ command and
  439. * operating frequency. However, this applies universally to all 'FAST' READ
  440. * commands, including those used by the SPIBoot controller, and remains in
  441. * force until the device is power-cycled. Since the SPIBoot controller is
  442. * hard-wired to use 8 dummy cycles, we must configure the device to also use 8
  443. * cycles.
  444. */
  445. static struct seq_rw_config n25q_read3_configs[] = {
  446. {FLASH_FLAG_READ_1_4_4, SPINOR_OP_READ_1_4_4, 0, 4, 4, 0x00, 0, 8},
  447. {FLASH_FLAG_READ_1_1_4, SPINOR_OP_READ_1_1_4, 0, 1, 4, 0x00, 0, 8},
  448. {FLASH_FLAG_READ_1_2_2, SPINOR_OP_READ_1_2_2, 0, 2, 2, 0x00, 0, 8},
  449. {FLASH_FLAG_READ_1_1_2, SPINOR_OP_READ_1_1_2, 0, 1, 2, 0x00, 0, 8},
  450. {FLASH_FLAG_READ_FAST, SPINOR_OP_READ_FAST, 0, 1, 1, 0x00, 0, 8},
  451. {FLASH_FLAG_READ_WRITE, SPINOR_OP_READ, 0, 1, 1, 0x00, 0, 0},
  452. {0x00, 0, 0, 0, 0, 0x00, 0, 0},
  453. };
  454. /* N25Q 4-byte Address READ configurations
  455. * - use special 4-byte address READ commands (reduces overheads, and
  456. * reduces risk of hitting watchdog reset issues).
  457. * - 'FAST' variants configured for 8 dummy cycles (see note above.)
  458. */
  459. static struct seq_rw_config n25q_read4_configs[] = {
  460. {FLASH_FLAG_READ_1_4_4, SPINOR_OP_READ4_1_4_4, 0, 4, 4, 0x00, 0, 8},
  461. {FLASH_FLAG_READ_1_1_4, SPINOR_OP_READ4_1_1_4, 0, 1, 4, 0x00, 0, 8},
  462. {FLASH_FLAG_READ_1_2_2, SPINOR_OP_READ4_1_2_2, 0, 2, 2, 0x00, 0, 8},
  463. {FLASH_FLAG_READ_1_1_2, SPINOR_OP_READ4_1_1_2, 0, 1, 2, 0x00, 0, 8},
  464. {FLASH_FLAG_READ_FAST, SPINOR_OP_READ4_FAST, 0, 1, 1, 0x00, 0, 8},
  465. {FLASH_FLAG_READ_WRITE, SPINOR_OP_READ4, 0, 1, 1, 0x00, 0, 0},
  466. {0x00, 0, 0, 0, 0, 0x00, 0, 0},
  467. };
  468. /*
  469. * [MX25xxx] Configuration
  470. */
  471. #define MX25_STATUS_QE (0x1 << 6)
  472. static int stfsm_mx25_en_32bit_addr_seq(struct stfsm_seq *seq)
  473. {
  474. seq->seq_opc[0] = (SEQ_OPC_PADS_1 |
  475. SEQ_OPC_CYCLES(8) |
  476. SEQ_OPC_OPCODE(SPINOR_OP_EN4B) |
  477. SEQ_OPC_CSDEASSERT);
  478. seq->seq[0] = STFSM_INST_CMD1;
  479. seq->seq[1] = STFSM_INST_WAIT;
  480. seq->seq[2] = STFSM_INST_STOP;
  481. seq->seq_cfg = (SEQ_CFG_PADS_1 |
  482. SEQ_CFG_ERASE |
  483. SEQ_CFG_READNOTWRITE |
  484. SEQ_CFG_CSDEASSERT |
  485. SEQ_CFG_STARTSEQ);
  486. return 0;
  487. }
  488. /*
  489. * [S25FLxxx] Configuration
  490. */
  491. #define STFSM_S25FL_CONFIG_QE (0x1 << 1)
  492. /*
  493. * S25FLxxxS devices provide three ways of supporting 32-bit addressing: Bank
  494. * Register, Extended Address Modes, and a 32-bit address command set. The
  495. * 32-bit address command set is used here, since it avoids any problems with
  496. * entering a state that is incompatible with the SPIBoot Controller.
  497. */
  498. static struct seq_rw_config stfsm_s25fl_read4_configs[] = {
  499. {FLASH_FLAG_READ_1_4_4, SPINOR_OP_READ4_1_4_4, 0, 4, 4, 0x00, 2, 4},
  500. {FLASH_FLAG_READ_1_1_4, SPINOR_OP_READ4_1_1_4, 0, 1, 4, 0x00, 0, 8},
  501. {FLASH_FLAG_READ_1_2_2, SPINOR_OP_READ4_1_2_2, 0, 2, 2, 0x00, 4, 0},
  502. {FLASH_FLAG_READ_1_1_2, SPINOR_OP_READ4_1_1_2, 0, 1, 2, 0x00, 0, 8},
  503. {FLASH_FLAG_READ_FAST, SPINOR_OP_READ4_FAST, 0, 1, 1, 0x00, 0, 8},
  504. {FLASH_FLAG_READ_WRITE, SPINOR_OP_READ4, 0, 1, 1, 0x00, 0, 0},
  505. {0x00, 0, 0, 0, 0, 0x00, 0, 0},
  506. };
  507. static struct seq_rw_config stfsm_s25fl_write4_configs[] = {
  508. {FLASH_FLAG_WRITE_1_1_4, S25FL_CMD_WRITE4_1_1_4, 1, 1, 4, 0x00, 0, 0},
  509. {FLASH_FLAG_READ_WRITE, S25FL_CMD_WRITE4, 1, 1, 1, 0x00, 0, 0},
  510. {0x00, 0, 0, 0, 0, 0x00, 0, 0},
  511. };
  512. /*
  513. * [W25Qxxx] Configuration
  514. */
  515. #define W25Q_STATUS_QE (0x1 << 1)
  516. static struct stfsm_seq stfsm_seq_read_jedec = {
  517. .data_size = TRANSFER_SIZE(8),
  518. .seq_opc[0] = (SEQ_OPC_PADS_1 |
  519. SEQ_OPC_CYCLES(8) |
  520. SEQ_OPC_OPCODE(SPINOR_OP_RDID)),
  521. .seq = {
  522. STFSM_INST_CMD1,
  523. STFSM_INST_DATA_READ,
  524. STFSM_INST_STOP,
  525. },
  526. .seq_cfg = (SEQ_CFG_PADS_1 |
  527. SEQ_CFG_READNOTWRITE |
  528. SEQ_CFG_CSDEASSERT |
  529. SEQ_CFG_STARTSEQ),
  530. };
  531. static struct stfsm_seq stfsm_seq_read_status_fifo = {
  532. .data_size = TRANSFER_SIZE(4),
  533. .seq_opc[0] = (SEQ_OPC_PADS_1 |
  534. SEQ_OPC_CYCLES(8) |
  535. SEQ_OPC_OPCODE(SPINOR_OP_RDSR)),
  536. .seq = {
  537. STFSM_INST_CMD1,
  538. STFSM_INST_DATA_READ,
  539. STFSM_INST_STOP,
  540. },
  541. .seq_cfg = (SEQ_CFG_PADS_1 |
  542. SEQ_CFG_READNOTWRITE |
  543. SEQ_CFG_CSDEASSERT |
  544. SEQ_CFG_STARTSEQ),
  545. };
  546. static struct stfsm_seq stfsm_seq_erase_sector = {
  547. /* 'addr_cfg' configured during initialisation */
  548. .seq_opc = {
  549. (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
  550. SEQ_OPC_OPCODE(SPINOR_OP_WREN) | SEQ_OPC_CSDEASSERT),
  551. (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
  552. SEQ_OPC_OPCODE(SPINOR_OP_SE)),
  553. },
  554. .seq = {
  555. STFSM_INST_CMD1,
  556. STFSM_INST_CMD2,
  557. STFSM_INST_ADD1,
  558. STFSM_INST_ADD2,
  559. STFSM_INST_STOP,
  560. },
  561. .seq_cfg = (SEQ_CFG_PADS_1 |
  562. SEQ_CFG_READNOTWRITE |
  563. SEQ_CFG_CSDEASSERT |
  564. SEQ_CFG_STARTSEQ),
  565. };
  566. static struct stfsm_seq stfsm_seq_erase_chip = {
  567. .seq_opc = {
  568. (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
  569. SEQ_OPC_OPCODE(SPINOR_OP_WREN) | SEQ_OPC_CSDEASSERT),
  570. (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
  571. SEQ_OPC_OPCODE(SPINOR_OP_CHIP_ERASE) | SEQ_OPC_CSDEASSERT),
  572. },
  573. .seq = {
  574. STFSM_INST_CMD1,
  575. STFSM_INST_CMD2,
  576. STFSM_INST_WAIT,
  577. STFSM_INST_STOP,
  578. },
  579. .seq_cfg = (SEQ_CFG_PADS_1 |
  580. SEQ_CFG_ERASE |
  581. SEQ_CFG_READNOTWRITE |
  582. SEQ_CFG_CSDEASSERT |
  583. SEQ_CFG_STARTSEQ),
  584. };
  585. static struct stfsm_seq stfsm_seq_write_status = {
  586. .seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
  587. SEQ_OPC_OPCODE(SPINOR_OP_WREN) | SEQ_OPC_CSDEASSERT),
  588. .seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
  589. SEQ_OPC_OPCODE(SPINOR_OP_WRSR)),
  590. .seq = {
  591. STFSM_INST_CMD1,
  592. STFSM_INST_CMD2,
  593. STFSM_INST_STA_WR1,
  594. STFSM_INST_STOP,
  595. },
  596. .seq_cfg = (SEQ_CFG_PADS_1 |
  597. SEQ_CFG_READNOTWRITE |
  598. SEQ_CFG_CSDEASSERT |
  599. SEQ_CFG_STARTSEQ),
  600. };
  601. static int stfsm_n25q_en_32bit_addr_seq(struct stfsm_seq *seq)
  602. {
  603. seq->seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
  604. SEQ_OPC_OPCODE(SPINOR_OP_EN4B));
  605. seq->seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
  606. SEQ_OPC_OPCODE(SPINOR_OP_WREN) |
  607. SEQ_OPC_CSDEASSERT);
  608. seq->seq[0] = STFSM_INST_CMD2;
  609. seq->seq[1] = STFSM_INST_CMD1;
  610. seq->seq[2] = STFSM_INST_WAIT;
  611. seq->seq[3] = STFSM_INST_STOP;
  612. seq->seq_cfg = (SEQ_CFG_PADS_1 |
  613. SEQ_CFG_ERASE |
  614. SEQ_CFG_READNOTWRITE |
  615. SEQ_CFG_CSDEASSERT |
  616. SEQ_CFG_STARTSEQ);
  617. return 0;
  618. }
  619. static inline int stfsm_is_idle(struct stfsm *fsm)
  620. {
  621. return readl(fsm->base + SPI_FAST_SEQ_STA) & 0x10;
  622. }
  623. static inline uint32_t stfsm_fifo_available(struct stfsm *fsm)
  624. {
  625. return (readl(fsm->base + SPI_FAST_SEQ_STA) >> 5) & 0x7f;
  626. }
  627. static void stfsm_clear_fifo(struct stfsm *fsm)
  628. {
  629. uint32_t avail;
  630. for (;;) {
  631. avail = stfsm_fifo_available(fsm);
  632. if (!avail)
  633. break;
  634. while (avail) {
  635. readl(fsm->base + SPI_FAST_SEQ_DATA_REG);
  636. avail--;
  637. }
  638. }
  639. }
  640. static inline void stfsm_load_seq(struct stfsm *fsm,
  641. const struct stfsm_seq *seq)
  642. {
  643. void __iomem *dst = fsm->base + SPI_FAST_SEQ_TRANSFER_SIZE;
  644. const uint32_t *src = (const uint32_t *)seq;
  645. int words = sizeof(*seq) / sizeof(*src);
  646. BUG_ON(!stfsm_is_idle(fsm));
  647. while (words--) {
  648. writel(*src, dst);
  649. src++;
  650. dst += 4;
  651. }
  652. }
  653. static void stfsm_wait_seq(struct stfsm *fsm)
  654. {
  655. unsigned long deadline;
  656. int timeout = 0;
  657. deadline = jiffies + msecs_to_jiffies(STFSM_MAX_WAIT_SEQ_MS);
  658. while (!timeout) {
  659. if (time_after_eq(jiffies, deadline))
  660. timeout = 1;
  661. if (stfsm_is_idle(fsm))
  662. return;
  663. cond_resched();
  664. }
  665. dev_err(fsm->dev, "timeout on sequence completion\n");
  666. }
  667. static void stfsm_read_fifo(struct stfsm *fsm, uint32_t *buf, uint32_t size)
  668. {
  669. uint32_t remaining = size >> 2;
  670. uint32_t avail;
  671. uint32_t words;
  672. dev_dbg(fsm->dev, "Reading %d bytes from FIFO\n", size);
  673. BUG_ON((((uintptr_t)buf) & 0x3) || (size & 0x3));
  674. while (remaining) {
  675. for (;;) {
  676. avail = stfsm_fifo_available(fsm);
  677. if (avail)
  678. break;
  679. udelay(1);
  680. }
  681. words = min(avail, remaining);
  682. remaining -= words;
  683. readsl(fsm->base + SPI_FAST_SEQ_DATA_REG, buf, words);
  684. buf += words;
  685. }
  686. }
  687. static int stfsm_write_fifo(struct stfsm *fsm, const uint32_t *buf,
  688. uint32_t size)
  689. {
  690. uint32_t words = size >> 2;
  691. dev_dbg(fsm->dev, "writing %d bytes to FIFO\n", size);
  692. BUG_ON((((uintptr_t)buf) & 0x3) || (size & 0x3));
  693. writesl(fsm->base + SPI_FAST_SEQ_DATA_REG, buf, words);
  694. return size;
  695. }
  696. static int stfsm_enter_32bit_addr(struct stfsm *fsm, int enter)
  697. {
  698. struct stfsm_seq *seq = &fsm->stfsm_seq_en_32bit_addr;
  699. uint32_t cmd = enter ? SPINOR_OP_EN4B : SPINOR_OP_EX4B;
  700. seq->seq_opc[0] = (SEQ_OPC_PADS_1 |
  701. SEQ_OPC_CYCLES(8) |
  702. SEQ_OPC_OPCODE(cmd) |
  703. SEQ_OPC_CSDEASSERT);
  704. stfsm_load_seq(fsm, seq);
  705. stfsm_wait_seq(fsm);
  706. return 0;
  707. }
  708. static uint8_t stfsm_wait_busy(struct stfsm *fsm)
  709. {
  710. struct stfsm_seq *seq = &stfsm_seq_read_status_fifo;
  711. unsigned long deadline;
  712. uint32_t status;
  713. int timeout = 0;
  714. /* Use RDRS1 */
  715. seq->seq_opc[0] = (SEQ_OPC_PADS_1 |
  716. SEQ_OPC_CYCLES(8) |
  717. SEQ_OPC_OPCODE(SPINOR_OP_RDSR));
  718. /* Load read_status sequence */
  719. stfsm_load_seq(fsm, seq);
  720. /*
  721. * Repeat until busy bit is deasserted, or timeout, or error (S25FLxxxS)
  722. */
  723. deadline = jiffies + FLASH_MAX_BUSY_WAIT;
  724. while (!timeout) {
  725. if (time_after_eq(jiffies, deadline))
  726. timeout = 1;
  727. stfsm_wait_seq(fsm);
  728. stfsm_read_fifo(fsm, &status, 4);
  729. if ((status & FLASH_STATUS_BUSY) == 0)
  730. return 0;
  731. if ((fsm->configuration & CFG_S25FL_CHECK_ERROR_FLAGS) &&
  732. ((status & S25FL_STATUS_P_ERR) ||
  733. (status & S25FL_STATUS_E_ERR)))
  734. return (uint8_t)(status & 0xff);
  735. if (!timeout)
  736. /* Restart */
  737. writel(seq->seq_cfg, fsm->base + SPI_FAST_SEQ_CFG);
  738. cond_resched();
  739. }
  740. dev_err(fsm->dev, "timeout on wait_busy\n");
  741. return FLASH_STATUS_TIMEOUT;
  742. }
  743. static int stfsm_read_status(struct stfsm *fsm, uint8_t cmd,
  744. uint8_t *data, int bytes)
  745. {
  746. struct stfsm_seq *seq = &stfsm_seq_read_status_fifo;
  747. uint32_t tmp;
  748. uint8_t *t = (uint8_t *)&tmp;
  749. int i;
  750. dev_dbg(fsm->dev, "read 'status' register [0x%02x], %d byte(s)\n",
  751. cmd, bytes);
  752. BUG_ON(bytes != 1 && bytes != 2);
  753. seq->seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
  754. SEQ_OPC_OPCODE(cmd)),
  755. stfsm_load_seq(fsm, seq);
  756. stfsm_read_fifo(fsm, &tmp, 4);
  757. for (i = 0; i < bytes; i++)
  758. data[i] = t[i];
  759. stfsm_wait_seq(fsm);
  760. return 0;
  761. }
  762. static int stfsm_write_status(struct stfsm *fsm, uint8_t cmd,
  763. uint16_t data, int bytes, int wait_busy)
  764. {
  765. struct stfsm_seq *seq = &stfsm_seq_write_status;
  766. dev_dbg(fsm->dev,
  767. "write 'status' register [0x%02x], %d byte(s), 0x%04x\n"
  768. " %s wait-busy\n", cmd, bytes, data, wait_busy ? "with" : "no");
  769. BUG_ON(bytes != 1 && bytes != 2);
  770. seq->seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
  771. SEQ_OPC_OPCODE(cmd));
  772. seq->status = (uint32_t)data | STA_PADS_1 | STA_CSDEASSERT;
  773. seq->seq[2] = (bytes == 1) ? STFSM_INST_STA_WR1 : STFSM_INST_STA_WR1_2;
  774. stfsm_load_seq(fsm, seq);
  775. stfsm_wait_seq(fsm);
  776. if (wait_busy)
  777. stfsm_wait_busy(fsm);
  778. return 0;
  779. }
  780. /*
  781. * SoC reset on 'boot-from-spi' systems
  782. *
  783. * Certain modes of operation cause the Flash device to enter a particular state
  784. * for a period of time (e.g. 'Erase Sector', 'Quad Enable', and 'Enter 32-bit
  785. * Addr' commands). On boot-from-spi systems, it is important to consider what
  786. * happens if a warm reset occurs during this period. The SPIBoot controller
  787. * assumes that Flash device is in its default reset state, 24-bit address mode,
  788. * and ready to accept commands. This can be achieved using some form of
  789. * on-board logic/controller to force a device POR in response to a SoC-level
  790. * reset or by making use of the device reset signal if available (limited
  791. * number of devices only).
  792. *
  793. * Failure to take such precautions can cause problems following a warm reset.
  794. * For some operations (e.g. ERASE), there is little that can be done. For
  795. * other modes of operation (e.g. 32-bit addressing), options are often
  796. * available that can help minimise the window in which a reset could cause a
  797. * problem.
  798. *
  799. */
  800. static bool stfsm_can_handle_soc_reset(struct stfsm *fsm)
  801. {
  802. /* Reset signal is available on the board and supported by the device */
  803. if (fsm->reset_signal && fsm->info->flags & FLASH_FLAG_RESET)
  804. return true;
  805. /* Board-level logic forces a power-on-reset */
  806. if (fsm->reset_por)
  807. return true;
  808. /* Reset is not properly handled and may result in failure to reboot */
  809. return false;
  810. }
  811. /* Configure 'addr_cfg' according to addressing mode */
  812. static void stfsm_prepare_erasesec_seq(struct stfsm *fsm,
  813. struct stfsm_seq *seq)
  814. {
  815. int addr1_cycles = fsm->info->flags & FLASH_FLAG_32BIT_ADDR ? 16 : 8;
  816. seq->addr_cfg = (ADR_CFG_CYCLES_ADD1(addr1_cycles) |
  817. ADR_CFG_PADS_1_ADD1 |
  818. ADR_CFG_CYCLES_ADD2(16) |
  819. ADR_CFG_PADS_1_ADD2 |
  820. ADR_CFG_CSDEASSERT_ADD2);
  821. }
  822. /* Search for preferred configuration based on available flags */
  823. static struct seq_rw_config *
  824. stfsm_search_seq_rw_configs(struct stfsm *fsm,
  825. struct seq_rw_config cfgs[])
  826. {
  827. struct seq_rw_config *config;
  828. int flags = fsm->info->flags;
  829. for (config = cfgs; config->cmd != 0; config++)
  830. if ((config->flags & flags) == config->flags)
  831. return config;
  832. return NULL;
  833. }
  834. /* Prepare a READ/WRITE sequence according to configuration parameters */
  835. static void stfsm_prepare_rw_seq(struct stfsm *fsm,
  836. struct stfsm_seq *seq,
  837. struct seq_rw_config *cfg)
  838. {
  839. int addr1_cycles, addr2_cycles;
  840. int i = 0;
  841. memset(seq, 0, sizeof(*seq));
  842. /* Add READ/WRITE OPC */
  843. seq->seq_opc[i++] = (SEQ_OPC_PADS_1 |
  844. SEQ_OPC_CYCLES(8) |
  845. SEQ_OPC_OPCODE(cfg->cmd));
  846. /* Add WREN OPC for a WRITE sequence */
  847. if (cfg->write)
  848. seq->seq_opc[i++] = (SEQ_OPC_PADS_1 |
  849. SEQ_OPC_CYCLES(8) |
  850. SEQ_OPC_OPCODE(SPINOR_OP_WREN) |
  851. SEQ_OPC_CSDEASSERT);
  852. /* Address configuration (24 or 32-bit addresses) */
  853. addr1_cycles = (fsm->info->flags & FLASH_FLAG_32BIT_ADDR) ? 16 : 8;
  854. addr1_cycles /= cfg->addr_pads;
  855. addr2_cycles = 16 / cfg->addr_pads;
  856. seq->addr_cfg = ((addr1_cycles & 0x3f) << 0 | /* ADD1 cycles */
  857. (cfg->addr_pads - 1) << 6 | /* ADD1 pads */
  858. (addr2_cycles & 0x3f) << 16 | /* ADD2 cycles */
  859. ((cfg->addr_pads - 1) << 22)); /* ADD2 pads */
  860. /* Data/Sequence configuration */
  861. seq->seq_cfg = ((cfg->data_pads - 1) << 16 |
  862. SEQ_CFG_STARTSEQ |
  863. SEQ_CFG_CSDEASSERT);
  864. if (!cfg->write)
  865. seq->seq_cfg |= SEQ_CFG_READNOTWRITE;
  866. /* Mode configuration (no. of pads taken from addr cfg) */
  867. seq->mode = ((cfg->mode_data & 0xff) << 0 | /* data */
  868. (cfg->mode_cycles & 0x3f) << 16 | /* cycles */
  869. (cfg->addr_pads - 1) << 22); /* pads */
  870. /* Dummy configuration (no. of pads taken from addr cfg) */
  871. seq->dummy = ((cfg->dummy_cycles & 0x3f) << 16 | /* cycles */
  872. (cfg->addr_pads - 1) << 22); /* pads */
  873. /* Instruction sequence */
  874. i = 0;
  875. if (cfg->write)
  876. seq->seq[i++] = STFSM_INST_CMD2;
  877. seq->seq[i++] = STFSM_INST_CMD1;
  878. seq->seq[i++] = STFSM_INST_ADD1;
  879. seq->seq[i++] = STFSM_INST_ADD2;
  880. if (cfg->mode_cycles)
  881. seq->seq[i++] = STFSM_INST_MODE;
  882. if (cfg->dummy_cycles)
  883. seq->seq[i++] = STFSM_INST_DUMMY;
  884. seq->seq[i++] =
  885. cfg->write ? STFSM_INST_DATA_WRITE : STFSM_INST_DATA_READ;
  886. seq->seq[i++] = STFSM_INST_STOP;
  887. }
  888. static int stfsm_search_prepare_rw_seq(struct stfsm *fsm,
  889. struct stfsm_seq *seq,
  890. struct seq_rw_config *cfgs)
  891. {
  892. struct seq_rw_config *config;
  893. config = stfsm_search_seq_rw_configs(fsm, cfgs);
  894. if (!config) {
  895. dev_err(fsm->dev, "failed to find suitable config\n");
  896. return -EINVAL;
  897. }
  898. stfsm_prepare_rw_seq(fsm, seq, config);
  899. return 0;
  900. }
  901. /* Prepare a READ/WRITE/ERASE 'default' sequences */
  902. static int stfsm_prepare_rwe_seqs_default(struct stfsm *fsm)
  903. {
  904. uint32_t flags = fsm->info->flags;
  905. int ret;
  906. /* Configure 'READ' sequence */
  907. ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
  908. default_read_configs);
  909. if (ret) {
  910. dev_err(fsm->dev,
  911. "failed to prep READ sequence with flags [0x%08x]\n",
  912. flags);
  913. return ret;
  914. }
  915. /* Configure 'WRITE' sequence */
  916. ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_write,
  917. default_write_configs);
  918. if (ret) {
  919. dev_err(fsm->dev,
  920. "failed to prep WRITE sequence with flags [0x%08x]\n",
  921. flags);
  922. return ret;
  923. }
  924. /* Configure 'ERASE_SECTOR' sequence */
  925. stfsm_prepare_erasesec_seq(fsm, &stfsm_seq_erase_sector);
  926. return 0;
  927. }
  928. static int stfsm_mx25_config(struct stfsm *fsm)
  929. {
  930. uint32_t flags = fsm->info->flags;
  931. uint32_t data_pads;
  932. uint8_t sta;
  933. int ret;
  934. bool soc_reset;
  935. /*
  936. * Use default READ/WRITE sequences
  937. */
  938. ret = stfsm_prepare_rwe_seqs_default(fsm);
  939. if (ret)
  940. return ret;
  941. /*
  942. * Configure 32-bit Address Support
  943. */
  944. if (flags & FLASH_FLAG_32BIT_ADDR) {
  945. /* Configure 'enter_32bitaddr' FSM sequence */
  946. stfsm_mx25_en_32bit_addr_seq(&fsm->stfsm_seq_en_32bit_addr);
  947. soc_reset = stfsm_can_handle_soc_reset(fsm);
  948. if (soc_reset || !fsm->booted_from_spi)
  949. /* If we can handle SoC resets, we enable 32-bit address
  950. * mode pervasively */
  951. stfsm_enter_32bit_addr(fsm, 1);
  952. else
  953. /* Else, enable/disable 32-bit addressing before/after
  954. * each operation */
  955. fsm->configuration = (CFG_READ_TOGGLE_32BIT_ADDR |
  956. CFG_WRITE_TOGGLE_32BIT_ADDR |
  957. CFG_ERASESEC_TOGGLE_32BIT_ADDR);
  958. }
  959. /* Check status of 'QE' bit, update if required. */
  960. stfsm_read_status(fsm, SPINOR_OP_RDSR, &sta, 1);
  961. data_pads = ((fsm->stfsm_seq_read.seq_cfg >> 16) & 0x3) + 1;
  962. if (data_pads == 4) {
  963. if (!(sta & MX25_STATUS_QE)) {
  964. /* Set 'QE' */
  965. sta |= MX25_STATUS_QE;
  966. stfsm_write_status(fsm, SPINOR_OP_WRSR, sta, 1, 1);
  967. }
  968. } else {
  969. if (sta & MX25_STATUS_QE) {
  970. /* Clear 'QE' */
  971. sta &= ~MX25_STATUS_QE;
  972. stfsm_write_status(fsm, SPINOR_OP_WRSR, sta, 1, 1);
  973. }
  974. }
  975. return 0;
  976. }
  977. static int stfsm_n25q_config(struct stfsm *fsm)
  978. {
  979. uint32_t flags = fsm->info->flags;
  980. uint8_t vcr;
  981. int ret = 0;
  982. bool soc_reset;
  983. /* Configure 'READ' sequence */
  984. if (flags & FLASH_FLAG_32BIT_ADDR)
  985. ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
  986. n25q_read4_configs);
  987. else
  988. ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
  989. n25q_read3_configs);
  990. if (ret) {
  991. dev_err(fsm->dev,
  992. "failed to prepare READ sequence with flags [0x%08x]\n",
  993. flags);
  994. return ret;
  995. }
  996. /* Configure 'WRITE' sequence (default configs) */
  997. ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_write,
  998. default_write_configs);
  999. if (ret) {
  1000. dev_err(fsm->dev,
  1001. "preparing WRITE sequence using flags [0x%08x] failed\n",
  1002. flags);
  1003. return ret;
  1004. }
  1005. /* * Configure 'ERASE_SECTOR' sequence */
  1006. stfsm_prepare_erasesec_seq(fsm, &stfsm_seq_erase_sector);
  1007. /* Configure 32-bit address support */
  1008. if (flags & FLASH_FLAG_32BIT_ADDR) {
  1009. stfsm_n25q_en_32bit_addr_seq(&fsm->stfsm_seq_en_32bit_addr);
  1010. soc_reset = stfsm_can_handle_soc_reset(fsm);
  1011. if (soc_reset || !fsm->booted_from_spi) {
  1012. /*
  1013. * If we can handle SoC resets, we enable 32-bit
  1014. * address mode pervasively
  1015. */
  1016. stfsm_enter_32bit_addr(fsm, 1);
  1017. } else {
  1018. /*
  1019. * If not, enable/disable for WRITE and ERASE
  1020. * operations (READ uses special commands)
  1021. */
  1022. fsm->configuration = (CFG_WRITE_TOGGLE_32BIT_ADDR |
  1023. CFG_ERASESEC_TOGGLE_32BIT_ADDR);
  1024. }
  1025. }
  1026. /*
  1027. * Configure device to use 8 dummy cycles
  1028. */
  1029. vcr = (N25Q_VCR_DUMMY_CYCLES(8) | N25Q_VCR_XIP_DISABLED |
  1030. N25Q_VCR_WRAP_CONT);
  1031. stfsm_write_status(fsm, N25Q_CMD_WRVCR, vcr, 1, 0);
  1032. return 0;
  1033. }
  1034. static void stfsm_s25fl_prepare_erasesec_seq_32(struct stfsm_seq *seq)
  1035. {
  1036. seq->seq_opc[1] = (SEQ_OPC_PADS_1 |
  1037. SEQ_OPC_CYCLES(8) |
  1038. SEQ_OPC_OPCODE(S25FL_CMD_SE4));
  1039. seq->addr_cfg = (ADR_CFG_CYCLES_ADD1(16) |
  1040. ADR_CFG_PADS_1_ADD1 |
  1041. ADR_CFG_CYCLES_ADD2(16) |
  1042. ADR_CFG_PADS_1_ADD2 |
  1043. ADR_CFG_CSDEASSERT_ADD2);
  1044. }
  1045. static void stfsm_s25fl_read_dyb(struct stfsm *fsm, uint32_t offs, uint8_t *dby)
  1046. {
  1047. uint32_t tmp;
  1048. struct stfsm_seq seq = {
  1049. .data_size = TRANSFER_SIZE(4),
  1050. .seq_opc[0] = (SEQ_OPC_PADS_1 |
  1051. SEQ_OPC_CYCLES(8) |
  1052. SEQ_OPC_OPCODE(S25FL_CMD_DYBRD)),
  1053. .addr_cfg = (ADR_CFG_CYCLES_ADD1(16) |
  1054. ADR_CFG_PADS_1_ADD1 |
  1055. ADR_CFG_CYCLES_ADD2(16) |
  1056. ADR_CFG_PADS_1_ADD2),
  1057. .addr1 = (offs >> 16) & 0xffff,
  1058. .addr2 = offs & 0xffff,
  1059. .seq = {
  1060. STFSM_INST_CMD1,
  1061. STFSM_INST_ADD1,
  1062. STFSM_INST_ADD2,
  1063. STFSM_INST_DATA_READ,
  1064. STFSM_INST_STOP,
  1065. },
  1066. .seq_cfg = (SEQ_CFG_PADS_1 |
  1067. SEQ_CFG_READNOTWRITE |
  1068. SEQ_CFG_CSDEASSERT |
  1069. SEQ_CFG_STARTSEQ),
  1070. };
  1071. stfsm_load_seq(fsm, &seq);
  1072. stfsm_read_fifo(fsm, &tmp, 4);
  1073. *dby = (uint8_t)(tmp >> 24);
  1074. stfsm_wait_seq(fsm);
  1075. }
  1076. static void stfsm_s25fl_write_dyb(struct stfsm *fsm, uint32_t offs, uint8_t dby)
  1077. {
  1078. struct stfsm_seq seq = {
  1079. .seq_opc[0] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
  1080. SEQ_OPC_OPCODE(SPINOR_OP_WREN) |
  1081. SEQ_OPC_CSDEASSERT),
  1082. .seq_opc[1] = (SEQ_OPC_PADS_1 | SEQ_OPC_CYCLES(8) |
  1083. SEQ_OPC_OPCODE(S25FL_CMD_DYBWR)),
  1084. .addr_cfg = (ADR_CFG_CYCLES_ADD1(16) |
  1085. ADR_CFG_PADS_1_ADD1 |
  1086. ADR_CFG_CYCLES_ADD2(16) |
  1087. ADR_CFG_PADS_1_ADD2),
  1088. .status = (uint32_t)dby | STA_PADS_1 | STA_CSDEASSERT,
  1089. .addr1 = (offs >> 16) & 0xffff,
  1090. .addr2 = offs & 0xffff,
  1091. .seq = {
  1092. STFSM_INST_CMD1,
  1093. STFSM_INST_CMD2,
  1094. STFSM_INST_ADD1,
  1095. STFSM_INST_ADD2,
  1096. STFSM_INST_STA_WR1,
  1097. STFSM_INST_STOP,
  1098. },
  1099. .seq_cfg = (SEQ_CFG_PADS_1 |
  1100. SEQ_CFG_READNOTWRITE |
  1101. SEQ_CFG_CSDEASSERT |
  1102. SEQ_CFG_STARTSEQ),
  1103. };
  1104. stfsm_load_seq(fsm, &seq);
  1105. stfsm_wait_seq(fsm);
  1106. stfsm_wait_busy(fsm);
  1107. }
  1108. static int stfsm_s25fl_clear_status_reg(struct stfsm *fsm)
  1109. {
  1110. struct stfsm_seq seq = {
  1111. .seq_opc[0] = (SEQ_OPC_PADS_1 |
  1112. SEQ_OPC_CYCLES(8) |
  1113. SEQ_OPC_OPCODE(S25FL_CMD_CLSR) |
  1114. SEQ_OPC_CSDEASSERT),
  1115. .seq_opc[1] = (SEQ_OPC_PADS_1 |
  1116. SEQ_OPC_CYCLES(8) |
  1117. SEQ_OPC_OPCODE(SPINOR_OP_WRDI) |
  1118. SEQ_OPC_CSDEASSERT),
  1119. .seq = {
  1120. STFSM_INST_CMD1,
  1121. STFSM_INST_CMD2,
  1122. STFSM_INST_WAIT,
  1123. STFSM_INST_STOP,
  1124. },
  1125. .seq_cfg = (SEQ_CFG_PADS_1 |
  1126. SEQ_CFG_ERASE |
  1127. SEQ_CFG_READNOTWRITE |
  1128. SEQ_CFG_CSDEASSERT |
  1129. SEQ_CFG_STARTSEQ),
  1130. };
  1131. stfsm_load_seq(fsm, &seq);
  1132. stfsm_wait_seq(fsm);
  1133. return 0;
  1134. }
  1135. static int stfsm_s25fl_config(struct stfsm *fsm)
  1136. {
  1137. struct flash_info *info = fsm->info;
  1138. uint32_t flags = info->flags;
  1139. uint32_t data_pads;
  1140. uint32_t offs;
  1141. uint16_t sta_wr;
  1142. uint8_t sr1, cr1, dyb;
  1143. int update_sr = 0;
  1144. int ret;
  1145. if (flags & FLASH_FLAG_32BIT_ADDR) {
  1146. /*
  1147. * Prepare Read/Write/Erase sequences according to S25FLxxx
  1148. * 32-bit address command set
  1149. */
  1150. ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_read,
  1151. stfsm_s25fl_read4_configs);
  1152. if (ret)
  1153. return ret;
  1154. ret = stfsm_search_prepare_rw_seq(fsm, &fsm->stfsm_seq_write,
  1155. stfsm_s25fl_write4_configs);
  1156. if (ret)
  1157. return ret;
  1158. stfsm_s25fl_prepare_erasesec_seq_32(&stfsm_seq_erase_sector);
  1159. } else {
  1160. /* Use default configurations for 24-bit addressing */
  1161. ret = stfsm_prepare_rwe_seqs_default(fsm);
  1162. if (ret)
  1163. return ret;
  1164. }
  1165. /*
  1166. * For devices that support 'DYB' sector locking, check lock status and
  1167. * unlock sectors if necessary (some variants power-on with sectors
  1168. * locked by default)
  1169. */
  1170. if (flags & FLASH_FLAG_DYB_LOCKING) {
  1171. offs = 0;
  1172. for (offs = 0; offs < info->sector_size * info->n_sectors;) {
  1173. stfsm_s25fl_read_dyb(fsm, offs, &dyb);
  1174. if (dyb == 0x00)
  1175. stfsm_s25fl_write_dyb(fsm, offs, 0xff);
  1176. /* Handle bottom/top 4KiB parameter sectors */
  1177. if ((offs < info->sector_size * 2) ||
  1178. (offs >= (info->sector_size - info->n_sectors * 4)))
  1179. offs += 0x1000;
  1180. else
  1181. offs += 0x10000;
  1182. }
  1183. }
  1184. /* Check status of 'QE' bit, update if required. */
  1185. stfsm_read_status(fsm, SPINOR_OP_RDSR2, &cr1, 1);
  1186. data_pads = ((fsm->stfsm_seq_read.seq_cfg >> 16) & 0x3) + 1;
  1187. if (data_pads == 4) {
  1188. if (!(cr1 & STFSM_S25FL_CONFIG_QE)) {
  1189. /* Set 'QE' */
  1190. cr1 |= STFSM_S25FL_CONFIG_QE;
  1191. update_sr = 1;
  1192. }
  1193. } else {
  1194. if (cr1 & STFSM_S25FL_CONFIG_QE) {
  1195. /* Clear 'QE' */
  1196. cr1 &= ~STFSM_S25FL_CONFIG_QE;
  1197. update_sr = 1;
  1198. }
  1199. }
  1200. if (update_sr) {
  1201. stfsm_read_status(fsm, SPINOR_OP_RDSR, &sr1, 1);
  1202. sta_wr = ((uint16_t)cr1 << 8) | sr1;
  1203. stfsm_write_status(fsm, SPINOR_OP_WRSR, sta_wr, 2, 1);
  1204. }
  1205. /*
  1206. * S25FLxxx devices support Program and Error error flags.
  1207. * Configure driver to check flags and clear if necessary.
  1208. */
  1209. fsm->configuration |= CFG_S25FL_CHECK_ERROR_FLAGS;
  1210. return 0;
  1211. }
  1212. static int stfsm_w25q_config(struct stfsm *fsm)
  1213. {
  1214. uint32_t data_pads;
  1215. uint8_t sr1, sr2;
  1216. uint16_t sr_wr;
  1217. int update_sr = 0;
  1218. int ret;
  1219. ret = stfsm_prepare_rwe_seqs_default(fsm);
  1220. if (ret)
  1221. return ret;
  1222. /* Check status of 'QE' bit, update if required. */
  1223. stfsm_read_status(fsm, SPINOR_OP_RDSR2, &sr2, 1);
  1224. data_pads = ((fsm->stfsm_seq_read.seq_cfg >> 16) & 0x3) + 1;
  1225. if (data_pads == 4) {
  1226. if (!(sr2 & W25Q_STATUS_QE)) {
  1227. /* Set 'QE' */
  1228. sr2 |= W25Q_STATUS_QE;
  1229. update_sr = 1;
  1230. }
  1231. } else {
  1232. if (sr2 & W25Q_STATUS_QE) {
  1233. /* Clear 'QE' */
  1234. sr2 &= ~W25Q_STATUS_QE;
  1235. update_sr = 1;
  1236. }
  1237. }
  1238. if (update_sr) {
  1239. /* Write status register */
  1240. stfsm_read_status(fsm, SPINOR_OP_RDSR, &sr1, 1);
  1241. sr_wr = ((uint16_t)sr2 << 8) | sr1;
  1242. stfsm_write_status(fsm, SPINOR_OP_WRSR, sr_wr, 2, 1);
  1243. }
  1244. return 0;
  1245. }
  1246. static int stfsm_read(struct stfsm *fsm, uint8_t *buf, uint32_t size,
  1247. uint32_t offset)
  1248. {
  1249. struct stfsm_seq *seq = &fsm->stfsm_seq_read;
  1250. uint32_t data_pads;
  1251. uint32_t read_mask;
  1252. uint32_t size_ub;
  1253. uint32_t size_lb;
  1254. uint32_t size_mop;
  1255. uint32_t tmp[4];
  1256. uint32_t page_buf[FLASH_PAGESIZE_32];
  1257. uint8_t *p;
  1258. dev_dbg(fsm->dev, "reading %d bytes from 0x%08x\n", size, offset);
  1259. /* Enter 32-bit address mode, if required */
  1260. if (fsm->configuration & CFG_READ_TOGGLE_32BIT_ADDR)
  1261. stfsm_enter_32bit_addr(fsm, 1);
  1262. /* Must read in multiples of 32 cycles (or 32*pads/8 Bytes) */
  1263. data_pads = ((seq->seq_cfg >> 16) & 0x3) + 1;
  1264. read_mask = (data_pads << 2) - 1;
  1265. /* Handle non-aligned buf */
  1266. p = ((uintptr_t)buf & 0x3) ? (uint8_t *)page_buf : buf;
  1267. /* Handle non-aligned size */
  1268. size_ub = (size + read_mask) & ~read_mask;
  1269. size_lb = size & ~read_mask;
  1270. size_mop = size & read_mask;
  1271. seq->data_size = TRANSFER_SIZE(size_ub);
  1272. seq->addr1 = (offset >> 16) & 0xffff;
  1273. seq->addr2 = offset & 0xffff;
  1274. stfsm_load_seq(fsm, seq);
  1275. if (size_lb)
  1276. stfsm_read_fifo(fsm, (uint32_t *)p, size_lb);
  1277. if (size_mop) {
  1278. stfsm_read_fifo(fsm, tmp, read_mask + 1);
  1279. memcpy(p + size_lb, &tmp, size_mop);
  1280. }
  1281. /* Handle non-aligned buf */
  1282. if ((uintptr_t)buf & 0x3)
  1283. memcpy(buf, page_buf, size);
  1284. /* Wait for sequence to finish */
  1285. stfsm_wait_seq(fsm);
  1286. stfsm_clear_fifo(fsm);
  1287. /* Exit 32-bit address mode, if required */
  1288. if (fsm->configuration & CFG_READ_TOGGLE_32BIT_ADDR)
  1289. stfsm_enter_32bit_addr(fsm, 0);
  1290. return 0;
  1291. }
  1292. static int stfsm_write(struct stfsm *fsm, const uint8_t *buf,
  1293. uint32_t size, uint32_t offset)
  1294. {
  1295. struct stfsm_seq *seq = &fsm->stfsm_seq_write;
  1296. uint32_t data_pads;
  1297. uint32_t write_mask;
  1298. uint32_t size_ub;
  1299. uint32_t size_lb;
  1300. uint32_t size_mop;
  1301. uint32_t tmp[4];
  1302. uint32_t page_buf[FLASH_PAGESIZE_32];
  1303. uint8_t *t = (uint8_t *)&tmp;
  1304. const uint8_t *p;
  1305. int ret;
  1306. int i;
  1307. dev_dbg(fsm->dev, "writing %d bytes to 0x%08x\n", size, offset);
  1308. /* Enter 32-bit address mode, if required */
  1309. if (fsm->configuration & CFG_WRITE_TOGGLE_32BIT_ADDR)
  1310. stfsm_enter_32bit_addr(fsm, 1);
  1311. /* Must write in multiples of 32 cycles (or 32*pads/8 bytes) */
  1312. data_pads = ((seq->seq_cfg >> 16) & 0x3) + 1;
  1313. write_mask = (data_pads << 2) - 1;
  1314. /* Handle non-aligned buf */
  1315. if ((uintptr_t)buf & 0x3) {
  1316. memcpy(page_buf, buf, size);
  1317. p = (uint8_t *)page_buf;
  1318. } else {
  1319. p = buf;
  1320. }
  1321. /* Handle non-aligned size */
  1322. size_ub = (size + write_mask) & ~write_mask;
  1323. size_lb = size & ~write_mask;
  1324. size_mop = size & write_mask;
  1325. seq->data_size = TRANSFER_SIZE(size_ub);
  1326. seq->addr1 = (offset >> 16) & 0xffff;
  1327. seq->addr2 = offset & 0xffff;
  1328. /* Need to set FIFO to write mode, before writing data to FIFO (see
  1329. * GNBvb79594)
  1330. */
  1331. writel(0x00040000, fsm->base + SPI_FAST_SEQ_CFG);
  1332. /*
  1333. * Before writing data to the FIFO, apply a small delay to allow a
  1334. * potential change of FIFO direction to complete.
  1335. */
  1336. if (fsm->fifo_dir_delay == 0)
  1337. readl(fsm->base + SPI_FAST_SEQ_CFG);
  1338. else
  1339. udelay(fsm->fifo_dir_delay);
  1340. /* Write data to FIFO, before starting sequence (see GNBvd79593) */
  1341. if (size_lb) {
  1342. stfsm_write_fifo(fsm, (uint32_t *)p, size_lb);
  1343. p += size_lb;
  1344. }
  1345. /* Handle non-aligned size */
  1346. if (size_mop) {
  1347. memset(t, 0xff, write_mask + 1); /* fill with 0xff's */
  1348. for (i = 0; i < size_mop; i++)
  1349. t[i] = *p++;
  1350. stfsm_write_fifo(fsm, tmp, write_mask + 1);
  1351. }
  1352. /* Start sequence */
  1353. stfsm_load_seq(fsm, seq);
  1354. /* Wait for sequence to finish */
  1355. stfsm_wait_seq(fsm);
  1356. /* Wait for completion */
  1357. ret = stfsm_wait_busy(fsm);
  1358. if (ret && fsm->configuration & CFG_S25FL_CHECK_ERROR_FLAGS)
  1359. stfsm_s25fl_clear_status_reg(fsm);
  1360. /* Exit 32-bit address mode, if required */
  1361. if (fsm->configuration & CFG_WRITE_TOGGLE_32BIT_ADDR)
  1362. stfsm_enter_32bit_addr(fsm, 0);
  1363. return 0;
  1364. }
  1365. /*
  1366. * Read an address range from the flash chip. The address range
  1367. * may be any size provided it is within the physical boundaries.
  1368. */
  1369. static int stfsm_mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
  1370. size_t *retlen, u_char *buf)
  1371. {
  1372. struct stfsm *fsm = dev_get_drvdata(mtd->dev.parent);
  1373. uint32_t bytes;
  1374. dev_dbg(fsm->dev, "%s from 0x%08x, len %zd\n",
  1375. __func__, (u32)from, len);
  1376. mutex_lock(&fsm->lock);
  1377. while (len > 0) {
  1378. bytes = min_t(size_t, len, FLASH_PAGESIZE);
  1379. stfsm_read(fsm, buf, bytes, from);
  1380. buf += bytes;
  1381. from += bytes;
  1382. len -= bytes;
  1383. *retlen += bytes;
  1384. }
  1385. mutex_unlock(&fsm->lock);
  1386. return 0;
  1387. }
  1388. static int stfsm_erase_sector(struct stfsm *fsm, uint32_t offset)
  1389. {
  1390. struct stfsm_seq *seq = &stfsm_seq_erase_sector;
  1391. int ret;
  1392. dev_dbg(fsm->dev, "erasing sector at 0x%08x\n", offset);
  1393. /* Enter 32-bit address mode, if required */
  1394. if (fsm->configuration & CFG_ERASESEC_TOGGLE_32BIT_ADDR)
  1395. stfsm_enter_32bit_addr(fsm, 1);
  1396. seq->addr1 = (offset >> 16) & 0xffff;
  1397. seq->addr2 = offset & 0xffff;
  1398. stfsm_load_seq(fsm, seq);
  1399. stfsm_wait_seq(fsm);
  1400. /* Wait for completion */
  1401. ret = stfsm_wait_busy(fsm);
  1402. if (ret && fsm->configuration & CFG_S25FL_CHECK_ERROR_FLAGS)
  1403. stfsm_s25fl_clear_status_reg(fsm);
  1404. /* Exit 32-bit address mode, if required */
  1405. if (fsm->configuration & CFG_ERASESEC_TOGGLE_32BIT_ADDR)
  1406. stfsm_enter_32bit_addr(fsm, 0);
  1407. return ret;
  1408. }
  1409. static int stfsm_erase_chip(struct stfsm *fsm)
  1410. {
  1411. const struct stfsm_seq *seq = &stfsm_seq_erase_chip;
  1412. dev_dbg(fsm->dev, "erasing chip\n");
  1413. stfsm_load_seq(fsm, seq);
  1414. stfsm_wait_seq(fsm);
  1415. return stfsm_wait_busy(fsm);
  1416. }
  1417. /*
  1418. * Write an address range to the flash chip. Data must be written in
  1419. * FLASH_PAGESIZE chunks. The address range may be any size provided
  1420. * it is within the physical boundaries.
  1421. */
  1422. static int stfsm_mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
  1423. size_t *retlen, const u_char *buf)
  1424. {
  1425. struct stfsm *fsm = dev_get_drvdata(mtd->dev.parent);
  1426. u32 page_offs;
  1427. u32 bytes;
  1428. uint8_t *b = (uint8_t *)buf;
  1429. int ret = 0;
  1430. dev_dbg(fsm->dev, "%s to 0x%08x, len %zd\n", __func__, (u32)to, len);
  1431. /* Offset within page */
  1432. page_offs = to % FLASH_PAGESIZE;
  1433. mutex_lock(&fsm->lock);
  1434. while (len) {
  1435. /* Write up to page boundary */
  1436. bytes = min_t(size_t, FLASH_PAGESIZE - page_offs, len);
  1437. ret = stfsm_write(fsm, b, bytes, to);
  1438. if (ret)
  1439. goto out1;
  1440. b += bytes;
  1441. len -= bytes;
  1442. to += bytes;
  1443. /* We are now page-aligned */
  1444. page_offs = 0;
  1445. *retlen += bytes;
  1446. }
  1447. out1:
  1448. mutex_unlock(&fsm->lock);
  1449. return ret;
  1450. }
  1451. /*
  1452. * Erase an address range on the flash chip. The address range may extend
  1453. * one or more erase sectors. Return an error is there is a problem erasing.
  1454. */
  1455. static int stfsm_mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
  1456. {
  1457. struct stfsm *fsm = dev_get_drvdata(mtd->dev.parent);
  1458. u32 addr, len;
  1459. int ret;
  1460. dev_dbg(fsm->dev, "%s at 0x%llx, len %lld\n", __func__,
  1461. (long long)instr->addr, (long long)instr->len);
  1462. addr = instr->addr;
  1463. len = instr->len;
  1464. mutex_lock(&fsm->lock);
  1465. /* Whole-chip erase? */
  1466. if (len == mtd->size) {
  1467. ret = stfsm_erase_chip(fsm);
  1468. if (ret)
  1469. goto out1;
  1470. } else {
  1471. while (len) {
  1472. ret = stfsm_erase_sector(fsm, addr);
  1473. if (ret)
  1474. goto out1;
  1475. addr += mtd->erasesize;
  1476. len -= mtd->erasesize;
  1477. }
  1478. }
  1479. mutex_unlock(&fsm->lock);
  1480. instr->state = MTD_ERASE_DONE;
  1481. mtd_erase_callback(instr);
  1482. return 0;
  1483. out1:
  1484. instr->state = MTD_ERASE_FAILED;
  1485. mutex_unlock(&fsm->lock);
  1486. return ret;
  1487. }
  1488. static void stfsm_read_jedec(struct stfsm *fsm, uint8_t *jedec)
  1489. {
  1490. const struct stfsm_seq *seq = &stfsm_seq_read_jedec;
  1491. uint32_t tmp[2];
  1492. stfsm_load_seq(fsm, seq);
  1493. stfsm_read_fifo(fsm, tmp, 8);
  1494. memcpy(jedec, tmp, 5);
  1495. stfsm_wait_seq(fsm);
  1496. }
  1497. static struct flash_info *stfsm_jedec_probe(struct stfsm *fsm)
  1498. {
  1499. struct flash_info *info;
  1500. u16 ext_jedec;
  1501. u32 jedec;
  1502. u8 id[5];
  1503. stfsm_read_jedec(fsm, id);
  1504. jedec = id[0] << 16 | id[1] << 8 | id[2];
  1505. /*
  1506. * JEDEC also defines an optional "extended device information"
  1507. * string for after vendor-specific data, after the three bytes
  1508. * we use here. Supporting some chips might require using it.
  1509. */
  1510. ext_jedec = id[3] << 8 | id[4];
  1511. dev_dbg(fsm->dev, "JEDEC = 0x%08x [%02x %02x %02x %02x %02x]\n",
  1512. jedec, id[0], id[1], id[2], id[3], id[4]);
  1513. for (info = flash_types; info->name; info++) {
  1514. if (info->jedec_id == jedec) {
  1515. if (info->ext_id && info->ext_id != ext_jedec)
  1516. continue;
  1517. return info;
  1518. }
  1519. }
  1520. dev_err(fsm->dev, "Unrecognized JEDEC id %06x\n", jedec);
  1521. return NULL;
  1522. }
  1523. static int stfsm_set_mode(struct stfsm *fsm, uint32_t mode)
  1524. {
  1525. int ret, timeout = 10;
  1526. /* Wait for controller to accept mode change */
  1527. while (--timeout) {
  1528. ret = readl(fsm->base + SPI_STA_MODE_CHANGE);
  1529. if (ret & 0x1)
  1530. break;
  1531. udelay(1);
  1532. }
  1533. if (!timeout)
  1534. return -EBUSY;
  1535. writel(mode, fsm->base + SPI_MODESELECT);
  1536. return 0;
  1537. }
  1538. static void stfsm_set_freq(struct stfsm *fsm, uint32_t spi_freq)
  1539. {
  1540. uint32_t emi_freq;
  1541. uint32_t clk_div;
  1542. /* TODO: Make this dynamic */
  1543. emi_freq = STFSM_DEFAULT_EMI_FREQ;
  1544. /*
  1545. * Calculate clk_div - values between 2 and 128
  1546. * Multiple of 2, rounded up
  1547. */
  1548. clk_div = 2 * DIV_ROUND_UP(emi_freq, 2 * spi_freq);
  1549. if (clk_div < 2)
  1550. clk_div = 2;
  1551. else if (clk_div > 128)
  1552. clk_div = 128;
  1553. /*
  1554. * Determine a suitable delay for the IP to complete a change of
  1555. * direction of the FIFO. The required delay is related to the clock
  1556. * divider used. The following heuristics are based on empirical tests,
  1557. * using a 100MHz EMI clock.
  1558. */
  1559. if (clk_div <= 4)
  1560. fsm->fifo_dir_delay = 0;
  1561. else if (clk_div <= 10)
  1562. fsm->fifo_dir_delay = 1;
  1563. else
  1564. fsm->fifo_dir_delay = DIV_ROUND_UP(clk_div, 10);
  1565. dev_dbg(fsm->dev, "emi_clk = %uHZ, spi_freq = %uHZ, clk_div = %u\n",
  1566. emi_freq, spi_freq, clk_div);
  1567. writel(clk_div, fsm->base + SPI_CLOCKDIV);
  1568. }
  1569. static int stfsm_init(struct stfsm *fsm)
  1570. {
  1571. int ret;
  1572. /* Perform a soft reset of the FSM controller */
  1573. writel(SEQ_CFG_SWRESET, fsm->base + SPI_FAST_SEQ_CFG);
  1574. udelay(1);
  1575. writel(0, fsm->base + SPI_FAST_SEQ_CFG);
  1576. /* Set clock to 'safe' frequency initially */
  1577. stfsm_set_freq(fsm, STFSM_FLASH_SAFE_FREQ);
  1578. /* Switch to FSM */
  1579. ret = stfsm_set_mode(fsm, SPI_MODESELECT_FSM);
  1580. if (ret)
  1581. return ret;
  1582. /* Set timing parameters */
  1583. writel(SPI_CFG_DEVICE_ST |
  1584. SPI_CFG_DEFAULT_MIN_CS_HIGH |
  1585. SPI_CFG_DEFAULT_CS_SETUPHOLD |
  1586. SPI_CFG_DEFAULT_DATA_HOLD,
  1587. fsm->base + SPI_CONFIGDATA);
  1588. writel(STFSM_DEFAULT_WR_TIME, fsm->base + SPI_STATUS_WR_TIME_REG);
  1589. /*
  1590. * Set the FSM 'WAIT' delay to the minimum workable value. Note, for
  1591. * our purposes, the WAIT instruction is used purely to achieve
  1592. * "sequence validity" rather than actually implement a delay.
  1593. */
  1594. writel(0x00000001, fsm->base + SPI_PROGRAM_ERASE_TIME);
  1595. /* Clear FIFO, just in case */
  1596. stfsm_clear_fifo(fsm);
  1597. return 0;
  1598. }
  1599. static void stfsm_fetch_platform_configs(struct platform_device *pdev)
  1600. {
  1601. struct stfsm *fsm = platform_get_drvdata(pdev);
  1602. struct device_node *np = pdev->dev.of_node;
  1603. struct regmap *regmap;
  1604. uint32_t boot_device_reg;
  1605. uint32_t boot_device_spi;
  1606. uint32_t boot_device; /* Value we read from *boot_device_reg */
  1607. int ret;
  1608. /* Booting from SPI NOR Flash is the default */
  1609. fsm->booted_from_spi = true;
  1610. regmap = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
  1611. if (IS_ERR(regmap))
  1612. goto boot_device_fail;
  1613. fsm->reset_signal = of_property_read_bool(np, "st,reset-signal");
  1614. fsm->reset_por = of_property_read_bool(np, "st,reset-por");
  1615. /* Where in the syscon the boot device information lives */
  1616. ret = of_property_read_u32(np, "st,boot-device-reg", &boot_device_reg);
  1617. if (ret)
  1618. goto boot_device_fail;
  1619. /* Boot device value when booted from SPI NOR */
  1620. ret = of_property_read_u32(np, "st,boot-device-spi", &boot_device_spi);
  1621. if (ret)
  1622. goto boot_device_fail;
  1623. ret = regmap_read(regmap, boot_device_reg, &boot_device);
  1624. if (ret)
  1625. goto boot_device_fail;
  1626. if (boot_device != boot_device_spi)
  1627. fsm->booted_from_spi = false;
  1628. return;
  1629. boot_device_fail:
  1630. dev_warn(&pdev->dev,
  1631. "failed to fetch boot device, assuming boot from SPI\n");
  1632. }
  1633. static int stfsm_probe(struct platform_device *pdev)
  1634. {
  1635. struct device_node *np = pdev->dev.of_node;
  1636. struct mtd_part_parser_data ppdata;
  1637. struct flash_info *info;
  1638. struct resource *res;
  1639. struct stfsm *fsm;
  1640. int ret;
  1641. if (!np) {
  1642. dev_err(&pdev->dev, "No DT found\n");
  1643. return -EINVAL;
  1644. }
  1645. ppdata.of_node = np;
  1646. fsm = devm_kzalloc(&pdev->dev, sizeof(*fsm), GFP_KERNEL);
  1647. if (!fsm)
  1648. return -ENOMEM;
  1649. fsm->dev = &pdev->dev;
  1650. platform_set_drvdata(pdev, fsm);
  1651. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1652. if (!res) {
  1653. dev_err(&pdev->dev, "Resource not found\n");
  1654. return -ENODEV;
  1655. }
  1656. fsm->base = devm_ioremap_resource(&pdev->dev, res);
  1657. if (IS_ERR(fsm->base)) {
  1658. dev_err(&pdev->dev,
  1659. "Failed to reserve memory region %pR\n", res);
  1660. return PTR_ERR(fsm->base);
  1661. }
  1662. mutex_init(&fsm->lock);
  1663. ret = stfsm_init(fsm);
  1664. if (ret) {
  1665. dev_err(&pdev->dev, "Failed to initialise FSM Controller\n");
  1666. return ret;
  1667. }
  1668. stfsm_fetch_platform_configs(pdev);
  1669. /* Detect SPI FLASH device */
  1670. info = stfsm_jedec_probe(fsm);
  1671. if (!info)
  1672. return -ENODEV;
  1673. fsm->info = info;
  1674. /* Use device size to determine address width */
  1675. if (info->sector_size * info->n_sectors > 0x1000000)
  1676. info->flags |= FLASH_FLAG_32BIT_ADDR;
  1677. /*
  1678. * Configure READ/WRITE/ERASE sequences according to platform and
  1679. * device flags.
  1680. */
  1681. if (info->config) {
  1682. ret = info->config(fsm);
  1683. if (ret)
  1684. return ret;
  1685. } else {
  1686. ret = stfsm_prepare_rwe_seqs_default(fsm);
  1687. if (ret)
  1688. return ret;
  1689. }
  1690. fsm->mtd.name = info->name;
  1691. fsm->mtd.dev.parent = &pdev->dev;
  1692. fsm->mtd.type = MTD_NORFLASH;
  1693. fsm->mtd.writesize = 4;
  1694. fsm->mtd.writebufsize = fsm->mtd.writesize;
  1695. fsm->mtd.flags = MTD_CAP_NORFLASH;
  1696. fsm->mtd.size = info->sector_size * info->n_sectors;
  1697. fsm->mtd.erasesize = info->sector_size;
  1698. fsm->mtd._read = stfsm_mtd_read;
  1699. fsm->mtd._write = stfsm_mtd_write;
  1700. fsm->mtd._erase = stfsm_mtd_erase;
  1701. dev_info(&pdev->dev,
  1702. "Found serial flash device: %s\n"
  1703. " size = %llx (%lldMiB) erasesize = 0x%08x (%uKiB)\n",
  1704. info->name,
  1705. (long long)fsm->mtd.size, (long long)(fsm->mtd.size >> 20),
  1706. fsm->mtd.erasesize, (fsm->mtd.erasesize >> 10));
  1707. return mtd_device_parse_register(&fsm->mtd, NULL, &ppdata, NULL, 0);
  1708. }
  1709. static int stfsm_remove(struct platform_device *pdev)
  1710. {
  1711. struct stfsm *fsm = platform_get_drvdata(pdev);
  1712. return mtd_device_unregister(&fsm->mtd);
  1713. }
  1714. static const struct of_device_id stfsm_match[] = {
  1715. { .compatible = "st,spi-fsm", },
  1716. {},
  1717. };
  1718. MODULE_DEVICE_TABLE(of, stfsm_match);
  1719. static struct platform_driver stfsm_driver = {
  1720. .probe = stfsm_probe,
  1721. .remove = stfsm_remove,
  1722. .driver = {
  1723. .name = "st-spi-fsm",
  1724. .owner = THIS_MODULE,
  1725. .of_match_table = stfsm_match,
  1726. },
  1727. };
  1728. module_platform_driver(stfsm_driver);
  1729. MODULE_AUTHOR("Angus Clark <angus.clark@st.com>");
  1730. MODULE_DESCRIPTION("ST SPI FSM driver");
  1731. MODULE_LICENSE("GPL");