001-mtd-spi-nor.patch 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443
  1. This patches adds the SPI-NOR device support code form kernel 3.17-rc1.
  2. This patch does not contain any further code not in this mainline kernel.
  3. --- a/drivers/mtd/Kconfig
  4. +++ b/drivers/mtd/Kconfig
  5. @@ -366,6 +366,8 @@ source "drivers/mtd/onenand/Kconfig"
  6. source "drivers/mtd/lpddr/Kconfig"
  7. +source "drivers/mtd/spi-nor/Kconfig"
  8. +
  9. source "drivers/mtd/ubi/Kconfig"
  10. endif # MTD
  11. --- a/drivers/mtd/Makefile
  12. +++ b/drivers/mtd/Makefile
  13. @@ -35,4 +35,5 @@ inftl-objs := inftlcore.o inftlmount.o
  14. obj-y += chips/ lpddr/ maps/ devices/ nand/ onenand/ tests/
  15. +obj-$(CONFIG_MTD_SPI_NOR) += spi-nor/
  16. obj-$(CONFIG_MTD_UBI) += ubi/
  17. --- /dev/null
  18. +++ b/drivers/mtd/spi-nor/fsl-quadspi.c
  19. @@ -0,0 +1,1009 @@
  20. +/*
  21. + * Freescale QuadSPI driver.
  22. + *
  23. + * Copyright (C) 2013 Freescale Semiconductor, Inc.
  24. + *
  25. + * This program is free software; you can redistribute it and/or modify
  26. + * it under the terms of the GNU General Public License as published by
  27. + * the Free Software Foundation; either version 2 of the License, or
  28. + * (at your option) any later version.
  29. + */
  30. +#include <linux/kernel.h>
  31. +#include <linux/module.h>
  32. +#include <linux/interrupt.h>
  33. +#include <linux/errno.h>
  34. +#include <linux/platform_device.h>
  35. +#include <linux/sched.h>
  36. +#include <linux/delay.h>
  37. +#include <linux/io.h>
  38. +#include <linux/clk.h>
  39. +#include <linux/err.h>
  40. +#include <linux/of.h>
  41. +#include <linux/of_device.h>
  42. +#include <linux/timer.h>
  43. +#include <linux/jiffies.h>
  44. +#include <linux/completion.h>
  45. +#include <linux/mtd/mtd.h>
  46. +#include <linux/mtd/partitions.h>
  47. +#include <linux/mtd/spi-nor.h>
  48. +
  49. +/* The registers */
  50. +#define QUADSPI_MCR 0x00
  51. +#define QUADSPI_MCR_RESERVED_SHIFT 16
  52. +#define QUADSPI_MCR_RESERVED_MASK (0xF << QUADSPI_MCR_RESERVED_SHIFT)
  53. +#define QUADSPI_MCR_MDIS_SHIFT 14
  54. +#define QUADSPI_MCR_MDIS_MASK (1 << QUADSPI_MCR_MDIS_SHIFT)
  55. +#define QUADSPI_MCR_CLR_TXF_SHIFT 11
  56. +#define QUADSPI_MCR_CLR_TXF_MASK (1 << QUADSPI_MCR_CLR_TXF_SHIFT)
  57. +#define QUADSPI_MCR_CLR_RXF_SHIFT 10
  58. +#define QUADSPI_MCR_CLR_RXF_MASK (1 << QUADSPI_MCR_CLR_RXF_SHIFT)
  59. +#define QUADSPI_MCR_DDR_EN_SHIFT 7
  60. +#define QUADSPI_MCR_DDR_EN_MASK (1 << QUADSPI_MCR_DDR_EN_SHIFT)
  61. +#define QUADSPI_MCR_END_CFG_SHIFT 2
  62. +#define QUADSPI_MCR_END_CFG_MASK (3 << QUADSPI_MCR_END_CFG_SHIFT)
  63. +#define QUADSPI_MCR_SWRSTHD_SHIFT 1
  64. +#define QUADSPI_MCR_SWRSTHD_MASK (1 << QUADSPI_MCR_SWRSTHD_SHIFT)
  65. +#define QUADSPI_MCR_SWRSTSD_SHIFT 0
  66. +#define QUADSPI_MCR_SWRSTSD_MASK (1 << QUADSPI_MCR_SWRSTSD_SHIFT)
  67. +
  68. +#define QUADSPI_IPCR 0x08
  69. +#define QUADSPI_IPCR_SEQID_SHIFT 24
  70. +#define QUADSPI_IPCR_SEQID_MASK (0xF << QUADSPI_IPCR_SEQID_SHIFT)
  71. +
  72. +#define QUADSPI_BUF0CR 0x10
  73. +#define QUADSPI_BUF1CR 0x14
  74. +#define QUADSPI_BUF2CR 0x18
  75. +#define QUADSPI_BUFXCR_INVALID_MSTRID 0xe
  76. +
  77. +#define QUADSPI_BUF3CR 0x1c
  78. +#define QUADSPI_BUF3CR_ALLMST_SHIFT 31
  79. +#define QUADSPI_BUF3CR_ALLMST (1 << QUADSPI_BUF3CR_ALLMST_SHIFT)
  80. +
  81. +#define QUADSPI_BFGENCR 0x20
  82. +#define QUADSPI_BFGENCR_PAR_EN_SHIFT 16
  83. +#define QUADSPI_BFGENCR_PAR_EN_MASK (1 << (QUADSPI_BFGENCR_PAR_EN_SHIFT))
  84. +#define QUADSPI_BFGENCR_SEQID_SHIFT 12
  85. +#define QUADSPI_BFGENCR_SEQID_MASK (0xF << QUADSPI_BFGENCR_SEQID_SHIFT)
  86. +
  87. +#define QUADSPI_BUF0IND 0x30
  88. +#define QUADSPI_BUF1IND 0x34
  89. +#define QUADSPI_BUF2IND 0x38
  90. +#define QUADSPI_SFAR 0x100
  91. +
  92. +#define QUADSPI_SMPR 0x108
  93. +#define QUADSPI_SMPR_DDRSMP_SHIFT 16
  94. +#define QUADSPI_SMPR_DDRSMP_MASK (7 << QUADSPI_SMPR_DDRSMP_SHIFT)
  95. +#define QUADSPI_SMPR_FSDLY_SHIFT 6
  96. +#define QUADSPI_SMPR_FSDLY_MASK (1 << QUADSPI_SMPR_FSDLY_SHIFT)
  97. +#define QUADSPI_SMPR_FSPHS_SHIFT 5
  98. +#define QUADSPI_SMPR_FSPHS_MASK (1 << QUADSPI_SMPR_FSPHS_SHIFT)
  99. +#define QUADSPI_SMPR_HSENA_SHIFT 0
  100. +#define QUADSPI_SMPR_HSENA_MASK (1 << QUADSPI_SMPR_HSENA_SHIFT)
  101. +
  102. +#define QUADSPI_RBSR 0x10c
  103. +#define QUADSPI_RBSR_RDBFL_SHIFT 8
  104. +#define QUADSPI_RBSR_RDBFL_MASK (0x3F << QUADSPI_RBSR_RDBFL_SHIFT)
  105. +
  106. +#define QUADSPI_RBCT 0x110
  107. +#define QUADSPI_RBCT_WMRK_MASK 0x1F
  108. +#define QUADSPI_RBCT_RXBRD_SHIFT 8
  109. +#define QUADSPI_RBCT_RXBRD_USEIPS (0x1 << QUADSPI_RBCT_RXBRD_SHIFT)
  110. +
  111. +#define QUADSPI_TBSR 0x150
  112. +#define QUADSPI_TBDR 0x154
  113. +#define QUADSPI_SR 0x15c
  114. +#define QUADSPI_SR_IP_ACC_SHIFT 1
  115. +#define QUADSPI_SR_IP_ACC_MASK (0x1 << QUADSPI_SR_IP_ACC_SHIFT)
  116. +#define QUADSPI_SR_AHB_ACC_SHIFT 2
  117. +#define QUADSPI_SR_AHB_ACC_MASK (0x1 << QUADSPI_SR_AHB_ACC_SHIFT)
  118. +
  119. +#define QUADSPI_FR 0x160
  120. +#define QUADSPI_FR_TFF_MASK 0x1
  121. +
  122. +#define QUADSPI_SFA1AD 0x180
  123. +#define QUADSPI_SFA2AD 0x184
  124. +#define QUADSPI_SFB1AD 0x188
  125. +#define QUADSPI_SFB2AD 0x18c
  126. +#define QUADSPI_RBDR 0x200
  127. +
  128. +#define QUADSPI_LUTKEY 0x300
  129. +#define QUADSPI_LUTKEY_VALUE 0x5AF05AF0
  130. +
  131. +#define QUADSPI_LCKCR 0x304
  132. +#define QUADSPI_LCKER_LOCK 0x1
  133. +#define QUADSPI_LCKER_UNLOCK 0x2
  134. +
  135. +#define QUADSPI_RSER 0x164
  136. +#define QUADSPI_RSER_TFIE (0x1 << 0)
  137. +
  138. +#define QUADSPI_LUT_BASE 0x310
  139. +
  140. +/*
  141. + * The definition of the LUT register shows below:
  142. + *
  143. + * ---------------------------------------------------
  144. + * | INSTR1 | PAD1 | OPRND1 | INSTR0 | PAD0 | OPRND0 |
  145. + * ---------------------------------------------------
  146. + */
  147. +#define OPRND0_SHIFT 0
  148. +#define PAD0_SHIFT 8
  149. +#define INSTR0_SHIFT 10
  150. +#define OPRND1_SHIFT 16
  151. +
  152. +/* Instruction set for the LUT register. */
  153. +#define LUT_STOP 0
  154. +#define LUT_CMD 1
  155. +#define LUT_ADDR 2
  156. +#define LUT_DUMMY 3
  157. +#define LUT_MODE 4
  158. +#define LUT_MODE2 5
  159. +#define LUT_MODE4 6
  160. +#define LUT_READ 7
  161. +#define LUT_WRITE 8
  162. +#define LUT_JMP_ON_CS 9
  163. +#define LUT_ADDR_DDR 10
  164. +#define LUT_MODE_DDR 11
  165. +#define LUT_MODE2_DDR 12
  166. +#define LUT_MODE4_DDR 13
  167. +#define LUT_READ_DDR 14
  168. +#define LUT_WRITE_DDR 15
  169. +#define LUT_DATA_LEARN 16
  170. +
  171. +/*
  172. + * The PAD definitions for LUT register.
  173. + *
  174. + * The pad stands for the lines number of IO[0:3].
  175. + * For example, the Quad read need four IO lines, so you should
  176. + * set LUT_PAD4 which means we use four IO lines.
  177. + */
  178. +#define LUT_PAD1 0
  179. +#define LUT_PAD2 1
  180. +#define LUT_PAD4 2
  181. +
  182. +/* Oprands for the LUT register. */
  183. +#define ADDR24BIT 0x18
  184. +#define ADDR32BIT 0x20
  185. +
  186. +/* Macros for constructing the LUT register. */
  187. +#define LUT0(ins, pad, opr) \
  188. + (((opr) << OPRND0_SHIFT) | ((LUT_##pad) << PAD0_SHIFT) | \
  189. + ((LUT_##ins) << INSTR0_SHIFT))
  190. +
  191. +#define LUT1(ins, pad, opr) (LUT0(ins, pad, opr) << OPRND1_SHIFT)
  192. +
  193. +/* other macros for LUT register. */
  194. +#define QUADSPI_LUT(x) (QUADSPI_LUT_BASE + (x) * 4)
  195. +#define QUADSPI_LUT_NUM 64
  196. +
  197. +/* SEQID -- we can have 16 seqids at most. */
  198. +#define SEQID_QUAD_READ 0
  199. +#define SEQID_WREN 1
  200. +#define SEQID_WRDI 2
  201. +#define SEQID_RDSR 3
  202. +#define SEQID_SE 4
  203. +#define SEQID_CHIP_ERASE 5
  204. +#define SEQID_PP 6
  205. +#define SEQID_RDID 7
  206. +#define SEQID_WRSR 8
  207. +#define SEQID_RDCR 9
  208. +#define SEQID_EN4B 10
  209. +#define SEQID_BRWR 11
  210. +
  211. +enum fsl_qspi_devtype {
  212. + FSL_QUADSPI_VYBRID,
  213. + FSL_QUADSPI_IMX6SX,
  214. +};
  215. +
  216. +struct fsl_qspi_devtype_data {
  217. + enum fsl_qspi_devtype devtype;
  218. + int rxfifo;
  219. + int txfifo;
  220. +};
  221. +
  222. +static struct fsl_qspi_devtype_data vybrid_data = {
  223. + .devtype = FSL_QUADSPI_VYBRID,
  224. + .rxfifo = 128,
  225. + .txfifo = 64
  226. +};
  227. +
  228. +static struct fsl_qspi_devtype_data imx6sx_data = {
  229. + .devtype = FSL_QUADSPI_IMX6SX,
  230. + .rxfifo = 128,
  231. + .txfifo = 512
  232. +};
  233. +
  234. +#define FSL_QSPI_MAX_CHIP 4
  235. +struct fsl_qspi {
  236. + struct mtd_info mtd[FSL_QSPI_MAX_CHIP];
  237. + struct spi_nor nor[FSL_QSPI_MAX_CHIP];
  238. + void __iomem *iobase;
  239. + void __iomem *ahb_base; /* Used when read from AHB bus */
  240. + u32 memmap_phy;
  241. + struct clk *clk, *clk_en;
  242. + struct device *dev;
  243. + struct completion c;
  244. + struct fsl_qspi_devtype_data *devtype_data;
  245. + u32 nor_size;
  246. + u32 nor_num;
  247. + u32 clk_rate;
  248. + unsigned int chip_base_addr; /* We may support two chips. */
  249. +};
  250. +
  251. +static inline int is_vybrid_qspi(struct fsl_qspi *q)
  252. +{
  253. + return q->devtype_data->devtype == FSL_QUADSPI_VYBRID;
  254. +}
  255. +
  256. +static inline int is_imx6sx_qspi(struct fsl_qspi *q)
  257. +{
  258. + return q->devtype_data->devtype == FSL_QUADSPI_IMX6SX;
  259. +}
  260. +
  261. +/*
  262. + * An IC bug makes us to re-arrange the 32-bit data.
  263. + * The following chips, such as IMX6SLX, have fixed this bug.
  264. + */
  265. +static inline u32 fsl_qspi_endian_xchg(struct fsl_qspi *q, u32 a)
  266. +{
  267. + return is_vybrid_qspi(q) ? __swab32(a) : a;
  268. +}
  269. +
  270. +static inline void fsl_qspi_unlock_lut(struct fsl_qspi *q)
  271. +{
  272. + writel(QUADSPI_LUTKEY_VALUE, q->iobase + QUADSPI_LUTKEY);
  273. + writel(QUADSPI_LCKER_UNLOCK, q->iobase + QUADSPI_LCKCR);
  274. +}
  275. +
  276. +static inline void fsl_qspi_lock_lut(struct fsl_qspi *q)
  277. +{
  278. + writel(QUADSPI_LUTKEY_VALUE, q->iobase + QUADSPI_LUTKEY);
  279. + writel(QUADSPI_LCKER_LOCK, q->iobase + QUADSPI_LCKCR);
  280. +}
  281. +
  282. +static irqreturn_t fsl_qspi_irq_handler(int irq, void *dev_id)
  283. +{
  284. + struct fsl_qspi *q = dev_id;
  285. + u32 reg;
  286. +
  287. + /* clear interrupt */
  288. + reg = readl(q->iobase + QUADSPI_FR);
  289. + writel(reg, q->iobase + QUADSPI_FR);
  290. +
  291. + if (reg & QUADSPI_FR_TFF_MASK)
  292. + complete(&q->c);
  293. +
  294. + dev_dbg(q->dev, "QUADSPI_FR : 0x%.8x:0x%.8x\n", q->chip_base_addr, reg);
  295. + return IRQ_HANDLED;
  296. +}
  297. +
  298. +static void fsl_qspi_init_lut(struct fsl_qspi *q)
  299. +{
  300. + void __iomem *base = q->iobase;
  301. + int rxfifo = q->devtype_data->rxfifo;
  302. + u32 lut_base;
  303. + u8 cmd, addrlen, dummy;
  304. + int i;
  305. +
  306. + fsl_qspi_unlock_lut(q);
  307. +
  308. + /* Clear all the LUT table */
  309. + for (i = 0; i < QUADSPI_LUT_NUM; i++)
  310. + writel(0, base + QUADSPI_LUT_BASE + i * 4);
  311. +
  312. + /* Quad Read */
  313. + lut_base = SEQID_QUAD_READ * 4;
  314. +
  315. + if (q->nor_size <= SZ_16M) {
  316. + cmd = SPINOR_OP_READ_1_1_4;
  317. + addrlen = ADDR24BIT;
  318. + dummy = 8;
  319. + } else {
  320. + /* use the 4-byte address */
  321. + cmd = SPINOR_OP_READ_1_1_4;
  322. + addrlen = ADDR32BIT;
  323. + dummy = 8;
  324. + }
  325. +
  326. + writel(LUT0(CMD, PAD1, cmd) | LUT1(ADDR, PAD1, addrlen),
  327. + base + QUADSPI_LUT(lut_base));
  328. + writel(LUT0(DUMMY, PAD1, dummy) | LUT1(READ, PAD4, rxfifo),
  329. + base + QUADSPI_LUT(lut_base + 1));
  330. +
  331. + /* Write enable */
  332. + lut_base = SEQID_WREN * 4;
  333. + writel(LUT0(CMD, PAD1, SPINOR_OP_WREN), base + QUADSPI_LUT(lut_base));
  334. +
  335. + /* Page Program */
  336. + lut_base = SEQID_PP * 4;
  337. +
  338. + if (q->nor_size <= SZ_16M) {
  339. + cmd = SPINOR_OP_PP;
  340. + addrlen = ADDR24BIT;
  341. + } else {
  342. + /* use the 4-byte address */
  343. + cmd = SPINOR_OP_PP;
  344. + addrlen = ADDR32BIT;
  345. + }
  346. +
  347. + writel(LUT0(CMD, PAD1, cmd) | LUT1(ADDR, PAD1, addrlen),
  348. + base + QUADSPI_LUT(lut_base));
  349. + writel(LUT0(WRITE, PAD1, 0), base + QUADSPI_LUT(lut_base + 1));
  350. +
  351. + /* Read Status */
  352. + lut_base = SEQID_RDSR * 4;
  353. + writel(LUT0(CMD, PAD1, SPINOR_OP_RDSR) | LUT1(READ, PAD1, 0x1),
  354. + base + QUADSPI_LUT(lut_base));
  355. +
  356. + /* Erase a sector */
  357. + lut_base = SEQID_SE * 4;
  358. +
  359. + if (q->nor_size <= SZ_16M) {
  360. + cmd = SPINOR_OP_SE;
  361. + addrlen = ADDR24BIT;
  362. + } else {
  363. + /* use the 4-byte address */
  364. + cmd = SPINOR_OP_SE;
  365. + addrlen = ADDR32BIT;
  366. + }
  367. +
  368. + writel(LUT0(CMD, PAD1, cmd) | LUT1(ADDR, PAD1, addrlen),
  369. + base + QUADSPI_LUT(lut_base));
  370. +
  371. + /* Erase the whole chip */
  372. + lut_base = SEQID_CHIP_ERASE * 4;
  373. + writel(LUT0(CMD, PAD1, SPINOR_OP_CHIP_ERASE),
  374. + base + QUADSPI_LUT(lut_base));
  375. +
  376. + /* READ ID */
  377. + lut_base = SEQID_RDID * 4;
  378. + writel(LUT0(CMD, PAD1, SPINOR_OP_RDID) | LUT1(READ, PAD1, 0x8),
  379. + base + QUADSPI_LUT(lut_base));
  380. +
  381. + /* Write Register */
  382. + lut_base = SEQID_WRSR * 4;
  383. + writel(LUT0(CMD, PAD1, SPINOR_OP_WRSR) | LUT1(WRITE, PAD1, 0x2),
  384. + base + QUADSPI_LUT(lut_base));
  385. +
  386. + /* Read Configuration Register */
  387. + lut_base = SEQID_RDCR * 4;
  388. + writel(LUT0(CMD, PAD1, SPINOR_OP_RDCR) | LUT1(READ, PAD1, 0x1),
  389. + base + QUADSPI_LUT(lut_base));
  390. +
  391. + /* Write disable */
  392. + lut_base = SEQID_WRDI * 4;
  393. + writel(LUT0(CMD, PAD1, SPINOR_OP_WRDI), base + QUADSPI_LUT(lut_base));
  394. +
  395. + /* Enter 4 Byte Mode (Micron) */
  396. + lut_base = SEQID_EN4B * 4;
  397. + writel(LUT0(CMD, PAD1, SPINOR_OP_EN4B), base + QUADSPI_LUT(lut_base));
  398. +
  399. + /* Enter 4 Byte Mode (Spansion) */
  400. + lut_base = SEQID_BRWR * 4;
  401. + writel(LUT0(CMD, PAD1, SPINOR_OP_BRWR), base + QUADSPI_LUT(lut_base));
  402. +
  403. + fsl_qspi_lock_lut(q);
  404. +}
  405. +
  406. +/* Get the SEQID for the command */
  407. +static int fsl_qspi_get_seqid(struct fsl_qspi *q, u8 cmd)
  408. +{
  409. + switch (cmd) {
  410. + case SPINOR_OP_READ_1_1_4:
  411. + return SEQID_QUAD_READ;
  412. + case SPINOR_OP_WREN:
  413. + return SEQID_WREN;
  414. + case SPINOR_OP_WRDI:
  415. + return SEQID_WRDI;
  416. + case SPINOR_OP_RDSR:
  417. + return SEQID_RDSR;
  418. + case SPINOR_OP_SE:
  419. + return SEQID_SE;
  420. + case SPINOR_OP_CHIP_ERASE:
  421. + return SEQID_CHIP_ERASE;
  422. + case SPINOR_OP_PP:
  423. + return SEQID_PP;
  424. + case SPINOR_OP_RDID:
  425. + return SEQID_RDID;
  426. + case SPINOR_OP_WRSR:
  427. + return SEQID_WRSR;
  428. + case SPINOR_OP_RDCR:
  429. + return SEQID_RDCR;
  430. + case SPINOR_OP_EN4B:
  431. + return SEQID_EN4B;
  432. + case SPINOR_OP_BRWR:
  433. + return SEQID_BRWR;
  434. + default:
  435. + dev_err(q->dev, "Unsupported cmd 0x%.2x\n", cmd);
  436. + break;
  437. + }
  438. + return -EINVAL;
  439. +}
  440. +
  441. +static int
  442. +fsl_qspi_runcmd(struct fsl_qspi *q, u8 cmd, unsigned int addr, int len)
  443. +{
  444. + void __iomem *base = q->iobase;
  445. + int seqid;
  446. + u32 reg, reg2;
  447. + int err;
  448. +
  449. + init_completion(&q->c);
  450. + dev_dbg(q->dev, "to 0x%.8x:0x%.8x, len:%d, cmd:%.2x\n",
  451. + q->chip_base_addr, addr, len, cmd);
  452. +
  453. + /* save the reg */
  454. + reg = readl(base + QUADSPI_MCR);
  455. +
  456. + writel(q->memmap_phy + q->chip_base_addr + addr, base + QUADSPI_SFAR);
  457. + writel(QUADSPI_RBCT_WMRK_MASK | QUADSPI_RBCT_RXBRD_USEIPS,
  458. + base + QUADSPI_RBCT);
  459. + writel(reg | QUADSPI_MCR_CLR_RXF_MASK, base + QUADSPI_MCR);
  460. +
  461. + do {
  462. + reg2 = readl(base + QUADSPI_SR);
  463. + if (reg2 & (QUADSPI_SR_IP_ACC_MASK | QUADSPI_SR_AHB_ACC_MASK)) {
  464. + udelay(1);
  465. + dev_dbg(q->dev, "The controller is busy, 0x%x\n", reg2);
  466. + continue;
  467. + }
  468. + break;
  469. + } while (1);
  470. +
  471. + /* trigger the LUT now */
  472. + seqid = fsl_qspi_get_seqid(q, cmd);
  473. + writel((seqid << QUADSPI_IPCR_SEQID_SHIFT) | len, base + QUADSPI_IPCR);
  474. +
  475. + /* Wait for the interrupt. */
  476. + err = wait_for_completion_timeout(&q->c, msecs_to_jiffies(1000));
  477. + if (!err) {
  478. + dev_err(q->dev,
  479. + "cmd 0x%.2x timeout, addr@%.8x, FR:0x%.8x, SR:0x%.8x\n",
  480. + cmd, addr, readl(base + QUADSPI_FR),
  481. + readl(base + QUADSPI_SR));
  482. + err = -ETIMEDOUT;
  483. + } else {
  484. + err = 0;
  485. + }
  486. +
  487. + /* restore the MCR */
  488. + writel(reg, base + QUADSPI_MCR);
  489. +
  490. + return err;
  491. +}
  492. +
  493. +/* Read out the data from the QUADSPI_RBDR buffer registers. */
  494. +static void fsl_qspi_read_data(struct fsl_qspi *q, int len, u8 *rxbuf)
  495. +{
  496. + u32 tmp;
  497. + int i = 0;
  498. +
  499. + while (len > 0) {
  500. + tmp = readl(q->iobase + QUADSPI_RBDR + i * 4);
  501. + tmp = fsl_qspi_endian_xchg(q, tmp);
  502. + dev_dbg(q->dev, "chip addr:0x%.8x, rcv:0x%.8x\n",
  503. + q->chip_base_addr, tmp);
  504. +
  505. + if (len >= 4) {
  506. + *((u32 *)rxbuf) = tmp;
  507. + rxbuf += 4;
  508. + } else {
  509. + memcpy(rxbuf, &tmp, len);
  510. + break;
  511. + }
  512. +
  513. + len -= 4;
  514. + i++;
  515. + }
  516. +}
  517. +
  518. +/*
  519. + * If we have changed the content of the flash by writing or erasing,
  520. + * we need to invalidate the AHB buffer. If we do not do so, we may read out
  521. + * the wrong data. The spec tells us reset the AHB domain and Serial Flash
  522. + * domain at the same time.
  523. + */
  524. +static inline void fsl_qspi_invalid(struct fsl_qspi *q)
  525. +{
  526. + u32 reg;
  527. +
  528. + reg = readl(q->iobase + QUADSPI_MCR);
  529. + reg |= QUADSPI_MCR_SWRSTHD_MASK | QUADSPI_MCR_SWRSTSD_MASK;
  530. + writel(reg, q->iobase + QUADSPI_MCR);
  531. +
  532. + /*
  533. + * The minimum delay : 1 AHB + 2 SFCK clocks.
  534. + * Delay 1 us is enough.
  535. + */
  536. + udelay(1);
  537. +
  538. + reg &= ~(QUADSPI_MCR_SWRSTHD_MASK | QUADSPI_MCR_SWRSTSD_MASK);
  539. + writel(reg, q->iobase + QUADSPI_MCR);
  540. +}
  541. +
  542. +static int fsl_qspi_nor_write(struct fsl_qspi *q, struct spi_nor *nor,
  543. + u8 opcode, unsigned int to, u32 *txbuf,
  544. + unsigned count, size_t *retlen)
  545. +{
  546. + int ret, i, j;
  547. + u32 tmp;
  548. +
  549. + dev_dbg(q->dev, "to 0x%.8x:0x%.8x, len : %d\n",
  550. + q->chip_base_addr, to, count);
  551. +
  552. + /* clear the TX FIFO. */
  553. + tmp = readl(q->iobase + QUADSPI_MCR);
  554. + writel(tmp | QUADSPI_MCR_CLR_RXF_MASK, q->iobase + QUADSPI_MCR);
  555. +
  556. + /* fill the TX data to the FIFO */
  557. + for (j = 0, i = ((count + 3) / 4); j < i; j++) {
  558. + tmp = fsl_qspi_endian_xchg(q, *txbuf);
  559. + writel(tmp, q->iobase + QUADSPI_TBDR);
  560. + txbuf++;
  561. + }
  562. +
  563. + /* Trigger it */
  564. + ret = fsl_qspi_runcmd(q, opcode, to, count);
  565. +
  566. + if (ret == 0 && retlen)
  567. + *retlen += count;
  568. +
  569. + return ret;
  570. +}
  571. +
  572. +static void fsl_qspi_set_map_addr(struct fsl_qspi *q)
  573. +{
  574. + int nor_size = q->nor_size;
  575. + void __iomem *base = q->iobase;
  576. +
  577. + writel(nor_size + q->memmap_phy, base + QUADSPI_SFA1AD);
  578. + writel(nor_size * 2 + q->memmap_phy, base + QUADSPI_SFA2AD);
  579. + writel(nor_size * 3 + q->memmap_phy, base + QUADSPI_SFB1AD);
  580. + writel(nor_size * 4 + q->memmap_phy, base + QUADSPI_SFB2AD);
  581. +}
  582. +
  583. +/*
  584. + * There are two different ways to read out the data from the flash:
  585. + * the "IP Command Read" and the "AHB Command Read".
  586. + *
  587. + * The IC guy suggests we use the "AHB Command Read" which is faster
  588. + * then the "IP Command Read". (What's more is that there is a bug in
  589. + * the "IP Command Read" in the Vybrid.)
  590. + *
  591. + * After we set up the registers for the "AHB Command Read", we can use
  592. + * the memcpy to read the data directly. A "missed" access to the buffer
  593. + * causes the controller to clear the buffer, and use the sequence pointed
  594. + * by the QUADSPI_BFGENCR[SEQID] to initiate a read from the flash.
  595. + */
  596. +static void fsl_qspi_init_abh_read(struct fsl_qspi *q)
  597. +{
  598. + void __iomem *base = q->iobase;
  599. + int seqid;
  600. +
  601. + /* AHB configuration for access buffer 0/1/2 .*/
  602. + writel(QUADSPI_BUFXCR_INVALID_MSTRID, base + QUADSPI_BUF0CR);
  603. + writel(QUADSPI_BUFXCR_INVALID_MSTRID, base + QUADSPI_BUF1CR);
  604. + writel(QUADSPI_BUFXCR_INVALID_MSTRID, base + QUADSPI_BUF2CR);
  605. + writel(QUADSPI_BUF3CR_ALLMST, base + QUADSPI_BUF3CR);
  606. +
  607. + /* We only use the buffer3 */
  608. + writel(0, base + QUADSPI_BUF0IND);
  609. + writel(0, base + QUADSPI_BUF1IND);
  610. + writel(0, base + QUADSPI_BUF2IND);
  611. +
  612. + /* Set the default lut sequence for AHB Read. */
  613. + seqid = fsl_qspi_get_seqid(q, q->nor[0].read_opcode);
  614. + writel(seqid << QUADSPI_BFGENCR_SEQID_SHIFT,
  615. + q->iobase + QUADSPI_BFGENCR);
  616. +}
  617. +
  618. +/* We use this function to do some basic init for spi_nor_scan(). */
  619. +static int fsl_qspi_nor_setup(struct fsl_qspi *q)
  620. +{
  621. + void __iomem *base = q->iobase;
  622. + u32 reg;
  623. + int ret;
  624. +
  625. + /* the default frequency, we will change it in the future.*/
  626. + ret = clk_set_rate(q->clk, 66000000);
  627. + if (ret)
  628. + return ret;
  629. +
  630. + /* Init the LUT table. */
  631. + fsl_qspi_init_lut(q);
  632. +
  633. + /* Disable the module */
  634. + writel(QUADSPI_MCR_MDIS_MASK | QUADSPI_MCR_RESERVED_MASK,
  635. + base + QUADSPI_MCR);
  636. +
  637. + reg = readl(base + QUADSPI_SMPR);
  638. + writel(reg & ~(QUADSPI_SMPR_FSDLY_MASK
  639. + | QUADSPI_SMPR_FSPHS_MASK
  640. + | QUADSPI_SMPR_HSENA_MASK
  641. + | QUADSPI_SMPR_DDRSMP_MASK), base + QUADSPI_SMPR);
  642. +
  643. + /* Enable the module */
  644. + writel(QUADSPI_MCR_RESERVED_MASK | QUADSPI_MCR_END_CFG_MASK,
  645. + base + QUADSPI_MCR);
  646. +
  647. + /* enable the interrupt */
  648. + writel(QUADSPI_RSER_TFIE, q->iobase + QUADSPI_RSER);
  649. +
  650. + return 0;
  651. +}
  652. +
  653. +static int fsl_qspi_nor_setup_last(struct fsl_qspi *q)
  654. +{
  655. + unsigned long rate = q->clk_rate;
  656. + int ret;
  657. +
  658. + if (is_imx6sx_qspi(q))
  659. + rate *= 4;
  660. +
  661. + ret = clk_set_rate(q->clk, rate);
  662. + if (ret)
  663. + return ret;
  664. +
  665. + /* Init the LUT table again. */
  666. + fsl_qspi_init_lut(q);
  667. +
  668. + /* Init for AHB read */
  669. + fsl_qspi_init_abh_read(q);
  670. +
  671. + return 0;
  672. +}
  673. +
  674. +static struct of_device_id fsl_qspi_dt_ids[] = {
  675. + { .compatible = "fsl,vf610-qspi", .data = (void *)&vybrid_data, },
  676. + { .compatible = "fsl,imx6sx-qspi", .data = (void *)&imx6sx_data, },
  677. + { /* sentinel */ }
  678. +};
  679. +MODULE_DEVICE_TABLE(of, fsl_qspi_dt_ids);
  680. +
  681. +static void fsl_qspi_set_base_addr(struct fsl_qspi *q, struct spi_nor *nor)
  682. +{
  683. + q->chip_base_addr = q->nor_size * (nor - q->nor);
  684. +}
  685. +
  686. +static int fsl_qspi_read_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
  687. +{
  688. + int ret;
  689. + struct fsl_qspi *q = nor->priv;
  690. +
  691. + ret = fsl_qspi_runcmd(q, opcode, 0, len);
  692. + if (ret)
  693. + return ret;
  694. +
  695. + fsl_qspi_read_data(q, len, buf);
  696. + return 0;
  697. +}
  698. +
  699. +static int fsl_qspi_write_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len,
  700. + int write_enable)
  701. +{
  702. + struct fsl_qspi *q = nor->priv;
  703. + int ret;
  704. +
  705. + if (!buf) {
  706. + ret = fsl_qspi_runcmd(q, opcode, 0, 1);
  707. + if (ret)
  708. + return ret;
  709. +
  710. + if (opcode == SPINOR_OP_CHIP_ERASE)
  711. + fsl_qspi_invalid(q);
  712. +
  713. + } else if (len > 0) {
  714. + ret = fsl_qspi_nor_write(q, nor, opcode, 0,
  715. + (u32 *)buf, len, NULL);
  716. + } else {
  717. + dev_err(q->dev, "invalid cmd %d\n", opcode);
  718. + ret = -EINVAL;
  719. + }
  720. +
  721. + return ret;
  722. +}
  723. +
  724. +static void fsl_qspi_write(struct spi_nor *nor, loff_t to,
  725. + size_t len, size_t *retlen, const u_char *buf)
  726. +{
  727. + struct fsl_qspi *q = nor->priv;
  728. +
  729. + fsl_qspi_nor_write(q, nor, nor->program_opcode, to,
  730. + (u32 *)buf, len, retlen);
  731. +
  732. + /* invalid the data in the AHB buffer. */
  733. + fsl_qspi_invalid(q);
  734. +}
  735. +
  736. +static int fsl_qspi_read(struct spi_nor *nor, loff_t from,
  737. + size_t len, size_t *retlen, u_char *buf)
  738. +{
  739. + struct fsl_qspi *q = nor->priv;
  740. + u8 cmd = nor->read_opcode;
  741. + int ret;
  742. +
  743. + dev_dbg(q->dev, "cmd [%x],read from (0x%p, 0x%.8x, 0x%.8x),len:%d\n",
  744. + cmd, q->ahb_base, q->chip_base_addr, (unsigned int)from, len);
  745. +
  746. + /* Wait until the previous command is finished. */
  747. + ret = nor->wait_till_ready(nor);
  748. + if (ret)
  749. + return ret;
  750. +
  751. + /* Read out the data directly from the AHB buffer.*/
  752. + memcpy(buf, q->ahb_base + q->chip_base_addr + from, len);
  753. +
  754. + *retlen += len;
  755. + return 0;
  756. +}
  757. +
  758. +static int fsl_qspi_erase(struct spi_nor *nor, loff_t offs)
  759. +{
  760. + struct fsl_qspi *q = nor->priv;
  761. + int ret;
  762. +
  763. + dev_dbg(nor->dev, "%dKiB at 0x%08x:0x%08x\n",
  764. + nor->mtd->erasesize / 1024, q->chip_base_addr, (u32)offs);
  765. +
  766. + /* Wait until finished previous write command. */
  767. + ret = nor->wait_till_ready(nor);
  768. + if (ret)
  769. + return ret;
  770. +
  771. + /* Send write enable, then erase commands. */
  772. + ret = nor->write_reg(nor, SPINOR_OP_WREN, NULL, 0, 0);
  773. + if (ret)
  774. + return ret;
  775. +
  776. + ret = fsl_qspi_runcmd(q, nor->erase_opcode, offs, 0);
  777. + if (ret)
  778. + return ret;
  779. +
  780. + fsl_qspi_invalid(q);
  781. + return 0;
  782. +}
  783. +
  784. +static int fsl_qspi_prep(struct spi_nor *nor, enum spi_nor_ops ops)
  785. +{
  786. + struct fsl_qspi *q = nor->priv;
  787. + int ret;
  788. +
  789. + ret = clk_enable(q->clk_en);
  790. + if (ret)
  791. + return ret;
  792. +
  793. + ret = clk_enable(q->clk);
  794. + if (ret) {
  795. + clk_disable(q->clk_en);
  796. + return ret;
  797. + }
  798. +
  799. + fsl_qspi_set_base_addr(q, nor);
  800. + return 0;
  801. +}
  802. +
  803. +static void fsl_qspi_unprep(struct spi_nor *nor, enum spi_nor_ops ops)
  804. +{
  805. + struct fsl_qspi *q = nor->priv;
  806. +
  807. + clk_disable(q->clk);
  808. + clk_disable(q->clk_en);
  809. +}
  810. +
  811. +static int fsl_qspi_probe(struct platform_device *pdev)
  812. +{
  813. + struct device_node *np = pdev->dev.of_node;
  814. + struct mtd_part_parser_data ppdata;
  815. + struct device *dev = &pdev->dev;
  816. + struct fsl_qspi *q;
  817. + struct resource *res;
  818. + struct spi_nor *nor;
  819. + struct mtd_info *mtd;
  820. + int ret, i = 0;
  821. + bool has_second_chip = false;
  822. + const struct of_device_id *of_id =
  823. + of_match_device(fsl_qspi_dt_ids, &pdev->dev);
  824. +
  825. + q = devm_kzalloc(dev, sizeof(*q), GFP_KERNEL);
  826. + if (!q)
  827. + return -ENOMEM;
  828. +
  829. + q->nor_num = of_get_child_count(dev->of_node);
  830. + if (!q->nor_num || q->nor_num > FSL_QSPI_MAX_CHIP)
  831. + return -ENODEV;
  832. +
  833. + /* find the resources */
  834. + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "QuadSPI");
  835. + q->iobase = devm_ioremap_resource(dev, res);
  836. + if (IS_ERR(q->iobase)) {
  837. + ret = PTR_ERR(q->iobase);
  838. + goto map_failed;
  839. + }
  840. +
  841. + res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  842. + "QuadSPI-memory");
  843. + q->ahb_base = devm_ioremap_resource(dev, res);
  844. + if (IS_ERR(q->ahb_base)) {
  845. + ret = PTR_ERR(q->ahb_base);
  846. + goto map_failed;
  847. + }
  848. + q->memmap_phy = res->start;
  849. +
  850. + /* find the clocks */
  851. + q->clk_en = devm_clk_get(dev, "qspi_en");
  852. + if (IS_ERR(q->clk_en)) {
  853. + ret = PTR_ERR(q->clk_en);
  854. + goto map_failed;
  855. + }
  856. +
  857. + q->clk = devm_clk_get(dev, "qspi");
  858. + if (IS_ERR(q->clk)) {
  859. + ret = PTR_ERR(q->clk);
  860. + goto map_failed;
  861. + }
  862. +
  863. + ret = clk_prepare_enable(q->clk_en);
  864. + if (ret) {
  865. + dev_err(dev, "can not enable the qspi_en clock\n");
  866. + goto map_failed;
  867. + }
  868. +
  869. + ret = clk_prepare_enable(q->clk);
  870. + if (ret) {
  871. + clk_disable_unprepare(q->clk_en);
  872. + dev_err(dev, "can not enable the qspi clock\n");
  873. + goto map_failed;
  874. + }
  875. +
  876. + /* find the irq */
  877. + ret = platform_get_irq(pdev, 0);
  878. + if (ret < 0) {
  879. + dev_err(dev, "failed to get the irq\n");
  880. + goto irq_failed;
  881. + }
  882. +
  883. + ret = devm_request_irq(dev, ret,
  884. + fsl_qspi_irq_handler, 0, pdev->name, q);
  885. + if (ret) {
  886. + dev_err(dev, "failed to request irq.\n");
  887. + goto irq_failed;
  888. + }
  889. +
  890. + q->dev = dev;
  891. + q->devtype_data = (struct fsl_qspi_devtype_data *)of_id->data;
  892. + platform_set_drvdata(pdev, q);
  893. +
  894. + ret = fsl_qspi_nor_setup(q);
  895. + if (ret)
  896. + goto irq_failed;
  897. +
  898. + if (of_get_property(np, "fsl,qspi-has-second-chip", NULL))
  899. + has_second_chip = true;
  900. +
  901. + /* iterate the subnodes. */
  902. + for_each_available_child_of_node(dev->of_node, np) {
  903. + const struct spi_device_id *id;
  904. + char modalias[40];
  905. +
  906. + /* skip the holes */
  907. + if (!has_second_chip)
  908. + i *= 2;
  909. +
  910. + nor = &q->nor[i];
  911. + mtd = &q->mtd[i];
  912. +
  913. + nor->mtd = mtd;
  914. + nor->dev = dev;
  915. + nor->priv = q;
  916. + mtd->priv = nor;
  917. +
  918. + /* fill the hooks */
  919. + nor->read_reg = fsl_qspi_read_reg;
  920. + nor->write_reg = fsl_qspi_write_reg;
  921. + nor->read = fsl_qspi_read;
  922. + nor->write = fsl_qspi_write;
  923. + nor->erase = fsl_qspi_erase;
  924. +
  925. + nor->prepare = fsl_qspi_prep;
  926. + nor->unprepare = fsl_qspi_unprep;
  927. +
  928. + if (of_modalias_node(np, modalias, sizeof(modalias)) < 0)
  929. + goto map_failed;
  930. +
  931. + id = spi_nor_match_id(modalias);
  932. + if (!id)
  933. + goto map_failed;
  934. +
  935. + ret = of_property_read_u32(np, "spi-max-frequency",
  936. + &q->clk_rate);
  937. + if (ret < 0)
  938. + goto map_failed;
  939. +
  940. + /* set the chip address for READID */
  941. + fsl_qspi_set_base_addr(q, nor);
  942. +
  943. + ret = spi_nor_scan(nor, id, SPI_NOR_QUAD);
  944. + if (ret)
  945. + goto map_failed;
  946. +
  947. + ppdata.of_node = np;
  948. + ret = mtd_device_parse_register(mtd, NULL, &ppdata, NULL, 0);
  949. + if (ret)
  950. + goto map_failed;
  951. +
  952. + /* Set the correct NOR size now. */
  953. + if (q->nor_size == 0) {
  954. + q->nor_size = mtd->size;
  955. +
  956. + /* Map the SPI NOR to accessiable address */
  957. + fsl_qspi_set_map_addr(q);
  958. + }
  959. +
  960. + /*
  961. + * The TX FIFO is 64 bytes in the Vybrid, but the Page Program
  962. + * may writes 265 bytes per time. The write is working in the
  963. + * unit of the TX FIFO, not in the unit of the SPI NOR's page
  964. + * size.
  965. + *
  966. + * So shrink the spi_nor->page_size if it is larger then the
  967. + * TX FIFO.
  968. + */
  969. + if (nor->page_size > q->devtype_data->txfifo)
  970. + nor->page_size = q->devtype_data->txfifo;
  971. +
  972. + i++;
  973. + }
  974. +
  975. + /* finish the rest init. */
  976. + ret = fsl_qspi_nor_setup_last(q);
  977. + if (ret)
  978. + goto last_init_failed;
  979. +
  980. + clk_disable(q->clk);
  981. + clk_disable(q->clk_en);
  982. + dev_info(dev, "QuadSPI SPI NOR flash driver\n");
  983. + return 0;
  984. +
  985. +last_init_failed:
  986. + for (i = 0; i < q->nor_num; i++)
  987. + mtd_device_unregister(&q->mtd[i]);
  988. +
  989. +irq_failed:
  990. + clk_disable_unprepare(q->clk);
  991. + clk_disable_unprepare(q->clk_en);
  992. +map_failed:
  993. + dev_err(dev, "Freescale QuadSPI probe failed\n");
  994. + return ret;
  995. +}
  996. +
  997. +static int fsl_qspi_remove(struct platform_device *pdev)
  998. +{
  999. + struct fsl_qspi *q = platform_get_drvdata(pdev);
  1000. + int i;
  1001. +
  1002. + for (i = 0; i < q->nor_num; i++)
  1003. + mtd_device_unregister(&q->mtd[i]);
  1004. +
  1005. + /* disable the hardware */
  1006. + writel(QUADSPI_MCR_MDIS_MASK, q->iobase + QUADSPI_MCR);
  1007. + writel(0x0, q->iobase + QUADSPI_RSER);
  1008. +
  1009. + clk_unprepare(q->clk);
  1010. + clk_unprepare(q->clk_en);
  1011. + return 0;
  1012. +}
  1013. +
  1014. +static struct platform_driver fsl_qspi_driver = {
  1015. + .driver = {
  1016. + .name = "fsl-quadspi",
  1017. + .bus = &platform_bus_type,
  1018. + .owner = THIS_MODULE,
  1019. + .of_match_table = fsl_qspi_dt_ids,
  1020. + },
  1021. + .probe = fsl_qspi_probe,
  1022. + .remove = fsl_qspi_remove,
  1023. +};
  1024. +module_platform_driver(fsl_qspi_driver);
  1025. +
  1026. +MODULE_DESCRIPTION("Freescale QuadSPI Controller Driver");
  1027. +MODULE_AUTHOR("Freescale Semiconductor Inc.");
  1028. +MODULE_LICENSE("GPL v2");
  1029. --- /dev/null
  1030. +++ b/drivers/mtd/spi-nor/Kconfig
  1031. @@ -0,0 +1,17 @@
  1032. +menuconfig MTD_SPI_NOR
  1033. + tristate "SPI-NOR device support"
  1034. + depends on MTD
  1035. + help
  1036. + This is the framework for the SPI NOR which can be used by the SPI
  1037. + device drivers and the SPI-NOR device driver.
  1038. +
  1039. +if MTD_SPI_NOR
  1040. +
  1041. +config SPI_FSL_QUADSPI
  1042. + tristate "Freescale Quad SPI controller"
  1043. + depends on ARCH_MXC
  1044. + help
  1045. + This enables support for the Quad SPI controller in master mode.
  1046. + We only connect the NOR to this controller now.
  1047. +
  1048. +endif # MTD_SPI_NOR
  1049. --- /dev/null
  1050. +++ b/drivers/mtd/spi-nor/Makefile
  1051. @@ -0,0 +1,2 @@
  1052. +obj-$(CONFIG_MTD_SPI_NOR) += spi-nor.o
  1053. +obj-$(CONFIG_SPI_FSL_QUADSPI) += fsl-quadspi.o
  1054. --- /dev/null
  1055. +++ b/drivers/mtd/spi-nor/spi-nor.c
  1056. @@ -0,0 +1,1160 @@
  1057. +/*
  1058. + * Based on m25p80.c, by Mike Lavender ([email protected]), with
  1059. + * influence from lart.c (Abraham Van Der Merwe) and mtd_dataflash.c
  1060. + *
  1061. + * Copyright (C) 2005, Intec Automation Inc.
  1062. + * Copyright (C) 2014, Freescale Semiconductor, Inc.
  1063. + *
  1064. + * This code is free software; you can redistribute it and/or modify
  1065. + * it under the terms of the GNU General Public License version 2 as
  1066. + * published by the Free Software Foundation.
  1067. + */
  1068. +
  1069. +#include <linux/err.h>
  1070. +#include <linux/errno.h>
  1071. +#include <linux/module.h>
  1072. +#include <linux/device.h>
  1073. +#include <linux/mutex.h>
  1074. +#include <linux/math64.h>
  1075. +
  1076. +#include <linux/mtd/cfi.h>
  1077. +#include <linux/mtd/mtd.h>
  1078. +#include <linux/of_platform.h>
  1079. +#include <linux/spi/flash.h>
  1080. +#include <linux/mtd/spi-nor.h>
  1081. +
  1082. +/* Define max times to check status register before we give up. */
  1083. +#define MAX_READY_WAIT_JIFFIES (40 * HZ) /* M25P16 specs 40s max chip erase */
  1084. +
  1085. +#define JEDEC_MFR(_jedec_id) ((_jedec_id) >> 16)
  1086. +
  1087. +/*
  1088. + * Read the status register, returning its value in the location
  1089. + * Return the status register value.
  1090. + * Returns negative if error occurred.
  1091. + */
  1092. +static int read_sr(struct spi_nor *nor)
  1093. +{
  1094. + int ret;
  1095. + u8 val;
  1096. +
  1097. + ret = nor->read_reg(nor, SPINOR_OP_RDSR, &val, 1);
  1098. + if (ret < 0) {
  1099. + pr_err("error %d reading SR\n", (int) ret);
  1100. + return ret;
  1101. + }
  1102. +
  1103. + return val;
  1104. +}
  1105. +
  1106. +/*
  1107. + * Read the flag status register, returning its value in the location
  1108. + * Return the status register value.
  1109. + * Returns negative if error occurred.
  1110. + */
  1111. +static int read_fsr(struct spi_nor *nor)
  1112. +{
  1113. + int ret;
  1114. + u8 val;
  1115. +
  1116. + ret = nor->read_reg(nor, SPINOR_OP_RDFSR, &val, 1);
  1117. + if (ret < 0) {
  1118. + pr_err("error %d reading FSR\n", ret);
  1119. + return ret;
  1120. + }
  1121. +
  1122. + return val;
  1123. +}
  1124. +
  1125. +/*
  1126. + * Read configuration register, returning its value in the
  1127. + * location. Return the configuration register value.
  1128. + * Returns negative if error occured.
  1129. + */
  1130. +static int read_cr(struct spi_nor *nor)
  1131. +{
  1132. + int ret;
  1133. + u8 val;
  1134. +
  1135. + ret = nor->read_reg(nor, SPINOR_OP_RDCR, &val, 1);
  1136. + if (ret < 0) {
  1137. + dev_err(nor->dev, "error %d reading CR\n", ret);
  1138. + return ret;
  1139. + }
  1140. +
  1141. + return val;
  1142. +}
  1143. +
  1144. +/*
  1145. + * Dummy Cycle calculation for different type of read.
  1146. + * It can be used to support more commands with
  1147. + * different dummy cycle requirements.
  1148. + */
  1149. +static inline int spi_nor_read_dummy_cycles(struct spi_nor *nor)
  1150. +{
  1151. + switch (nor->flash_read) {
  1152. + case SPI_NOR_FAST:
  1153. + case SPI_NOR_DUAL:
  1154. + case SPI_NOR_QUAD:
  1155. + return 1;
  1156. + case SPI_NOR_NORMAL:
  1157. + return 0;
  1158. + }
  1159. + return 0;
  1160. +}
  1161. +
  1162. +/*
  1163. + * Write status register 1 byte
  1164. + * Returns negative if error occurred.
  1165. + */
  1166. +static inline int write_sr(struct spi_nor *nor, u8 val)
  1167. +{
  1168. + nor->cmd_buf[0] = val;
  1169. + return nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 1, 0);
  1170. +}
  1171. +
  1172. +/*
  1173. + * Set write enable latch with Write Enable command.
  1174. + * Returns negative if error occurred.
  1175. + */
  1176. +static inline int write_enable(struct spi_nor *nor)
  1177. +{
  1178. + return nor->write_reg(nor, SPINOR_OP_WREN, NULL, 0, 0);
  1179. +}
  1180. +
  1181. +/*
  1182. + * Send write disble instruction to the chip.
  1183. + */
  1184. +static inline int write_disable(struct spi_nor *nor)
  1185. +{
  1186. + return nor->write_reg(nor, SPINOR_OP_WRDI, NULL, 0, 0);
  1187. +}
  1188. +
  1189. +static inline struct spi_nor *mtd_to_spi_nor(struct mtd_info *mtd)
  1190. +{
  1191. + return mtd->priv;
  1192. +}
  1193. +
  1194. +/* Enable/disable 4-byte addressing mode. */
  1195. +static inline int set_4byte(struct spi_nor *nor, u32 jedec_id, int enable)
  1196. +{
  1197. + int status;
  1198. + bool need_wren = false;
  1199. + u8 cmd;
  1200. +
  1201. + switch (JEDEC_MFR(jedec_id)) {
  1202. + case CFI_MFR_ST: /* Micron, actually */
  1203. + /* Some Micron need WREN command; all will accept it */
  1204. + need_wren = true;
  1205. + case CFI_MFR_MACRONIX:
  1206. + case 0xEF /* winbond */:
  1207. + if (need_wren)
  1208. + write_enable(nor);
  1209. +
  1210. + cmd = enable ? SPINOR_OP_EN4B : SPINOR_OP_EX4B;
  1211. + status = nor->write_reg(nor, cmd, NULL, 0, 0);
  1212. + if (need_wren)
  1213. + write_disable(nor);
  1214. +
  1215. + return status;
  1216. + default:
  1217. + /* Spansion style */
  1218. + nor->cmd_buf[0] = enable << 7;
  1219. + return nor->write_reg(nor, SPINOR_OP_BRWR, nor->cmd_buf, 1, 0);
  1220. + }
  1221. +}
  1222. +
  1223. +static int spi_nor_wait_till_ready(struct spi_nor *nor)
  1224. +{
  1225. + unsigned long deadline;
  1226. + int sr;
  1227. +
  1228. + deadline = jiffies + MAX_READY_WAIT_JIFFIES;
  1229. +
  1230. + do {
  1231. + cond_resched();
  1232. +
  1233. + sr = read_sr(nor);
  1234. + if (sr < 0)
  1235. + break;
  1236. + else if (!(sr & SR_WIP))
  1237. + return 0;
  1238. + } while (!time_after_eq(jiffies, deadline));
  1239. +
  1240. + return -ETIMEDOUT;
  1241. +}
  1242. +
  1243. +static int spi_nor_wait_till_fsr_ready(struct spi_nor *nor)
  1244. +{
  1245. + unsigned long deadline;
  1246. + int sr;
  1247. + int fsr;
  1248. +
  1249. + deadline = jiffies + MAX_READY_WAIT_JIFFIES;
  1250. +
  1251. + do {
  1252. + cond_resched();
  1253. +
  1254. + sr = read_sr(nor);
  1255. + if (sr < 0) {
  1256. + break;
  1257. + } else if (!(sr & SR_WIP)) {
  1258. + fsr = read_fsr(nor);
  1259. + if (fsr < 0)
  1260. + break;
  1261. + if (fsr & FSR_READY)
  1262. + return 0;
  1263. + }
  1264. + } while (!time_after_eq(jiffies, deadline));
  1265. +
  1266. + return -ETIMEDOUT;
  1267. +}
  1268. +
  1269. +/*
  1270. + * Service routine to read status register until ready, or timeout occurs.
  1271. + * Returns non-zero if error.
  1272. + */
  1273. +static int wait_till_ready(struct spi_nor *nor)
  1274. +{
  1275. + return nor->wait_till_ready(nor);
  1276. +}
  1277. +
  1278. +/*
  1279. + * Erase the whole flash memory
  1280. + *
  1281. + * Returns 0 if successful, non-zero otherwise.
  1282. + */
  1283. +static int erase_chip(struct spi_nor *nor)
  1284. +{
  1285. + int ret;
  1286. +
  1287. + dev_dbg(nor->dev, " %lldKiB\n", (long long)(nor->mtd->size >> 10));
  1288. +
  1289. + /* Wait until finished previous write command. */
  1290. + ret = wait_till_ready(nor);
  1291. + if (ret)
  1292. + return ret;
  1293. +
  1294. + /* Send write enable, then erase commands. */
  1295. + write_enable(nor);
  1296. +
  1297. + return nor->write_reg(nor, SPINOR_OP_CHIP_ERASE, NULL, 0, 0);
  1298. +}
  1299. +
  1300. +static int spi_nor_lock_and_prep(struct spi_nor *nor, enum spi_nor_ops ops)
  1301. +{
  1302. + int ret = 0;
  1303. +
  1304. + mutex_lock(&nor->lock);
  1305. +
  1306. + if (nor->prepare) {
  1307. + ret = nor->prepare(nor, ops);
  1308. + if (ret) {
  1309. + dev_err(nor->dev, "failed in the preparation.\n");
  1310. + mutex_unlock(&nor->lock);
  1311. + return ret;
  1312. + }
  1313. + }
  1314. + return ret;
  1315. +}
  1316. +
  1317. +static void spi_nor_unlock_and_unprep(struct spi_nor *nor, enum spi_nor_ops ops)
  1318. +{
  1319. + if (nor->unprepare)
  1320. + nor->unprepare(nor, ops);
  1321. + mutex_unlock(&nor->lock);
  1322. +}
  1323. +
  1324. +/*
  1325. + * Erase an address range on the nor chip. The address range may extend
  1326. + * one or more erase sectors. Return an error is there is a problem erasing.
  1327. + */
  1328. +static int spi_nor_erase(struct mtd_info *mtd, struct erase_info *instr)
  1329. +{
  1330. + struct spi_nor *nor = mtd_to_spi_nor(mtd);
  1331. + u32 addr, len;
  1332. + uint32_t rem;
  1333. + int ret;
  1334. +
  1335. + dev_dbg(nor->dev, "at 0x%llx, len %lld\n", (long long)instr->addr,
  1336. + (long long)instr->len);
  1337. +
  1338. + div_u64_rem(instr->len, mtd->erasesize, &rem);
  1339. + if (rem)
  1340. + return -EINVAL;
  1341. +
  1342. + addr = instr->addr;
  1343. + len = instr->len;
  1344. +
  1345. + ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_ERASE);
  1346. + if (ret)
  1347. + return ret;
  1348. +
  1349. + /* whole-chip erase? */
  1350. + if (len == mtd->size) {
  1351. + if (erase_chip(nor)) {
  1352. + ret = -EIO;
  1353. + goto erase_err;
  1354. + }
  1355. +
  1356. + /* REVISIT in some cases we could speed up erasing large regions
  1357. + * by using SPINOR_OP_SE instead of SPINOR_OP_BE_4K. We may have set up
  1358. + * to use "small sector erase", but that's not always optimal.
  1359. + */
  1360. +
  1361. + /* "sector"-at-a-time erase */
  1362. + } else {
  1363. + while (len) {
  1364. + if (nor->erase(nor, addr)) {
  1365. + ret = -EIO;
  1366. + goto erase_err;
  1367. + }
  1368. +
  1369. + addr += mtd->erasesize;
  1370. + len -= mtd->erasesize;
  1371. + }
  1372. + }
  1373. +
  1374. + spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_ERASE);
  1375. +
  1376. + instr->state = MTD_ERASE_DONE;
  1377. + mtd_erase_callback(instr);
  1378. +
  1379. + return ret;
  1380. +
  1381. +erase_err:
  1382. + spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_ERASE);
  1383. + instr->state = MTD_ERASE_FAILED;
  1384. + return ret;
  1385. +}
  1386. +
  1387. +static int spi_nor_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  1388. +{
  1389. + struct spi_nor *nor = mtd_to_spi_nor(mtd);
  1390. + uint32_t offset = ofs;
  1391. + uint8_t status_old, status_new;
  1392. + int ret = 0;
  1393. +
  1394. + ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_LOCK);
  1395. + if (ret)
  1396. + return ret;
  1397. +
  1398. + /* Wait until finished previous command */
  1399. + ret = wait_till_ready(nor);
  1400. + if (ret)
  1401. + goto err;
  1402. +
  1403. + status_old = read_sr(nor);
  1404. +
  1405. + if (offset < mtd->size - (mtd->size / 2))
  1406. + status_new = status_old | SR_BP2 | SR_BP1 | SR_BP0;
  1407. + else if (offset < mtd->size - (mtd->size / 4))
  1408. + status_new = (status_old & ~SR_BP0) | SR_BP2 | SR_BP1;
  1409. + else if (offset < mtd->size - (mtd->size / 8))
  1410. + status_new = (status_old & ~SR_BP1) | SR_BP2 | SR_BP0;
  1411. + else if (offset < mtd->size - (mtd->size / 16))
  1412. + status_new = (status_old & ~(SR_BP0 | SR_BP1)) | SR_BP2;
  1413. + else if (offset < mtd->size - (mtd->size / 32))
  1414. + status_new = (status_old & ~SR_BP2) | SR_BP1 | SR_BP0;
  1415. + else if (offset < mtd->size - (mtd->size / 64))
  1416. + status_new = (status_old & ~(SR_BP2 | SR_BP0)) | SR_BP1;
  1417. + else
  1418. + status_new = (status_old & ~(SR_BP2 | SR_BP1)) | SR_BP0;
  1419. +
  1420. + /* Only modify protection if it will not unlock other areas */
  1421. + if ((status_new & (SR_BP2 | SR_BP1 | SR_BP0)) >
  1422. + (status_old & (SR_BP2 | SR_BP1 | SR_BP0))) {
  1423. + write_enable(nor);
  1424. + ret = write_sr(nor, status_new);
  1425. + if (ret)
  1426. + goto err;
  1427. + }
  1428. +
  1429. +err:
  1430. + spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_LOCK);
  1431. + return ret;
  1432. +}
  1433. +
  1434. +static int spi_nor_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  1435. +{
  1436. + struct spi_nor *nor = mtd_to_spi_nor(mtd);
  1437. + uint32_t offset = ofs;
  1438. + uint8_t status_old, status_new;
  1439. + int ret = 0;
  1440. +
  1441. + ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_UNLOCK);
  1442. + if (ret)
  1443. + return ret;
  1444. +
  1445. + /* Wait until finished previous command */
  1446. + ret = wait_till_ready(nor);
  1447. + if (ret)
  1448. + goto err;
  1449. +
  1450. + status_old = read_sr(nor);
  1451. +
  1452. + if (offset+len > mtd->size - (mtd->size / 64))
  1453. + status_new = status_old & ~(SR_BP2 | SR_BP1 | SR_BP0);
  1454. + else if (offset+len > mtd->size - (mtd->size / 32))
  1455. + status_new = (status_old & ~(SR_BP2 | SR_BP1)) | SR_BP0;
  1456. + else if (offset+len > mtd->size - (mtd->size / 16))
  1457. + status_new = (status_old & ~(SR_BP2 | SR_BP0)) | SR_BP1;
  1458. + else if (offset+len > mtd->size - (mtd->size / 8))
  1459. + status_new = (status_old & ~SR_BP2) | SR_BP1 | SR_BP0;
  1460. + else if (offset+len > mtd->size - (mtd->size / 4))
  1461. + status_new = (status_old & ~(SR_BP0 | SR_BP1)) | SR_BP2;
  1462. + else if (offset+len > mtd->size - (mtd->size / 2))
  1463. + status_new = (status_old & ~SR_BP1) | SR_BP2 | SR_BP0;
  1464. + else
  1465. + status_new = (status_old & ~SR_BP0) | SR_BP2 | SR_BP1;
  1466. +
  1467. + /* Only modify protection if it will not lock other areas */
  1468. + if ((status_new & (SR_BP2 | SR_BP1 | SR_BP0)) <
  1469. + (status_old & (SR_BP2 | SR_BP1 | SR_BP0))) {
  1470. + write_enable(nor);
  1471. + ret = write_sr(nor, status_new);
  1472. + if (ret)
  1473. + goto err;
  1474. + }
  1475. +
  1476. +err:
  1477. + spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_UNLOCK);
  1478. + return ret;
  1479. +}
  1480. +
  1481. +struct flash_info {
  1482. + /* JEDEC id zero means "no ID" (most older chips); otherwise it has
  1483. + * a high byte of zero plus three data bytes: the manufacturer id,
  1484. + * then a two byte device id.
  1485. + */
  1486. + u32 jedec_id;
  1487. + u16 ext_id;
  1488. +
  1489. + /* The size listed here is what works with SPINOR_OP_SE, which isn't
  1490. + * necessarily called a "sector" by the vendor.
  1491. + */
  1492. + unsigned sector_size;
  1493. + u16 n_sectors;
  1494. +
  1495. + u16 page_size;
  1496. + u16 addr_width;
  1497. +
  1498. + u16 flags;
  1499. +#define SECT_4K 0x01 /* SPINOR_OP_BE_4K works uniformly */
  1500. +#define SPI_NOR_NO_ERASE 0x02 /* No erase command needed */
  1501. +#define SST_WRITE 0x04 /* use SST byte programming */
  1502. +#define SPI_NOR_NO_FR 0x08 /* Can't do fastread */
  1503. +#define SECT_4K_PMC 0x10 /* SPINOR_OP_BE_4K_PMC works uniformly */
  1504. +#define SPI_NOR_DUAL_READ 0x20 /* Flash supports Dual Read */
  1505. +#define SPI_NOR_QUAD_READ 0x40 /* Flash supports Quad Read */
  1506. +#define USE_FSR 0x80 /* use flag status register */
  1507. +};
  1508. +
  1509. +#define INFO(_jedec_id, _ext_id, _sector_size, _n_sectors, _flags) \
  1510. + ((kernel_ulong_t)&(struct flash_info) { \
  1511. + .jedec_id = (_jedec_id), \
  1512. + .ext_id = (_ext_id), \
  1513. + .sector_size = (_sector_size), \
  1514. + .n_sectors = (_n_sectors), \
  1515. + .page_size = 256, \
  1516. + .flags = (_flags), \
  1517. + })
  1518. +
  1519. +#define CAT25_INFO(_sector_size, _n_sectors, _page_size, _addr_width, _flags) \
  1520. + ((kernel_ulong_t)&(struct flash_info) { \
  1521. + .sector_size = (_sector_size), \
  1522. + .n_sectors = (_n_sectors), \
  1523. + .page_size = (_page_size), \
  1524. + .addr_width = (_addr_width), \
  1525. + .flags = (_flags), \
  1526. + })
  1527. +
  1528. +/* NOTE: double check command sets and memory organization when you add
  1529. + * more nor chips. This current list focusses on newer chips, which
  1530. + * have been converging on command sets which including JEDEC ID.
  1531. + */
  1532. +const struct spi_device_id spi_nor_ids[] = {
  1533. + /* Atmel -- some are (confusingly) marketed as "DataFlash" */
  1534. + { "at25fs010", INFO(0x1f6601, 0, 32 * 1024, 4, SECT_4K) },
  1535. + { "at25fs040", INFO(0x1f6604, 0, 64 * 1024, 8, SECT_4K) },
  1536. +
  1537. + { "at25df041a", INFO(0x1f4401, 0, 64 * 1024, 8, SECT_4K) },
  1538. + { "at25df321a", INFO(0x1f4701, 0, 64 * 1024, 64, SECT_4K) },
  1539. + { "at25df641", INFO(0x1f4800, 0, 64 * 1024, 128, SECT_4K) },
  1540. +
  1541. + { "at26f004", INFO(0x1f0400, 0, 64 * 1024, 8, SECT_4K) },
  1542. + { "at26df081a", INFO(0x1f4501, 0, 64 * 1024, 16, SECT_4K) },
  1543. + { "at26df161a", INFO(0x1f4601, 0, 64 * 1024, 32, SECT_4K) },
  1544. + { "at26df321", INFO(0x1f4700, 0, 64 * 1024, 64, SECT_4K) },
  1545. +
  1546. + { "at45db081d", INFO(0x1f2500, 0, 64 * 1024, 16, SECT_4K) },
  1547. +
  1548. + /* EON -- en25xxx */
  1549. + { "en25f32", INFO(0x1c3116, 0, 64 * 1024, 64, SECT_4K) },
  1550. + { "en25p32", INFO(0x1c2016, 0, 64 * 1024, 64, 0) },
  1551. + { "en25q32b", INFO(0x1c3016, 0, 64 * 1024, 64, 0) },
  1552. + { "en25p64", INFO(0x1c2017, 0, 64 * 1024, 128, 0) },
  1553. + { "en25q64", INFO(0x1c3017, 0, 64 * 1024, 128, SECT_4K) },
  1554. + { "en25qh128", INFO(0x1c7018, 0, 64 * 1024, 256, 0) },
  1555. + { "en25qh256", INFO(0x1c7019, 0, 64 * 1024, 512, 0) },
  1556. +
  1557. + /* ESMT */
  1558. + { "f25l32pa", INFO(0x8c2016, 0, 64 * 1024, 64, SECT_4K) },
  1559. +
  1560. + /* Everspin */
  1561. + { "mr25h256", CAT25_INFO( 32 * 1024, 1, 256, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  1562. + { "mr25h10", CAT25_INFO(128 * 1024, 1, 256, 3, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  1563. +
  1564. + /* GigaDevice */
  1565. + { "gd25q32", INFO(0xc84016, 0, 64 * 1024, 64, SECT_4K) },
  1566. + { "gd25q64", INFO(0xc84017, 0, 64 * 1024, 128, SECT_4K) },
  1567. +
  1568. + /* Intel/Numonyx -- xxxs33b */
  1569. + { "160s33b", INFO(0x898911, 0, 64 * 1024, 32, 0) },
  1570. + { "320s33b", INFO(0x898912, 0, 64 * 1024, 64, 0) },
  1571. + { "640s33b", INFO(0x898913, 0, 64 * 1024, 128, 0) },
  1572. +
  1573. + /* Macronix */
  1574. + { "mx25l2005a", INFO(0xc22012, 0, 64 * 1024, 4, SECT_4K) },
  1575. + { "mx25l4005a", INFO(0xc22013, 0, 64 * 1024, 8, SECT_4K) },
  1576. + { "mx25l8005", INFO(0xc22014, 0, 64 * 1024, 16, 0) },
  1577. + { "mx25l1606e", INFO(0xc22015, 0, 64 * 1024, 32, SECT_4K) },
  1578. + { "mx25l3205d", INFO(0xc22016, 0, 64 * 1024, 64, 0) },
  1579. + { "mx25l3255e", INFO(0xc29e16, 0, 64 * 1024, 64, SECT_4K) },
  1580. + { "mx25l6405d", INFO(0xc22017, 0, 64 * 1024, 128, 0) },
  1581. + { "mx25l12805d", INFO(0xc22018, 0, 64 * 1024, 256, 0) },
  1582. + { "mx25l12855e", INFO(0xc22618, 0, 64 * 1024, 256, 0) },
  1583. + { "mx25l25635e", INFO(0xc22019, 0, 64 * 1024, 512, 0) },
  1584. + { "mx25l25655e", INFO(0xc22619, 0, 64 * 1024, 512, 0) },
  1585. + { "mx66l51235l", INFO(0xc2201a, 0, 64 * 1024, 1024, SPI_NOR_QUAD_READ) },
  1586. + { "mx66l1g55g", INFO(0xc2261b, 0, 64 * 1024, 2048, SPI_NOR_QUAD_READ) },
  1587. +
  1588. + /* Micron */
  1589. + { "n25q064", INFO(0x20ba17, 0, 64 * 1024, 128, 0) },
  1590. + { "n25q128a11", INFO(0x20bb18, 0, 64 * 1024, 256, 0) },
  1591. + { "n25q128a13", INFO(0x20ba18, 0, 64 * 1024, 256, 0) },
  1592. + { "n25q256a", INFO(0x20ba19, 0, 64 * 1024, 512, SECT_4K) },
  1593. + { "n25q512a", INFO(0x20bb20, 0, 64 * 1024, 1024, SECT_4K) },
  1594. + { "n25q512ax3", INFO(0x20ba20, 0, 64 * 1024, 1024, USE_FSR) },
  1595. + { "n25q00", INFO(0x20ba21, 0, 64 * 1024, 2048, USE_FSR) },
  1596. +
  1597. + /* PMC */
  1598. + { "pm25lv512", INFO(0, 0, 32 * 1024, 2, SECT_4K_PMC) },
  1599. + { "pm25lv010", INFO(0, 0, 32 * 1024, 4, SECT_4K_PMC) },
  1600. + { "pm25lq032", INFO(0x7f9d46, 0, 64 * 1024, 64, SECT_4K) },
  1601. +
  1602. + /* Spansion -- single (large) sector size only, at least
  1603. + * for the chips listed here (without boot sectors).
  1604. + */
  1605. + { "s25sl032p", INFO(0x010215, 0x4d00, 64 * 1024, 64, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  1606. + { "s25sl064p", INFO(0x010216, 0x4d00, 64 * 1024, 128, 0) },
  1607. + { "s25fl256s0", INFO(0x010219, 0x4d00, 256 * 1024, 128, 0) },
  1608. + { "s25fl256s1", INFO(0x010219, 0x4d01, 64 * 1024, 512, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  1609. + { "s25fl512s", INFO(0x010220, 0x4d00, 256 * 1024, 256, SPI_NOR_DUAL_READ | SPI_NOR_QUAD_READ) },
  1610. + { "s70fl01gs", INFO(0x010221, 0x4d00, 256 * 1024, 256, 0) },
  1611. + { "s25sl12800", INFO(0x012018, 0x0300, 256 * 1024, 64, 0) },
  1612. + { "s25sl12801", INFO(0x012018, 0x0301, 64 * 1024, 256, 0) },
  1613. + { "s25fl129p0", INFO(0x012018, 0x4d00, 256 * 1024, 64, 0) },
  1614. + { "s25fl129p1", INFO(0x012018, 0x4d01, 64 * 1024, 256, 0) },
  1615. + { "s25sl004a", INFO(0x010212, 0, 64 * 1024, 8, 0) },
  1616. + { "s25sl008a", INFO(0x010213, 0, 64 * 1024, 16, 0) },
  1617. + { "s25sl016a", INFO(0x010214, 0, 64 * 1024, 32, 0) },
  1618. + { "s25sl032a", INFO(0x010215, 0, 64 * 1024, 64, 0) },
  1619. + { "s25sl064a", INFO(0x010216, 0, 64 * 1024, 128, 0) },
  1620. + { "s25fl008k", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K) },
  1621. + { "s25fl016k", INFO(0xef4015, 0, 64 * 1024, 32, SECT_4K) },
  1622. + { "s25fl064k", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
  1623. +
  1624. + /* SST -- large erase sizes are "overlays", "sectors" are 4K */
  1625. + { "sst25vf040b", INFO(0xbf258d, 0, 64 * 1024, 8, SECT_4K | SST_WRITE) },
  1626. + { "sst25vf080b", INFO(0xbf258e, 0, 64 * 1024, 16, SECT_4K | SST_WRITE) },
  1627. + { "sst25vf016b", INFO(0xbf2541, 0, 64 * 1024, 32, SECT_4K | SST_WRITE) },
  1628. + { "sst25vf032b", INFO(0xbf254a, 0, 64 * 1024, 64, SECT_4K | SST_WRITE) },
  1629. + { "sst25vf064c", INFO(0xbf254b, 0, 64 * 1024, 128, SECT_4K) },
  1630. + { "sst25wf512", INFO(0xbf2501, 0, 64 * 1024, 1, SECT_4K | SST_WRITE) },
  1631. + { "sst25wf010", INFO(0xbf2502, 0, 64 * 1024, 2, SECT_4K | SST_WRITE) },
  1632. + { "sst25wf020", INFO(0xbf2503, 0, 64 * 1024, 4, SECT_4K | SST_WRITE) },
  1633. + { "sst25wf040", INFO(0xbf2504, 0, 64 * 1024, 8, SECT_4K | SST_WRITE) },
  1634. +
  1635. + /* ST Microelectronics -- newer production may have feature updates */
  1636. + { "m25p05", INFO(0x202010, 0, 32 * 1024, 2, 0) },
  1637. + { "m25p10", INFO(0x202011, 0, 32 * 1024, 4, 0) },
  1638. + { "m25p20", INFO(0x202012, 0, 64 * 1024, 4, 0) },
  1639. + { "m25p40", INFO(0x202013, 0, 64 * 1024, 8, 0) },
  1640. + { "m25p80", INFO(0x202014, 0, 64 * 1024, 16, 0) },
  1641. + { "m25p16", INFO(0x202015, 0, 64 * 1024, 32, 0) },
  1642. + { "m25p32", INFO(0x202016, 0, 64 * 1024, 64, 0) },
  1643. + { "m25p64", INFO(0x202017, 0, 64 * 1024, 128, 0) },
  1644. + { "m25p128", INFO(0x202018, 0, 256 * 1024, 64, 0) },
  1645. + { "n25q032", INFO(0x20ba16, 0, 64 * 1024, 64, 0) },
  1646. +
  1647. + { "m25p05-nonjedec", INFO(0, 0, 32 * 1024, 2, 0) },
  1648. + { "m25p10-nonjedec", INFO(0, 0, 32 * 1024, 4, 0) },
  1649. + { "m25p20-nonjedec", INFO(0, 0, 64 * 1024, 4, 0) },
  1650. + { "m25p40-nonjedec", INFO(0, 0, 64 * 1024, 8, 0) },
  1651. + { "m25p80-nonjedec", INFO(0, 0, 64 * 1024, 16, 0) },
  1652. + { "m25p16-nonjedec", INFO(0, 0, 64 * 1024, 32, 0) },
  1653. + { "m25p32-nonjedec", INFO(0, 0, 64 * 1024, 64, 0) },
  1654. + { "m25p64-nonjedec", INFO(0, 0, 64 * 1024, 128, 0) },
  1655. + { "m25p128-nonjedec", INFO(0, 0, 256 * 1024, 64, 0) },
  1656. +
  1657. + { "m45pe10", INFO(0x204011, 0, 64 * 1024, 2, 0) },
  1658. + { "m45pe80", INFO(0x204014, 0, 64 * 1024, 16, 0) },
  1659. + { "m45pe16", INFO(0x204015, 0, 64 * 1024, 32, 0) },
  1660. +
  1661. + { "m25pe20", INFO(0x208012, 0, 64 * 1024, 4, 0) },
  1662. + { "m25pe80", INFO(0x208014, 0, 64 * 1024, 16, 0) },
  1663. + { "m25pe16", INFO(0x208015, 0, 64 * 1024, 32, SECT_4K) },
  1664. +
  1665. + { "m25px16", INFO(0x207115, 0, 64 * 1024, 32, SECT_4K) },
  1666. + { "m25px32", INFO(0x207116, 0, 64 * 1024, 64, SECT_4K) },
  1667. + { "m25px32-s0", INFO(0x207316, 0, 64 * 1024, 64, SECT_4K) },
  1668. + { "m25px32-s1", INFO(0x206316, 0, 64 * 1024, 64, SECT_4K) },
  1669. + { "m25px64", INFO(0x207117, 0, 64 * 1024, 128, 0) },
  1670. +
  1671. + /* Winbond -- w25x "blocks" are 64K, "sectors" are 4KiB */
  1672. + { "w25x10", INFO(0xef3011, 0, 64 * 1024, 2, SECT_4K) },
  1673. + { "w25x20", INFO(0xef3012, 0, 64 * 1024, 4, SECT_4K) },
  1674. + { "w25x40", INFO(0xef3013, 0, 64 * 1024, 8, SECT_4K) },
  1675. + { "w25x80", INFO(0xef3014, 0, 64 * 1024, 16, SECT_4K) },
  1676. + { "w25x16", INFO(0xef3015, 0, 64 * 1024, 32, SECT_4K) },
  1677. + { "w25x32", INFO(0xef3016, 0, 64 * 1024, 64, SECT_4K) },
  1678. + { "w25q32", INFO(0xef4016, 0, 64 * 1024, 64, SECT_4K) },
  1679. + { "w25q32dw", INFO(0xef6016, 0, 64 * 1024, 64, SECT_4K) },
  1680. + { "w25x64", INFO(0xef3017, 0, 64 * 1024, 128, SECT_4K) },
  1681. + { "w25q64", INFO(0xef4017, 0, 64 * 1024, 128, SECT_4K) },
  1682. + { "w25q128", INFO(0xef4018, 0, 64 * 1024, 256, SECT_4K) },
  1683. + { "w25q80", INFO(0xef5014, 0, 64 * 1024, 16, SECT_4K) },
  1684. + { "w25q80bl", INFO(0xef4014, 0, 64 * 1024, 16, SECT_4K) },
  1685. + { "w25q128", INFO(0xef4018, 0, 64 * 1024, 256, SECT_4K) },
  1686. + { "w25q256", INFO(0xef4019, 0, 64 * 1024, 512, SECT_4K) },
  1687. +
  1688. + /* Catalyst / On Semiconductor -- non-JEDEC */
  1689. + { "cat25c11", CAT25_INFO( 16, 8, 16, 1, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  1690. + { "cat25c03", CAT25_INFO( 32, 8, 16, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  1691. + { "cat25c09", CAT25_INFO( 128, 8, 32, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  1692. + { "cat25c17", CAT25_INFO( 256, 8, 32, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  1693. + { "cat25128", CAT25_INFO(2048, 8, 64, 2, SPI_NOR_NO_ERASE | SPI_NOR_NO_FR) },
  1694. + { },
  1695. +};
  1696. +EXPORT_SYMBOL_GPL(spi_nor_ids);
  1697. +
  1698. +static const struct spi_device_id *spi_nor_read_id(struct spi_nor *nor)
  1699. +{
  1700. + int tmp;
  1701. + u8 id[5];
  1702. + u32 jedec;
  1703. + u16 ext_jedec;
  1704. + struct flash_info *info;
  1705. +
  1706. + tmp = nor->read_reg(nor, SPINOR_OP_RDID, id, 5);
  1707. + if (tmp < 0) {
  1708. + dev_dbg(nor->dev, " error %d reading JEDEC ID\n", tmp);
  1709. + return ERR_PTR(tmp);
  1710. + }
  1711. + jedec = id[0];
  1712. + jedec = jedec << 8;
  1713. + jedec |= id[1];
  1714. + jedec = jedec << 8;
  1715. + jedec |= id[2];
  1716. +
  1717. + ext_jedec = id[3] << 8 | id[4];
  1718. +
  1719. + for (tmp = 0; tmp < ARRAY_SIZE(spi_nor_ids) - 1; tmp++) {
  1720. + info = (void *)spi_nor_ids[tmp].driver_data;
  1721. + if (info->jedec_id == jedec) {
  1722. + if (info->ext_id == 0 || info->ext_id == ext_jedec)
  1723. + return &spi_nor_ids[tmp];
  1724. + }
  1725. + }
  1726. + dev_err(nor->dev, "unrecognized JEDEC id %06x\n", jedec);
  1727. + return ERR_PTR(-ENODEV);
  1728. +}
  1729. +
  1730. +static const struct spi_device_id *jedec_probe(struct spi_nor *nor)
  1731. +{
  1732. + return nor->read_id(nor);
  1733. +}
  1734. +
  1735. +static int spi_nor_read(struct mtd_info *mtd, loff_t from, size_t len,
  1736. + size_t *retlen, u_char *buf)
  1737. +{
  1738. + struct spi_nor *nor = mtd_to_spi_nor(mtd);
  1739. + int ret;
  1740. +
  1741. + dev_dbg(nor->dev, "from 0x%08x, len %zd\n", (u32)from, len);
  1742. +
  1743. + ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_READ);
  1744. + if (ret)
  1745. + return ret;
  1746. +
  1747. + ret = nor->read(nor, from, len, retlen, buf);
  1748. +
  1749. + spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_READ);
  1750. + return ret;
  1751. +}
  1752. +
  1753. +static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
  1754. + size_t *retlen, const u_char *buf)
  1755. +{
  1756. + struct spi_nor *nor = mtd_to_spi_nor(mtd);
  1757. + size_t actual;
  1758. + int ret;
  1759. +
  1760. + dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
  1761. +
  1762. + ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_WRITE);
  1763. + if (ret)
  1764. + return ret;
  1765. +
  1766. + /* Wait until finished previous write command. */
  1767. + ret = wait_till_ready(nor);
  1768. + if (ret)
  1769. + goto time_out;
  1770. +
  1771. + write_enable(nor);
  1772. +
  1773. + nor->sst_write_second = false;
  1774. +
  1775. + actual = to % 2;
  1776. + /* Start write from odd address. */
  1777. + if (actual) {
  1778. + nor->program_opcode = SPINOR_OP_BP;
  1779. +
  1780. + /* write one byte. */
  1781. + nor->write(nor, to, 1, retlen, buf);
  1782. + ret = wait_till_ready(nor);
  1783. + if (ret)
  1784. + goto time_out;
  1785. + }
  1786. + to += actual;
  1787. +
  1788. + /* Write out most of the data here. */
  1789. + for (; actual < len - 1; actual += 2) {
  1790. + nor->program_opcode = SPINOR_OP_AAI_WP;
  1791. +
  1792. + /* write two bytes. */
  1793. + nor->write(nor, to, 2, retlen, buf + actual);
  1794. + ret = wait_till_ready(nor);
  1795. + if (ret)
  1796. + goto time_out;
  1797. + to += 2;
  1798. + nor->sst_write_second = true;
  1799. + }
  1800. + nor->sst_write_second = false;
  1801. +
  1802. + write_disable(nor);
  1803. + ret = wait_till_ready(nor);
  1804. + if (ret)
  1805. + goto time_out;
  1806. +
  1807. + /* Write out trailing byte if it exists. */
  1808. + if (actual != len) {
  1809. + write_enable(nor);
  1810. +
  1811. + nor->program_opcode = SPINOR_OP_BP;
  1812. + nor->write(nor, to, 1, retlen, buf + actual);
  1813. +
  1814. + ret = wait_till_ready(nor);
  1815. + if (ret)
  1816. + goto time_out;
  1817. + write_disable(nor);
  1818. + }
  1819. +time_out:
  1820. + spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_WRITE);
  1821. + return ret;
  1822. +}
  1823. +
  1824. +/*
  1825. + * Write an address range to the nor chip. Data must be written in
  1826. + * FLASH_PAGESIZE chunks. The address range may be any size provided
  1827. + * it is within the physical boundaries.
  1828. + */
  1829. +static int spi_nor_write(struct mtd_info *mtd, loff_t to, size_t len,
  1830. + size_t *retlen, const u_char *buf)
  1831. +{
  1832. + struct spi_nor *nor = mtd_to_spi_nor(mtd);
  1833. + u32 page_offset, page_size, i;
  1834. + int ret;
  1835. +
  1836. + dev_dbg(nor->dev, "to 0x%08x, len %zd\n", (u32)to, len);
  1837. +
  1838. + ret = spi_nor_lock_and_prep(nor, SPI_NOR_OPS_WRITE);
  1839. + if (ret)
  1840. + return ret;
  1841. +
  1842. + /* Wait until finished previous write command. */
  1843. + ret = wait_till_ready(nor);
  1844. + if (ret)
  1845. + goto write_err;
  1846. +
  1847. + write_enable(nor);
  1848. +
  1849. + page_offset = to & (nor->page_size - 1);
  1850. +
  1851. + /* do all the bytes fit onto one page? */
  1852. + if (page_offset + len <= nor->page_size) {
  1853. + nor->write(nor, to, len, retlen, buf);
  1854. + } else {
  1855. + /* the size of data remaining on the first page */
  1856. + page_size = nor->page_size - page_offset;
  1857. + nor->write(nor, to, page_size, retlen, buf);
  1858. +
  1859. + /* write everything in nor->page_size chunks */
  1860. + for (i = page_size; i < len; i += page_size) {
  1861. + page_size = len - i;
  1862. + if (page_size > nor->page_size)
  1863. + page_size = nor->page_size;
  1864. +
  1865. + wait_till_ready(nor);
  1866. + write_enable(nor);
  1867. +
  1868. + nor->write(nor, to + i, page_size, retlen, buf + i);
  1869. + }
  1870. + }
  1871. +
  1872. +write_err:
  1873. + spi_nor_unlock_and_unprep(nor, SPI_NOR_OPS_WRITE);
  1874. + return 0;
  1875. +}
  1876. +
  1877. +static int macronix_quad_enable(struct spi_nor *nor)
  1878. +{
  1879. + int ret, val;
  1880. +
  1881. + val = read_sr(nor);
  1882. + write_enable(nor);
  1883. +
  1884. + nor->cmd_buf[0] = val | SR_QUAD_EN_MX;
  1885. + nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 1, 0);
  1886. +
  1887. + if (wait_till_ready(nor))
  1888. + return 1;
  1889. +
  1890. + ret = read_sr(nor);
  1891. + if (!(ret > 0 && (ret & SR_QUAD_EN_MX))) {
  1892. + dev_err(nor->dev, "Macronix Quad bit not set\n");
  1893. + return -EINVAL;
  1894. + }
  1895. +
  1896. + return 0;
  1897. +}
  1898. +
  1899. +/*
  1900. + * Write status Register and configuration register with 2 bytes
  1901. + * The first byte will be written to the status register, while the
  1902. + * second byte will be written to the configuration register.
  1903. + * Return negative if error occured.
  1904. + */
  1905. +static int write_sr_cr(struct spi_nor *nor, u16 val)
  1906. +{
  1907. + nor->cmd_buf[0] = val & 0xff;
  1908. + nor->cmd_buf[1] = (val >> 8);
  1909. +
  1910. + return nor->write_reg(nor, SPINOR_OP_WRSR, nor->cmd_buf, 2, 0);
  1911. +}
  1912. +
  1913. +static int spansion_quad_enable(struct spi_nor *nor)
  1914. +{
  1915. + int ret;
  1916. + int quad_en = CR_QUAD_EN_SPAN << 8;
  1917. +
  1918. + write_enable(nor);
  1919. +
  1920. + ret = write_sr_cr(nor, quad_en);
  1921. + if (ret < 0) {
  1922. + dev_err(nor->dev,
  1923. + "error while writing configuration register\n");
  1924. + return -EINVAL;
  1925. + }
  1926. +
  1927. + /* read back and check it */
  1928. + ret = read_cr(nor);
  1929. + if (!(ret > 0 && (ret & CR_QUAD_EN_SPAN))) {
  1930. + dev_err(nor->dev, "Spansion Quad bit not set\n");
  1931. + return -EINVAL;
  1932. + }
  1933. +
  1934. + return 0;
  1935. +}
  1936. +
  1937. +static int set_quad_mode(struct spi_nor *nor, u32 jedec_id)
  1938. +{
  1939. + int status;
  1940. +
  1941. + switch (JEDEC_MFR(jedec_id)) {
  1942. + case CFI_MFR_MACRONIX:
  1943. + status = macronix_quad_enable(nor);
  1944. + if (status) {
  1945. + dev_err(nor->dev, "Macronix quad-read not enabled\n");
  1946. + return -EINVAL;
  1947. + }
  1948. + return status;
  1949. + default:
  1950. + status = spansion_quad_enable(nor);
  1951. + if (status) {
  1952. + dev_err(nor->dev, "Spansion quad-read not enabled\n");
  1953. + return -EINVAL;
  1954. + }
  1955. + return status;
  1956. + }
  1957. +}
  1958. +
  1959. +static int spi_nor_check(struct spi_nor *nor)
  1960. +{
  1961. + if (!nor->dev || !nor->read || !nor->write ||
  1962. + !nor->read_reg || !nor->write_reg || !nor->erase) {
  1963. + pr_err("spi-nor: please fill all the necessary fields!\n");
  1964. + return -EINVAL;
  1965. + }
  1966. +
  1967. + if (!nor->read_id)
  1968. + nor->read_id = spi_nor_read_id;
  1969. + if (!nor->wait_till_ready)
  1970. + nor->wait_till_ready = spi_nor_wait_till_ready;
  1971. +
  1972. + return 0;
  1973. +}
  1974. +
  1975. +int spi_nor_scan(struct spi_nor *nor, const struct spi_device_id *id,
  1976. + enum read_mode mode)
  1977. +{
  1978. + struct flash_info *info;
  1979. + struct flash_platform_data *data;
  1980. + struct device *dev = nor->dev;
  1981. + struct mtd_info *mtd = nor->mtd;
  1982. + struct device_node *np = dev->of_node;
  1983. + int ret;
  1984. + int i;
  1985. +
  1986. + ret = spi_nor_check(nor);
  1987. + if (ret)
  1988. + return ret;
  1989. +
  1990. + /* Platform data helps sort out which chip type we have, as
  1991. + * well as how this board partitions it. If we don't have
  1992. + * a chip ID, try the JEDEC id commands; they'll work for most
  1993. + * newer chips, even if we don't recognize the particular chip.
  1994. + */
  1995. + data = dev_get_platdata(dev);
  1996. + if (data && data->type) {
  1997. + const struct spi_device_id *plat_id;
  1998. +
  1999. + for (i = 0; i < ARRAY_SIZE(spi_nor_ids) - 1; i++) {
  2000. + plat_id = &spi_nor_ids[i];
  2001. + if (strcmp(data->type, plat_id->name))
  2002. + continue;
  2003. + break;
  2004. + }
  2005. +
  2006. + if (i < ARRAY_SIZE(spi_nor_ids) - 1)
  2007. + id = plat_id;
  2008. + else
  2009. + dev_warn(dev, "unrecognized id %s\n", data->type);
  2010. + }
  2011. +
  2012. + info = (void *)id->driver_data;
  2013. +
  2014. + if (info->jedec_id) {
  2015. + const struct spi_device_id *jid;
  2016. +
  2017. + jid = jedec_probe(nor);
  2018. + if (IS_ERR(jid)) {
  2019. + return PTR_ERR(jid);
  2020. + } else if (jid != id) {
  2021. + /*
  2022. + * JEDEC knows better, so overwrite platform ID. We
  2023. + * can't trust partitions any longer, but we'll let
  2024. + * mtd apply them anyway, since some partitions may be
  2025. + * marked read-only, and we don't want to lose that
  2026. + * information, even if it's not 100% accurate.
  2027. + */
  2028. + dev_warn(dev, "found %s, expected %s\n",
  2029. + jid->name, id->name);
  2030. + id = jid;
  2031. + info = (void *)jid->driver_data;
  2032. + }
  2033. + }
  2034. +
  2035. + mutex_init(&nor->lock);
  2036. +
  2037. + /*
  2038. + * Atmel, SST and Intel/Numonyx serial nor tend to power
  2039. + * up with the software protection bits set
  2040. + */
  2041. +
  2042. + if (JEDEC_MFR(info->jedec_id) == CFI_MFR_ATMEL ||
  2043. + JEDEC_MFR(info->jedec_id) == CFI_MFR_INTEL ||
  2044. + JEDEC_MFR(info->jedec_id) == CFI_MFR_SST) {
  2045. + write_enable(nor);
  2046. + write_sr(nor, 0);
  2047. + }
  2048. +
  2049. + if (data && data->name)
  2050. + mtd->name = data->name;
  2051. + else
  2052. + mtd->name = dev_name(dev);
  2053. +
  2054. + mtd->type = MTD_NORFLASH;
  2055. + mtd->writesize = 1;
  2056. + mtd->flags = MTD_CAP_NORFLASH;
  2057. + mtd->size = info->sector_size * info->n_sectors;
  2058. + mtd->_erase = spi_nor_erase;
  2059. + mtd->_read = spi_nor_read;
  2060. +
  2061. + /* nor protection support for STmicro chips */
  2062. + if (JEDEC_MFR(info->jedec_id) == CFI_MFR_ST) {
  2063. + mtd->_lock = spi_nor_lock;
  2064. + mtd->_unlock = spi_nor_unlock;
  2065. + }
  2066. +
  2067. + /* sst nor chips use AAI word program */
  2068. + if (info->flags & SST_WRITE)
  2069. + mtd->_write = sst_write;
  2070. + else
  2071. + mtd->_write = spi_nor_write;
  2072. +
  2073. + if ((info->flags & USE_FSR) &&
  2074. + nor->wait_till_ready == spi_nor_wait_till_ready)
  2075. + nor->wait_till_ready = spi_nor_wait_till_fsr_ready;
  2076. +
  2077. + /* prefer "small sector" erase if possible */
  2078. + if (info->flags & SECT_4K) {
  2079. + nor->erase_opcode = SPINOR_OP_BE_4K;
  2080. + mtd->erasesize = 4096;
  2081. + } else if (info->flags & SECT_4K_PMC) {
  2082. + nor->erase_opcode = SPINOR_OP_BE_4K_PMC;
  2083. + mtd->erasesize = 4096;
  2084. + } else {
  2085. + nor->erase_opcode = SPINOR_OP_SE;
  2086. + mtd->erasesize = info->sector_size;
  2087. + }
  2088. +
  2089. + if (info->flags & SPI_NOR_NO_ERASE)
  2090. + mtd->flags |= MTD_NO_ERASE;
  2091. +
  2092. + mtd->dev.parent = dev;
  2093. + nor->page_size = info->page_size;
  2094. + mtd->writebufsize = nor->page_size;
  2095. +
  2096. + if (np) {
  2097. + /* If we were instantiated by DT, use it */
  2098. + if (of_property_read_bool(np, "m25p,fast-read"))
  2099. + nor->flash_read = SPI_NOR_FAST;
  2100. + else
  2101. + nor->flash_read = SPI_NOR_NORMAL;
  2102. + } else {
  2103. + /* If we weren't instantiated by DT, default to fast-read */
  2104. + nor->flash_read = SPI_NOR_FAST;
  2105. + }
  2106. +
  2107. + /* Some devices cannot do fast-read, no matter what DT tells us */
  2108. + if (info->flags & SPI_NOR_NO_FR)
  2109. + nor->flash_read = SPI_NOR_NORMAL;
  2110. +
  2111. + /* Quad/Dual-read mode takes precedence over fast/normal */
  2112. + if (mode == SPI_NOR_QUAD && info->flags & SPI_NOR_QUAD_READ) {
  2113. + ret = set_quad_mode(nor, info->jedec_id);
  2114. + if (ret) {
  2115. + dev_err(dev, "quad mode not supported\n");
  2116. + return ret;
  2117. + }
  2118. + nor->flash_read = SPI_NOR_QUAD;
  2119. + } else if (mode == SPI_NOR_DUAL && info->flags & SPI_NOR_DUAL_READ) {
  2120. + nor->flash_read = SPI_NOR_DUAL;
  2121. + }
  2122. +
  2123. + /* Default commands */
  2124. + switch (nor->flash_read) {
  2125. + case SPI_NOR_QUAD:
  2126. + nor->read_opcode = SPINOR_OP_READ_1_1_4;
  2127. + break;
  2128. + case SPI_NOR_DUAL:
  2129. + nor->read_opcode = SPINOR_OP_READ_1_1_2;
  2130. + break;
  2131. + case SPI_NOR_FAST:
  2132. + nor->read_opcode = SPINOR_OP_READ_FAST;
  2133. + break;
  2134. + case SPI_NOR_NORMAL:
  2135. + nor->read_opcode = SPINOR_OP_READ;
  2136. + break;
  2137. + default:
  2138. + dev_err(dev, "No Read opcode defined\n");
  2139. + return -EINVAL;
  2140. + }
  2141. +
  2142. + nor->program_opcode = SPINOR_OP_PP;
  2143. +
  2144. + if (info->addr_width)
  2145. + nor->addr_width = info->addr_width;
  2146. + else if (mtd->size > 0x1000000) {
  2147. + /* enable 4-byte addressing if the device exceeds 16MiB */
  2148. + nor->addr_width = 4;
  2149. + if (JEDEC_MFR(info->jedec_id) == CFI_MFR_AMD) {
  2150. + /* Dedicated 4-byte command set */
  2151. + switch (nor->flash_read) {
  2152. + case SPI_NOR_QUAD:
  2153. + nor->read_opcode = SPINOR_OP_READ4_1_1_4;
  2154. + break;
  2155. + case SPI_NOR_DUAL:
  2156. + nor->read_opcode = SPINOR_OP_READ4_1_1_2;
  2157. + break;
  2158. + case SPI_NOR_FAST:
  2159. + nor->read_opcode = SPINOR_OP_READ4_FAST;
  2160. + break;
  2161. + case SPI_NOR_NORMAL:
  2162. + nor->read_opcode = SPINOR_OP_READ4;
  2163. + break;
  2164. + }
  2165. + nor->program_opcode = SPINOR_OP_PP_4B;
  2166. + /* No small sector erase for 4-byte command set */
  2167. + nor->erase_opcode = SPINOR_OP_SE_4B;
  2168. + mtd->erasesize = info->sector_size;
  2169. + } else
  2170. + set_4byte(nor, info->jedec_id, 1);
  2171. + } else {
  2172. + nor->addr_width = 3;
  2173. + }
  2174. +
  2175. + nor->read_dummy = spi_nor_read_dummy_cycles(nor);
  2176. +
  2177. + dev_info(dev, "%s (%lld Kbytes)\n", id->name,
  2178. + (long long)mtd->size >> 10);
  2179. +
  2180. + dev_dbg(dev,
  2181. + "mtd .name = %s, .size = 0x%llx (%lldMiB), "
  2182. + ".erasesize = 0x%.8x (%uKiB) .numeraseregions = %d\n",
  2183. + mtd->name, (long long)mtd->size, (long long)(mtd->size >> 20),
  2184. + mtd->erasesize, mtd->erasesize / 1024, mtd->numeraseregions);
  2185. +
  2186. + if (mtd->numeraseregions)
  2187. + for (i = 0; i < mtd->numeraseregions; i++)
  2188. + dev_dbg(dev,
  2189. + "mtd.eraseregions[%d] = { .offset = 0x%llx, "
  2190. + ".erasesize = 0x%.8x (%uKiB), "
  2191. + ".numblocks = %d }\n",
  2192. + i, (long long)mtd->eraseregions[i].offset,
  2193. + mtd->eraseregions[i].erasesize,
  2194. + mtd->eraseregions[i].erasesize / 1024,
  2195. + mtd->eraseregions[i].numblocks);
  2196. + return 0;
  2197. +}
  2198. +EXPORT_SYMBOL_GPL(spi_nor_scan);
  2199. +
  2200. +const struct spi_device_id *spi_nor_match_id(char *name)
  2201. +{
  2202. + const struct spi_device_id *id = spi_nor_ids;
  2203. +
  2204. + while (id->name[0]) {
  2205. + if (!strcmp(name, id->name))
  2206. + return id;
  2207. + id++;
  2208. + }
  2209. + return NULL;
  2210. +}
  2211. +EXPORT_SYMBOL_GPL(spi_nor_match_id);
  2212. +
  2213. +MODULE_LICENSE("GPL");
  2214. +MODULE_AUTHOR("Huang Shijie <[email protected]>");
  2215. +MODULE_AUTHOR("Mike Lavender");
  2216. +MODULE_DESCRIPTION("framework for SPI NOR");
  2217. --- /dev/null
  2218. +++ b/include/linux/mtd/spi-nor.h
  2219. @@ -0,0 +1,218 @@
  2220. +/*
  2221. + * Copyright (C) 2014 Freescale Semiconductor, Inc.
  2222. + *
  2223. + * This program is free software; you can redistribute it and/or modify
  2224. + * it under the terms of the GNU General Public License as published by
  2225. + * the Free Software Foundation; either version 2 of the License, or
  2226. + * (at your option) any later version.
  2227. + */
  2228. +
  2229. +#ifndef __LINUX_MTD_SPI_NOR_H
  2230. +#define __LINUX_MTD_SPI_NOR_H
  2231. +
  2232. +/*
  2233. + * Note on opcode nomenclature: some opcodes have a format like
  2234. + * SPINOR_OP_FUNCTION{4,}_x_y_z. The numbers x, y, and z stand for the number
  2235. + * of I/O lines used for the opcode, address, and data (respectively). The
  2236. + * FUNCTION has an optional suffix of '4', to represent an opcode which
  2237. + * requires a 4-byte (32-bit) address.
  2238. + */
  2239. +
  2240. +/* Flash opcodes. */
  2241. +#define SPINOR_OP_WREN 0x06 /* Write enable */
  2242. +#define SPINOR_OP_RDSR 0x05 /* Read status register */
  2243. +#define SPINOR_OP_WRSR 0x01 /* Write status register 1 byte */
  2244. +#define SPINOR_OP_READ 0x03 /* Read data bytes (low frequency) */
  2245. +#define SPINOR_OP_READ_FAST 0x0b /* Read data bytes (high frequency) */
  2246. +#define SPINOR_OP_READ_1_1_2 0x3b /* Read data bytes (Dual SPI) */
  2247. +#define SPINOR_OP_READ_1_1_4 0x6b /* Read data bytes (Quad SPI) */
  2248. +#define SPINOR_OP_PP 0x02 /* Page program (up to 256 bytes) */
  2249. +#define SPINOR_OP_BE_4K 0x20 /* Erase 4KiB block */
  2250. +#define SPINOR_OP_BE_4K_PMC 0xd7 /* Erase 4KiB block on PMC chips */
  2251. +#define SPINOR_OP_BE_32K 0x52 /* Erase 32KiB block */
  2252. +#define SPINOR_OP_CHIP_ERASE 0xc7 /* Erase whole flash chip */
  2253. +#define SPINOR_OP_SE 0xd8 /* Sector erase (usually 64KiB) */
  2254. +#define SPINOR_OP_RDID 0x9f /* Read JEDEC ID */
  2255. +#define SPINOR_OP_RDCR 0x35 /* Read configuration register */
  2256. +#define SPINOR_OP_RDFSR 0x70 /* Read flag status register */
  2257. +
  2258. +/* 4-byte address opcodes - used on Spansion and some Macronix flashes. */
  2259. +#define SPINOR_OP_READ4 0x13 /* Read data bytes (low frequency) */
  2260. +#define SPINOR_OP_READ4_FAST 0x0c /* Read data bytes (high frequency) */
  2261. +#define SPINOR_OP_READ4_1_1_2 0x3c /* Read data bytes (Dual SPI) */
  2262. +#define SPINOR_OP_READ4_1_1_4 0x6c /* Read data bytes (Quad SPI) */
  2263. +#define SPINOR_OP_PP_4B 0x12 /* Page program (up to 256 bytes) */
  2264. +#define SPINOR_OP_SE_4B 0xdc /* Sector erase (usually 64KiB) */
  2265. +
  2266. +/* Used for SST flashes only. */
  2267. +#define SPINOR_OP_BP 0x02 /* Byte program */
  2268. +#define SPINOR_OP_WRDI 0x04 /* Write disable */
  2269. +#define SPINOR_OP_AAI_WP 0xad /* Auto address increment word program */
  2270. +
  2271. +/* Used for Macronix and Winbond flashes. */
  2272. +#define SPINOR_OP_EN4B 0xb7 /* Enter 4-byte mode */
  2273. +#define SPINOR_OP_EX4B 0xe9 /* Exit 4-byte mode */
  2274. +
  2275. +/* Used for Spansion flashes only. */
  2276. +#define SPINOR_OP_BRWR 0x17 /* Bank register write */
  2277. +
  2278. +/* Status Register bits. */
  2279. +#define SR_WIP 1 /* Write in progress */
  2280. +#define SR_WEL 2 /* Write enable latch */
  2281. +/* meaning of other SR_* bits may differ between vendors */
  2282. +#define SR_BP0 4 /* Block protect 0 */
  2283. +#define SR_BP1 8 /* Block protect 1 */
  2284. +#define SR_BP2 0x10 /* Block protect 2 */
  2285. +#define SR_SRWD 0x80 /* SR write protect */
  2286. +
  2287. +#define SR_QUAD_EN_MX 0x40 /* Macronix Quad I/O */
  2288. +
  2289. +/* Flag Status Register bits */
  2290. +#define FSR_READY 0x80
  2291. +
  2292. +/* Configuration Register bits. */
  2293. +#define CR_QUAD_EN_SPAN 0x2 /* Spansion Quad I/O */
  2294. +
  2295. +enum read_mode {
  2296. + SPI_NOR_NORMAL = 0,
  2297. + SPI_NOR_FAST,
  2298. + SPI_NOR_DUAL,
  2299. + SPI_NOR_QUAD,
  2300. +};
  2301. +
  2302. +/**
  2303. + * struct spi_nor_xfer_cfg - Structure for defining a Serial Flash transfer
  2304. + * @wren: command for "Write Enable", or 0x00 for not required
  2305. + * @cmd: command for operation
  2306. + * @cmd_pins: number of pins to send @cmd (1, 2, 4)
  2307. + * @addr: address for operation
  2308. + * @addr_pins: number of pins to send @addr (1, 2, 4)
  2309. + * @addr_width: number of address bytes
  2310. + * (3,4, or 0 for address not required)
  2311. + * @mode: mode data
  2312. + * @mode_pins: number of pins to send @mode (1, 2, 4)
  2313. + * @mode_cycles: number of mode cycles (0 for mode not required)
  2314. + * @dummy_cycles: number of dummy cycles (0 for dummy not required)
  2315. + */
  2316. +struct spi_nor_xfer_cfg {
  2317. + u8 wren;
  2318. + u8 cmd;
  2319. + u8 cmd_pins;
  2320. + u32 addr;
  2321. + u8 addr_pins;
  2322. + u8 addr_width;
  2323. + u8 mode;
  2324. + u8 mode_pins;
  2325. + u8 mode_cycles;
  2326. + u8 dummy_cycles;
  2327. +};
  2328. +
  2329. +#define SPI_NOR_MAX_CMD_SIZE 8
  2330. +enum spi_nor_ops {
  2331. + SPI_NOR_OPS_READ = 0,
  2332. + SPI_NOR_OPS_WRITE,
  2333. + SPI_NOR_OPS_ERASE,
  2334. + SPI_NOR_OPS_LOCK,
  2335. + SPI_NOR_OPS_UNLOCK,
  2336. +};
  2337. +
  2338. +/**
  2339. + * struct spi_nor - Structure for defining a the SPI NOR layer
  2340. + * @mtd: point to a mtd_info structure
  2341. + * @lock: the lock for the read/write/erase/lock/unlock operations
  2342. + * @dev: point to a spi device, or a spi nor controller device.
  2343. + * @page_size: the page size of the SPI NOR
  2344. + * @addr_width: number of address bytes
  2345. + * @erase_opcode: the opcode for erasing a sector
  2346. + * @read_opcode: the read opcode
  2347. + * @read_dummy: the dummy needed by the read operation
  2348. + * @program_opcode: the program opcode
  2349. + * @flash_read: the mode of the read
  2350. + * @sst_write_second: used by the SST write operation
  2351. + * @cfg: used by the read_xfer/write_xfer
  2352. + * @cmd_buf: used by the write_reg
  2353. + * @prepare: [OPTIONAL] do some preparations for the
  2354. + * read/write/erase/lock/unlock operations
  2355. + * @unprepare: [OPTIONAL] do some post work after the
  2356. + * read/write/erase/lock/unlock operations
  2357. + * @read_xfer: [OPTIONAL] the read fundamental primitive
  2358. + * @write_xfer: [OPTIONAL] the writefundamental primitive
  2359. + * @read_reg: [DRIVER-SPECIFIC] read out the register
  2360. + * @write_reg: [DRIVER-SPECIFIC] write data to the register
  2361. + * @read_id: [REPLACEABLE] read out the ID data, and find
  2362. + * the proper spi_device_id
  2363. + * @wait_till_ready: [REPLACEABLE] wait till the NOR becomes ready
  2364. + * @read: [DRIVER-SPECIFIC] read data from the SPI NOR
  2365. + * @write: [DRIVER-SPECIFIC] write data to the SPI NOR
  2366. + * @erase: [DRIVER-SPECIFIC] erase a sector of the SPI NOR
  2367. + * at the offset @offs
  2368. + * @priv: the private data
  2369. + */
  2370. +struct spi_nor {
  2371. + struct mtd_info *mtd;
  2372. + struct mutex lock;
  2373. + struct device *dev;
  2374. + u32 page_size;
  2375. + u8 addr_width;
  2376. + u8 erase_opcode;
  2377. + u8 read_opcode;
  2378. + u8 read_dummy;
  2379. + u8 program_opcode;
  2380. + enum read_mode flash_read;
  2381. + bool sst_write_second;
  2382. + struct spi_nor_xfer_cfg cfg;
  2383. + u8 cmd_buf[SPI_NOR_MAX_CMD_SIZE];
  2384. +
  2385. + int (*prepare)(struct spi_nor *nor, enum spi_nor_ops ops);
  2386. + void (*unprepare)(struct spi_nor *nor, enum spi_nor_ops ops);
  2387. + int (*read_xfer)(struct spi_nor *nor, struct spi_nor_xfer_cfg *cfg,
  2388. + u8 *buf, size_t len);
  2389. + int (*write_xfer)(struct spi_nor *nor, struct spi_nor_xfer_cfg *cfg,
  2390. + u8 *buf, size_t len);
  2391. + int (*read_reg)(struct spi_nor *nor, u8 opcode, u8 *buf, int len);
  2392. + int (*write_reg)(struct spi_nor *nor, u8 opcode, u8 *buf, int len,
  2393. + int write_enable);
  2394. + const struct spi_device_id *(*read_id)(struct spi_nor *nor);
  2395. + int (*wait_till_ready)(struct spi_nor *nor);
  2396. +
  2397. + int (*read)(struct spi_nor *nor, loff_t from,
  2398. + size_t len, size_t *retlen, u_char *read_buf);
  2399. + void (*write)(struct spi_nor *nor, loff_t to,
  2400. + size_t len, size_t *retlen, const u_char *write_buf);
  2401. + int (*erase)(struct spi_nor *nor, loff_t offs);
  2402. +
  2403. + void *priv;
  2404. +};
  2405. +
  2406. +/**
  2407. + * spi_nor_scan() - scan the SPI NOR
  2408. + * @nor: the spi_nor structure
  2409. + * @id: the spi_device_id provided by the driver
  2410. + * @mode: the read mode supported by the driver
  2411. + *
  2412. + * The drivers can use this fuction to scan the SPI NOR.
  2413. + * In the scanning, it will try to get all the necessary information to
  2414. + * fill the mtd_info{} and the spi_nor{}.
  2415. + *
  2416. + * The board may assigns a spi_device_id with @id which be used to compared with
  2417. + * the spi_device_id detected by the scanning.
  2418. + *
  2419. + * Return: 0 for success, others for failure.
  2420. + */
  2421. +int spi_nor_scan(struct spi_nor *nor, const struct spi_device_id *id,
  2422. + enum read_mode mode);
  2423. +extern const struct spi_device_id spi_nor_ids[];
  2424. +
  2425. +/**
  2426. + * spi_nor_match_id() - find the spi_device_id by the name
  2427. + * @name: the name of the spi_device_id
  2428. + *
  2429. + * The drivers use this function to find the spi_device_id
  2430. + * specified by the @name.
  2431. + *
  2432. + * Return: returns the right spi_device_id pointer on success,
  2433. + * and returns NULL on failure.
  2434. + */
  2435. +const struct spi_device_id *spi_nor_match_id(char *name);
  2436. +
  2437. +#endif