st_spi_fsm.c 58 KB

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