brcmnand.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616
  1. /*
  2. * Copyright © 2010-2015 Broadcom Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #include <linux/clk.h>
  14. #include <linux/version.h>
  15. #include <linux/module.h>
  16. #include <linux/init.h>
  17. #include <linux/delay.h>
  18. #include <linux/device.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/err.h>
  21. #include <linux/completion.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/dma-mapping.h>
  25. #include <linux/ioport.h>
  26. #include <linux/bug.h>
  27. #include <linux/kernel.h>
  28. #include <linux/bitops.h>
  29. #include <linux/mm.h>
  30. #include <linux/mtd/mtd.h>
  31. #include <linux/mtd/rawnand.h>
  32. #include <linux/mtd/partitions.h>
  33. #include <linux/of.h>
  34. #include <linux/of_platform.h>
  35. #include <linux/slab.h>
  36. #include <linux/list.h>
  37. #include <linux/log2.h>
  38. #include "brcmnand.h"
  39. /*
  40. * This flag controls if WP stays on between erase/write commands to mitigate
  41. * flash corruption due to power glitches. Values:
  42. * 0: NAND_WP is not used or not available
  43. * 1: NAND_WP is set by default, cleared for erase/write operations
  44. * 2: NAND_WP is always cleared
  45. */
  46. static int wp_on = 1;
  47. module_param(wp_on, int, 0444);
  48. /***********************************************************************
  49. * Definitions
  50. ***********************************************************************/
  51. #define DRV_NAME "brcmnand"
  52. #define CMD_NULL 0x00
  53. #define CMD_PAGE_READ 0x01
  54. #define CMD_SPARE_AREA_READ 0x02
  55. #define CMD_STATUS_READ 0x03
  56. #define CMD_PROGRAM_PAGE 0x04
  57. #define CMD_PROGRAM_SPARE_AREA 0x05
  58. #define CMD_COPY_BACK 0x06
  59. #define CMD_DEVICE_ID_READ 0x07
  60. #define CMD_BLOCK_ERASE 0x08
  61. #define CMD_FLASH_RESET 0x09
  62. #define CMD_BLOCKS_LOCK 0x0a
  63. #define CMD_BLOCKS_LOCK_DOWN 0x0b
  64. #define CMD_BLOCKS_UNLOCK 0x0c
  65. #define CMD_READ_BLOCKS_LOCK_STATUS 0x0d
  66. #define CMD_PARAMETER_READ 0x0e
  67. #define CMD_PARAMETER_CHANGE_COL 0x0f
  68. #define CMD_LOW_LEVEL_OP 0x10
  69. struct brcm_nand_dma_desc {
  70. u32 next_desc;
  71. u32 next_desc_ext;
  72. u32 cmd_irq;
  73. u32 dram_addr;
  74. u32 dram_addr_ext;
  75. u32 tfr_len;
  76. u32 total_len;
  77. u32 flash_addr;
  78. u32 flash_addr_ext;
  79. u32 cs;
  80. u32 pad2[5];
  81. u32 status_valid;
  82. } __packed;
  83. /* Bitfields for brcm_nand_dma_desc::status_valid */
  84. #define FLASH_DMA_ECC_ERROR (1 << 8)
  85. #define FLASH_DMA_CORR_ERROR (1 << 9)
  86. /* 512B flash cache in the NAND controller HW */
  87. #define FC_SHIFT 9U
  88. #define FC_BYTES 512U
  89. #define FC_WORDS (FC_BYTES >> 2)
  90. #define BRCMNAND_MIN_PAGESIZE 512
  91. #define BRCMNAND_MIN_BLOCKSIZE (8 * 1024)
  92. #define BRCMNAND_MIN_DEVSIZE (4ULL * 1024 * 1024)
  93. #define NAND_CTRL_RDY (INTFC_CTLR_READY | INTFC_FLASH_READY)
  94. #define NAND_POLL_STATUS_TIMEOUT_MS 100
  95. /* Controller feature flags */
  96. enum {
  97. BRCMNAND_HAS_1K_SECTORS = BIT(0),
  98. BRCMNAND_HAS_PREFETCH = BIT(1),
  99. BRCMNAND_HAS_CACHE_MODE = BIT(2),
  100. BRCMNAND_HAS_WP = BIT(3),
  101. };
  102. struct brcmnand_controller {
  103. struct device *dev;
  104. struct nand_hw_control controller;
  105. void __iomem *nand_base;
  106. void __iomem *nand_fc; /* flash cache */
  107. void __iomem *flash_dma_base;
  108. unsigned int irq;
  109. unsigned int dma_irq;
  110. int nand_version;
  111. /* Some SoCs provide custom interrupt status register(s) */
  112. struct brcmnand_soc *soc;
  113. /* Some SoCs have a gateable clock for the controller */
  114. struct clk *clk;
  115. int cmd_pending;
  116. bool dma_pending;
  117. struct completion done;
  118. struct completion dma_done;
  119. /* List of NAND hosts (one for each chip-select) */
  120. struct list_head host_list;
  121. struct brcm_nand_dma_desc *dma_desc;
  122. dma_addr_t dma_pa;
  123. /* in-memory cache of the FLASH_CACHE, used only for some commands */
  124. u8 flash_cache[FC_BYTES];
  125. /* Controller revision details */
  126. const u16 *reg_offsets;
  127. unsigned int reg_spacing; /* between CS1, CS2, ... regs */
  128. const u8 *cs_offsets; /* within each chip-select */
  129. const u8 *cs0_offsets; /* within CS0, if different */
  130. unsigned int max_block_size;
  131. const unsigned int *block_sizes;
  132. unsigned int max_page_size;
  133. const unsigned int *page_sizes;
  134. unsigned int max_oob;
  135. u32 features;
  136. /* for low-power standby/resume only */
  137. u32 nand_cs_nand_select;
  138. u32 nand_cs_nand_xor;
  139. u32 corr_stat_threshold;
  140. u32 flash_dma_mode;
  141. };
  142. struct brcmnand_cfg {
  143. u64 device_size;
  144. unsigned int block_size;
  145. unsigned int page_size;
  146. unsigned int spare_area_size;
  147. unsigned int device_width;
  148. unsigned int col_adr_bytes;
  149. unsigned int blk_adr_bytes;
  150. unsigned int ful_adr_bytes;
  151. unsigned int sector_size_1k;
  152. unsigned int ecc_level;
  153. /* use for low-power standby/resume only */
  154. u32 acc_control;
  155. u32 config;
  156. u32 config_ext;
  157. u32 timing_1;
  158. u32 timing_2;
  159. };
  160. struct brcmnand_host {
  161. struct list_head node;
  162. struct nand_chip chip;
  163. struct platform_device *pdev;
  164. int cs;
  165. unsigned int last_cmd;
  166. unsigned int last_byte;
  167. u64 last_addr;
  168. struct brcmnand_cfg hwcfg;
  169. struct brcmnand_controller *ctrl;
  170. };
  171. enum brcmnand_reg {
  172. BRCMNAND_CMD_START = 0,
  173. BRCMNAND_CMD_EXT_ADDRESS,
  174. BRCMNAND_CMD_ADDRESS,
  175. BRCMNAND_INTFC_STATUS,
  176. BRCMNAND_CS_SELECT,
  177. BRCMNAND_CS_XOR,
  178. BRCMNAND_LL_OP,
  179. BRCMNAND_CS0_BASE,
  180. BRCMNAND_CS1_BASE, /* CS1 regs, if non-contiguous */
  181. BRCMNAND_CORR_THRESHOLD,
  182. BRCMNAND_CORR_THRESHOLD_EXT,
  183. BRCMNAND_UNCORR_COUNT,
  184. BRCMNAND_CORR_COUNT,
  185. BRCMNAND_CORR_EXT_ADDR,
  186. BRCMNAND_CORR_ADDR,
  187. BRCMNAND_UNCORR_EXT_ADDR,
  188. BRCMNAND_UNCORR_ADDR,
  189. BRCMNAND_SEMAPHORE,
  190. BRCMNAND_ID,
  191. BRCMNAND_ID_EXT,
  192. BRCMNAND_LL_RDATA,
  193. BRCMNAND_OOB_READ_BASE,
  194. BRCMNAND_OOB_READ_10_BASE, /* offset 0x10, if non-contiguous */
  195. BRCMNAND_OOB_WRITE_BASE,
  196. BRCMNAND_OOB_WRITE_10_BASE, /* offset 0x10, if non-contiguous */
  197. BRCMNAND_FC_BASE,
  198. };
  199. /* BRCMNAND v4.0 */
  200. static const u16 brcmnand_regs_v40[] = {
  201. [BRCMNAND_CMD_START] = 0x04,
  202. [BRCMNAND_CMD_EXT_ADDRESS] = 0x08,
  203. [BRCMNAND_CMD_ADDRESS] = 0x0c,
  204. [BRCMNAND_INTFC_STATUS] = 0x6c,
  205. [BRCMNAND_CS_SELECT] = 0x14,
  206. [BRCMNAND_CS_XOR] = 0x18,
  207. [BRCMNAND_LL_OP] = 0x178,
  208. [BRCMNAND_CS0_BASE] = 0x40,
  209. [BRCMNAND_CS1_BASE] = 0xd0,
  210. [BRCMNAND_CORR_THRESHOLD] = 0x84,
  211. [BRCMNAND_CORR_THRESHOLD_EXT] = 0,
  212. [BRCMNAND_UNCORR_COUNT] = 0,
  213. [BRCMNAND_CORR_COUNT] = 0,
  214. [BRCMNAND_CORR_EXT_ADDR] = 0x70,
  215. [BRCMNAND_CORR_ADDR] = 0x74,
  216. [BRCMNAND_UNCORR_EXT_ADDR] = 0x78,
  217. [BRCMNAND_UNCORR_ADDR] = 0x7c,
  218. [BRCMNAND_SEMAPHORE] = 0x58,
  219. [BRCMNAND_ID] = 0x60,
  220. [BRCMNAND_ID_EXT] = 0x64,
  221. [BRCMNAND_LL_RDATA] = 0x17c,
  222. [BRCMNAND_OOB_READ_BASE] = 0x20,
  223. [BRCMNAND_OOB_READ_10_BASE] = 0x130,
  224. [BRCMNAND_OOB_WRITE_BASE] = 0x30,
  225. [BRCMNAND_OOB_WRITE_10_BASE] = 0,
  226. [BRCMNAND_FC_BASE] = 0x200,
  227. };
  228. /* BRCMNAND v5.0 */
  229. static const u16 brcmnand_regs_v50[] = {
  230. [BRCMNAND_CMD_START] = 0x04,
  231. [BRCMNAND_CMD_EXT_ADDRESS] = 0x08,
  232. [BRCMNAND_CMD_ADDRESS] = 0x0c,
  233. [BRCMNAND_INTFC_STATUS] = 0x6c,
  234. [BRCMNAND_CS_SELECT] = 0x14,
  235. [BRCMNAND_CS_XOR] = 0x18,
  236. [BRCMNAND_LL_OP] = 0x178,
  237. [BRCMNAND_CS0_BASE] = 0x40,
  238. [BRCMNAND_CS1_BASE] = 0xd0,
  239. [BRCMNAND_CORR_THRESHOLD] = 0x84,
  240. [BRCMNAND_CORR_THRESHOLD_EXT] = 0,
  241. [BRCMNAND_UNCORR_COUNT] = 0,
  242. [BRCMNAND_CORR_COUNT] = 0,
  243. [BRCMNAND_CORR_EXT_ADDR] = 0x70,
  244. [BRCMNAND_CORR_ADDR] = 0x74,
  245. [BRCMNAND_UNCORR_EXT_ADDR] = 0x78,
  246. [BRCMNAND_UNCORR_ADDR] = 0x7c,
  247. [BRCMNAND_SEMAPHORE] = 0x58,
  248. [BRCMNAND_ID] = 0x60,
  249. [BRCMNAND_ID_EXT] = 0x64,
  250. [BRCMNAND_LL_RDATA] = 0x17c,
  251. [BRCMNAND_OOB_READ_BASE] = 0x20,
  252. [BRCMNAND_OOB_READ_10_BASE] = 0x130,
  253. [BRCMNAND_OOB_WRITE_BASE] = 0x30,
  254. [BRCMNAND_OOB_WRITE_10_BASE] = 0x140,
  255. [BRCMNAND_FC_BASE] = 0x200,
  256. };
  257. /* BRCMNAND v6.0 - v7.1 */
  258. static const u16 brcmnand_regs_v60[] = {
  259. [BRCMNAND_CMD_START] = 0x04,
  260. [BRCMNAND_CMD_EXT_ADDRESS] = 0x08,
  261. [BRCMNAND_CMD_ADDRESS] = 0x0c,
  262. [BRCMNAND_INTFC_STATUS] = 0x14,
  263. [BRCMNAND_CS_SELECT] = 0x18,
  264. [BRCMNAND_CS_XOR] = 0x1c,
  265. [BRCMNAND_LL_OP] = 0x20,
  266. [BRCMNAND_CS0_BASE] = 0x50,
  267. [BRCMNAND_CS1_BASE] = 0,
  268. [BRCMNAND_CORR_THRESHOLD] = 0xc0,
  269. [BRCMNAND_CORR_THRESHOLD_EXT] = 0xc4,
  270. [BRCMNAND_UNCORR_COUNT] = 0xfc,
  271. [BRCMNAND_CORR_COUNT] = 0x100,
  272. [BRCMNAND_CORR_EXT_ADDR] = 0x10c,
  273. [BRCMNAND_CORR_ADDR] = 0x110,
  274. [BRCMNAND_UNCORR_EXT_ADDR] = 0x114,
  275. [BRCMNAND_UNCORR_ADDR] = 0x118,
  276. [BRCMNAND_SEMAPHORE] = 0x150,
  277. [BRCMNAND_ID] = 0x194,
  278. [BRCMNAND_ID_EXT] = 0x198,
  279. [BRCMNAND_LL_RDATA] = 0x19c,
  280. [BRCMNAND_OOB_READ_BASE] = 0x200,
  281. [BRCMNAND_OOB_READ_10_BASE] = 0,
  282. [BRCMNAND_OOB_WRITE_BASE] = 0x280,
  283. [BRCMNAND_OOB_WRITE_10_BASE] = 0,
  284. [BRCMNAND_FC_BASE] = 0x400,
  285. };
  286. /* BRCMNAND v7.1 */
  287. static const u16 brcmnand_regs_v71[] = {
  288. [BRCMNAND_CMD_START] = 0x04,
  289. [BRCMNAND_CMD_EXT_ADDRESS] = 0x08,
  290. [BRCMNAND_CMD_ADDRESS] = 0x0c,
  291. [BRCMNAND_INTFC_STATUS] = 0x14,
  292. [BRCMNAND_CS_SELECT] = 0x18,
  293. [BRCMNAND_CS_XOR] = 0x1c,
  294. [BRCMNAND_LL_OP] = 0x20,
  295. [BRCMNAND_CS0_BASE] = 0x50,
  296. [BRCMNAND_CS1_BASE] = 0,
  297. [BRCMNAND_CORR_THRESHOLD] = 0xdc,
  298. [BRCMNAND_CORR_THRESHOLD_EXT] = 0xe0,
  299. [BRCMNAND_UNCORR_COUNT] = 0xfc,
  300. [BRCMNAND_CORR_COUNT] = 0x100,
  301. [BRCMNAND_CORR_EXT_ADDR] = 0x10c,
  302. [BRCMNAND_CORR_ADDR] = 0x110,
  303. [BRCMNAND_UNCORR_EXT_ADDR] = 0x114,
  304. [BRCMNAND_UNCORR_ADDR] = 0x118,
  305. [BRCMNAND_SEMAPHORE] = 0x150,
  306. [BRCMNAND_ID] = 0x194,
  307. [BRCMNAND_ID_EXT] = 0x198,
  308. [BRCMNAND_LL_RDATA] = 0x19c,
  309. [BRCMNAND_OOB_READ_BASE] = 0x200,
  310. [BRCMNAND_OOB_READ_10_BASE] = 0,
  311. [BRCMNAND_OOB_WRITE_BASE] = 0x280,
  312. [BRCMNAND_OOB_WRITE_10_BASE] = 0,
  313. [BRCMNAND_FC_BASE] = 0x400,
  314. };
  315. /* BRCMNAND v7.2 */
  316. static const u16 brcmnand_regs_v72[] = {
  317. [BRCMNAND_CMD_START] = 0x04,
  318. [BRCMNAND_CMD_EXT_ADDRESS] = 0x08,
  319. [BRCMNAND_CMD_ADDRESS] = 0x0c,
  320. [BRCMNAND_INTFC_STATUS] = 0x14,
  321. [BRCMNAND_CS_SELECT] = 0x18,
  322. [BRCMNAND_CS_XOR] = 0x1c,
  323. [BRCMNAND_LL_OP] = 0x20,
  324. [BRCMNAND_CS0_BASE] = 0x50,
  325. [BRCMNAND_CS1_BASE] = 0,
  326. [BRCMNAND_CORR_THRESHOLD] = 0xdc,
  327. [BRCMNAND_CORR_THRESHOLD_EXT] = 0xe0,
  328. [BRCMNAND_UNCORR_COUNT] = 0xfc,
  329. [BRCMNAND_CORR_COUNT] = 0x100,
  330. [BRCMNAND_CORR_EXT_ADDR] = 0x10c,
  331. [BRCMNAND_CORR_ADDR] = 0x110,
  332. [BRCMNAND_UNCORR_EXT_ADDR] = 0x114,
  333. [BRCMNAND_UNCORR_ADDR] = 0x118,
  334. [BRCMNAND_SEMAPHORE] = 0x150,
  335. [BRCMNAND_ID] = 0x194,
  336. [BRCMNAND_ID_EXT] = 0x198,
  337. [BRCMNAND_LL_RDATA] = 0x19c,
  338. [BRCMNAND_OOB_READ_BASE] = 0x200,
  339. [BRCMNAND_OOB_READ_10_BASE] = 0,
  340. [BRCMNAND_OOB_WRITE_BASE] = 0x400,
  341. [BRCMNAND_OOB_WRITE_10_BASE] = 0,
  342. [BRCMNAND_FC_BASE] = 0x600,
  343. };
  344. enum brcmnand_cs_reg {
  345. BRCMNAND_CS_CFG_EXT = 0,
  346. BRCMNAND_CS_CFG,
  347. BRCMNAND_CS_ACC_CONTROL,
  348. BRCMNAND_CS_TIMING1,
  349. BRCMNAND_CS_TIMING2,
  350. };
  351. /* Per chip-select offsets for v7.1 */
  352. static const u8 brcmnand_cs_offsets_v71[] = {
  353. [BRCMNAND_CS_ACC_CONTROL] = 0x00,
  354. [BRCMNAND_CS_CFG_EXT] = 0x04,
  355. [BRCMNAND_CS_CFG] = 0x08,
  356. [BRCMNAND_CS_TIMING1] = 0x0c,
  357. [BRCMNAND_CS_TIMING2] = 0x10,
  358. };
  359. /* Per chip-select offsets for pre v7.1, except CS0 on <= v5.0 */
  360. static const u8 brcmnand_cs_offsets[] = {
  361. [BRCMNAND_CS_ACC_CONTROL] = 0x00,
  362. [BRCMNAND_CS_CFG_EXT] = 0x04,
  363. [BRCMNAND_CS_CFG] = 0x04,
  364. [BRCMNAND_CS_TIMING1] = 0x08,
  365. [BRCMNAND_CS_TIMING2] = 0x0c,
  366. };
  367. /* Per chip-select offset for <= v5.0 on CS0 only */
  368. static const u8 brcmnand_cs_offsets_cs0[] = {
  369. [BRCMNAND_CS_ACC_CONTROL] = 0x00,
  370. [BRCMNAND_CS_CFG_EXT] = 0x08,
  371. [BRCMNAND_CS_CFG] = 0x08,
  372. [BRCMNAND_CS_TIMING1] = 0x10,
  373. [BRCMNAND_CS_TIMING2] = 0x14,
  374. };
  375. /*
  376. * Bitfields for the CFG and CFG_EXT registers. Pre-v7.1 controllers only had
  377. * one config register, but once the bitfields overflowed, newer controllers
  378. * (v7.1 and newer) added a CFG_EXT register and shuffled a few fields around.
  379. */
  380. enum {
  381. CFG_BLK_ADR_BYTES_SHIFT = 8,
  382. CFG_COL_ADR_BYTES_SHIFT = 12,
  383. CFG_FUL_ADR_BYTES_SHIFT = 16,
  384. CFG_BUS_WIDTH_SHIFT = 23,
  385. CFG_BUS_WIDTH = BIT(CFG_BUS_WIDTH_SHIFT),
  386. CFG_DEVICE_SIZE_SHIFT = 24,
  387. /* Only for pre-v7.1 (with no CFG_EXT register) */
  388. CFG_PAGE_SIZE_SHIFT = 20,
  389. CFG_BLK_SIZE_SHIFT = 28,
  390. /* Only for v7.1+ (with CFG_EXT register) */
  391. CFG_EXT_PAGE_SIZE_SHIFT = 0,
  392. CFG_EXT_BLK_SIZE_SHIFT = 4,
  393. };
  394. /* BRCMNAND_INTFC_STATUS */
  395. enum {
  396. INTFC_FLASH_STATUS = GENMASK(7, 0),
  397. INTFC_ERASED = BIT(27),
  398. INTFC_OOB_VALID = BIT(28),
  399. INTFC_CACHE_VALID = BIT(29),
  400. INTFC_FLASH_READY = BIT(30),
  401. INTFC_CTLR_READY = BIT(31),
  402. };
  403. static inline u32 nand_readreg(struct brcmnand_controller *ctrl, u32 offs)
  404. {
  405. return brcmnand_readl(ctrl->nand_base + offs);
  406. }
  407. static inline void nand_writereg(struct brcmnand_controller *ctrl, u32 offs,
  408. u32 val)
  409. {
  410. brcmnand_writel(val, ctrl->nand_base + offs);
  411. }
  412. static int brcmnand_revision_init(struct brcmnand_controller *ctrl)
  413. {
  414. static const unsigned int block_sizes_v6[] = { 8, 16, 128, 256, 512, 1024, 2048, 0 };
  415. static const unsigned int block_sizes_v4[] = { 16, 128, 8, 512, 256, 1024, 2048, 0 };
  416. static const unsigned int page_sizes[] = { 512, 2048, 4096, 8192, 0 };
  417. ctrl->nand_version = nand_readreg(ctrl, 0) & 0xffff;
  418. /* Only support v4.0+? */
  419. if (ctrl->nand_version < 0x0400) {
  420. dev_err(ctrl->dev, "version %#x not supported\n",
  421. ctrl->nand_version);
  422. return -ENODEV;
  423. }
  424. /* Register offsets */
  425. if (ctrl->nand_version >= 0x0702)
  426. ctrl->reg_offsets = brcmnand_regs_v72;
  427. else if (ctrl->nand_version >= 0x0701)
  428. ctrl->reg_offsets = brcmnand_regs_v71;
  429. else if (ctrl->nand_version >= 0x0600)
  430. ctrl->reg_offsets = brcmnand_regs_v60;
  431. else if (ctrl->nand_version >= 0x0500)
  432. ctrl->reg_offsets = brcmnand_regs_v50;
  433. else if (ctrl->nand_version >= 0x0400)
  434. ctrl->reg_offsets = brcmnand_regs_v40;
  435. /* Chip-select stride */
  436. if (ctrl->nand_version >= 0x0701)
  437. ctrl->reg_spacing = 0x14;
  438. else
  439. ctrl->reg_spacing = 0x10;
  440. /* Per chip-select registers */
  441. if (ctrl->nand_version >= 0x0701) {
  442. ctrl->cs_offsets = brcmnand_cs_offsets_v71;
  443. } else {
  444. ctrl->cs_offsets = brcmnand_cs_offsets;
  445. /* v3.3-5.0 have a different CS0 offset layout */
  446. if (ctrl->nand_version >= 0x0303 &&
  447. ctrl->nand_version <= 0x0500)
  448. ctrl->cs0_offsets = brcmnand_cs_offsets_cs0;
  449. }
  450. /* Page / block sizes */
  451. if (ctrl->nand_version >= 0x0701) {
  452. /* >= v7.1 use nice power-of-2 values! */
  453. ctrl->max_page_size = 16 * 1024;
  454. ctrl->max_block_size = 2 * 1024 * 1024;
  455. } else {
  456. ctrl->page_sizes = page_sizes;
  457. if (ctrl->nand_version >= 0x0600)
  458. ctrl->block_sizes = block_sizes_v6;
  459. else
  460. ctrl->block_sizes = block_sizes_v4;
  461. if (ctrl->nand_version < 0x0400) {
  462. ctrl->max_page_size = 4096;
  463. ctrl->max_block_size = 512 * 1024;
  464. }
  465. }
  466. /* Maximum spare area sector size (per 512B) */
  467. if (ctrl->nand_version >= 0x0702)
  468. ctrl->max_oob = 128;
  469. else if (ctrl->nand_version >= 0x0600)
  470. ctrl->max_oob = 64;
  471. else if (ctrl->nand_version >= 0x0500)
  472. ctrl->max_oob = 32;
  473. else
  474. ctrl->max_oob = 16;
  475. /* v6.0 and newer (except v6.1) have prefetch support */
  476. if (ctrl->nand_version >= 0x0600 && ctrl->nand_version != 0x0601)
  477. ctrl->features |= BRCMNAND_HAS_PREFETCH;
  478. /*
  479. * v6.x has cache mode, but it's implemented differently. Ignore it for
  480. * now.
  481. */
  482. if (ctrl->nand_version >= 0x0700)
  483. ctrl->features |= BRCMNAND_HAS_CACHE_MODE;
  484. if (ctrl->nand_version >= 0x0500)
  485. ctrl->features |= BRCMNAND_HAS_1K_SECTORS;
  486. if (ctrl->nand_version >= 0x0700)
  487. ctrl->features |= BRCMNAND_HAS_WP;
  488. else if (of_property_read_bool(ctrl->dev->of_node, "brcm,nand-has-wp"))
  489. ctrl->features |= BRCMNAND_HAS_WP;
  490. return 0;
  491. }
  492. static inline u32 brcmnand_read_reg(struct brcmnand_controller *ctrl,
  493. enum brcmnand_reg reg)
  494. {
  495. u16 offs = ctrl->reg_offsets[reg];
  496. if (offs)
  497. return nand_readreg(ctrl, offs);
  498. else
  499. return 0;
  500. }
  501. static inline void brcmnand_write_reg(struct brcmnand_controller *ctrl,
  502. enum brcmnand_reg reg, u32 val)
  503. {
  504. u16 offs = ctrl->reg_offsets[reg];
  505. if (offs)
  506. nand_writereg(ctrl, offs, val);
  507. }
  508. static inline void brcmnand_rmw_reg(struct brcmnand_controller *ctrl,
  509. enum brcmnand_reg reg, u32 mask, unsigned
  510. int shift, u32 val)
  511. {
  512. u32 tmp = brcmnand_read_reg(ctrl, reg);
  513. tmp &= ~mask;
  514. tmp |= val << shift;
  515. brcmnand_write_reg(ctrl, reg, tmp);
  516. }
  517. static inline u32 brcmnand_read_fc(struct brcmnand_controller *ctrl, int word)
  518. {
  519. return __raw_readl(ctrl->nand_fc + word * 4);
  520. }
  521. static inline void brcmnand_write_fc(struct brcmnand_controller *ctrl,
  522. int word, u32 val)
  523. {
  524. __raw_writel(val, ctrl->nand_fc + word * 4);
  525. }
  526. static inline u16 brcmnand_cs_offset(struct brcmnand_controller *ctrl, int cs,
  527. enum brcmnand_cs_reg reg)
  528. {
  529. u16 offs_cs0 = ctrl->reg_offsets[BRCMNAND_CS0_BASE];
  530. u16 offs_cs1 = ctrl->reg_offsets[BRCMNAND_CS1_BASE];
  531. u8 cs_offs;
  532. if (cs == 0 && ctrl->cs0_offsets)
  533. cs_offs = ctrl->cs0_offsets[reg];
  534. else
  535. cs_offs = ctrl->cs_offsets[reg];
  536. if (cs && offs_cs1)
  537. return offs_cs1 + (cs - 1) * ctrl->reg_spacing + cs_offs;
  538. return offs_cs0 + cs * ctrl->reg_spacing + cs_offs;
  539. }
  540. static inline u32 brcmnand_count_corrected(struct brcmnand_controller *ctrl)
  541. {
  542. if (ctrl->nand_version < 0x0600)
  543. return 1;
  544. return brcmnand_read_reg(ctrl, BRCMNAND_CORR_COUNT);
  545. }
  546. static void brcmnand_wr_corr_thresh(struct brcmnand_host *host, u8 val)
  547. {
  548. struct brcmnand_controller *ctrl = host->ctrl;
  549. unsigned int shift = 0, bits;
  550. enum brcmnand_reg reg = BRCMNAND_CORR_THRESHOLD;
  551. int cs = host->cs;
  552. if (ctrl->nand_version >= 0x0702)
  553. bits = 7;
  554. else if (ctrl->nand_version >= 0x0600)
  555. bits = 6;
  556. else if (ctrl->nand_version >= 0x0500)
  557. bits = 5;
  558. else
  559. bits = 4;
  560. if (ctrl->nand_version >= 0x0702) {
  561. if (cs >= 4)
  562. reg = BRCMNAND_CORR_THRESHOLD_EXT;
  563. shift = (cs % 4) * bits;
  564. } else if (ctrl->nand_version >= 0x0600) {
  565. if (cs >= 5)
  566. reg = BRCMNAND_CORR_THRESHOLD_EXT;
  567. shift = (cs % 5) * bits;
  568. }
  569. brcmnand_rmw_reg(ctrl, reg, (bits - 1) << shift, shift, val);
  570. }
  571. static inline int brcmnand_cmd_shift(struct brcmnand_controller *ctrl)
  572. {
  573. if (ctrl->nand_version < 0x0602)
  574. return 24;
  575. return 0;
  576. }
  577. /***********************************************************************
  578. * NAND ACC CONTROL bitfield
  579. *
  580. * Some bits have remained constant throughout hardware revision, while
  581. * others have shifted around.
  582. ***********************************************************************/
  583. /* Constant for all versions (where supported) */
  584. enum {
  585. /* See BRCMNAND_HAS_CACHE_MODE */
  586. ACC_CONTROL_CACHE_MODE = BIT(22),
  587. /* See BRCMNAND_HAS_PREFETCH */
  588. ACC_CONTROL_PREFETCH = BIT(23),
  589. ACC_CONTROL_PAGE_HIT = BIT(24),
  590. ACC_CONTROL_WR_PREEMPT = BIT(25),
  591. ACC_CONTROL_PARTIAL_PAGE = BIT(26),
  592. ACC_CONTROL_RD_ERASED = BIT(27),
  593. ACC_CONTROL_FAST_PGM_RDIN = BIT(28),
  594. ACC_CONTROL_WR_ECC = BIT(30),
  595. ACC_CONTROL_RD_ECC = BIT(31),
  596. };
  597. static inline u32 brcmnand_spare_area_mask(struct brcmnand_controller *ctrl)
  598. {
  599. if (ctrl->nand_version >= 0x0702)
  600. return GENMASK(7, 0);
  601. else if (ctrl->nand_version >= 0x0600)
  602. return GENMASK(6, 0);
  603. else
  604. return GENMASK(5, 0);
  605. }
  606. #define NAND_ACC_CONTROL_ECC_SHIFT 16
  607. #define NAND_ACC_CONTROL_ECC_EXT_SHIFT 13
  608. static inline u32 brcmnand_ecc_level_mask(struct brcmnand_controller *ctrl)
  609. {
  610. u32 mask = (ctrl->nand_version >= 0x0600) ? 0x1f : 0x0f;
  611. mask <<= NAND_ACC_CONTROL_ECC_SHIFT;
  612. /* v7.2 includes additional ECC levels */
  613. if (ctrl->nand_version >= 0x0702)
  614. mask |= 0x7 << NAND_ACC_CONTROL_ECC_EXT_SHIFT;
  615. return mask;
  616. }
  617. static void brcmnand_set_ecc_enabled(struct brcmnand_host *host, int en)
  618. {
  619. struct brcmnand_controller *ctrl = host->ctrl;
  620. u16 offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_ACC_CONTROL);
  621. u32 acc_control = nand_readreg(ctrl, offs);
  622. u32 ecc_flags = ACC_CONTROL_WR_ECC | ACC_CONTROL_RD_ECC;
  623. if (en) {
  624. acc_control |= ecc_flags; /* enable RD/WR ECC */
  625. acc_control |= host->hwcfg.ecc_level
  626. << NAND_ACC_CONTROL_ECC_SHIFT;
  627. } else {
  628. acc_control &= ~ecc_flags; /* disable RD/WR ECC */
  629. acc_control &= ~brcmnand_ecc_level_mask(ctrl);
  630. }
  631. nand_writereg(ctrl, offs, acc_control);
  632. }
  633. static inline int brcmnand_sector_1k_shift(struct brcmnand_controller *ctrl)
  634. {
  635. if (ctrl->nand_version >= 0x0702)
  636. return 9;
  637. else if (ctrl->nand_version >= 0x0600)
  638. return 7;
  639. else if (ctrl->nand_version >= 0x0500)
  640. return 6;
  641. else
  642. return -1;
  643. }
  644. static int brcmnand_get_sector_size_1k(struct brcmnand_host *host)
  645. {
  646. struct brcmnand_controller *ctrl = host->ctrl;
  647. int shift = brcmnand_sector_1k_shift(ctrl);
  648. u16 acc_control_offs = brcmnand_cs_offset(ctrl, host->cs,
  649. BRCMNAND_CS_ACC_CONTROL);
  650. if (shift < 0)
  651. return 0;
  652. return (nand_readreg(ctrl, acc_control_offs) >> shift) & 0x1;
  653. }
  654. static void brcmnand_set_sector_size_1k(struct brcmnand_host *host, int val)
  655. {
  656. struct brcmnand_controller *ctrl = host->ctrl;
  657. int shift = brcmnand_sector_1k_shift(ctrl);
  658. u16 acc_control_offs = brcmnand_cs_offset(ctrl, host->cs,
  659. BRCMNAND_CS_ACC_CONTROL);
  660. u32 tmp;
  661. if (shift < 0)
  662. return;
  663. tmp = nand_readreg(ctrl, acc_control_offs);
  664. tmp &= ~(1 << shift);
  665. tmp |= (!!val) << shift;
  666. nand_writereg(ctrl, acc_control_offs, tmp);
  667. }
  668. /***********************************************************************
  669. * CS_NAND_SELECT
  670. ***********************************************************************/
  671. enum {
  672. CS_SELECT_NAND_WP = BIT(29),
  673. CS_SELECT_AUTO_DEVICE_ID_CFG = BIT(30),
  674. };
  675. static int bcmnand_ctrl_poll_status(struct brcmnand_controller *ctrl,
  676. u32 mask, u32 expected_val,
  677. unsigned long timeout_ms)
  678. {
  679. unsigned long limit;
  680. u32 val;
  681. if (!timeout_ms)
  682. timeout_ms = NAND_POLL_STATUS_TIMEOUT_MS;
  683. limit = jiffies + msecs_to_jiffies(timeout_ms);
  684. do {
  685. val = brcmnand_read_reg(ctrl, BRCMNAND_INTFC_STATUS);
  686. if ((val & mask) == expected_val)
  687. return 0;
  688. cpu_relax();
  689. } while (time_after(limit, jiffies));
  690. dev_warn(ctrl->dev, "timeout on status poll (expected %x got %x)\n",
  691. expected_val, val & mask);
  692. return -ETIMEDOUT;
  693. }
  694. static inline void brcmnand_set_wp(struct brcmnand_controller *ctrl, bool en)
  695. {
  696. u32 val = en ? CS_SELECT_NAND_WP : 0;
  697. brcmnand_rmw_reg(ctrl, BRCMNAND_CS_SELECT, CS_SELECT_NAND_WP, 0, val);
  698. }
  699. /***********************************************************************
  700. * Flash DMA
  701. ***********************************************************************/
  702. enum flash_dma_reg {
  703. FLASH_DMA_REVISION = 0x00,
  704. FLASH_DMA_FIRST_DESC = 0x04,
  705. FLASH_DMA_FIRST_DESC_EXT = 0x08,
  706. FLASH_DMA_CTRL = 0x0c,
  707. FLASH_DMA_MODE = 0x10,
  708. FLASH_DMA_STATUS = 0x14,
  709. FLASH_DMA_INTERRUPT_DESC = 0x18,
  710. FLASH_DMA_INTERRUPT_DESC_EXT = 0x1c,
  711. FLASH_DMA_ERROR_STATUS = 0x20,
  712. FLASH_DMA_CURRENT_DESC = 0x24,
  713. FLASH_DMA_CURRENT_DESC_EXT = 0x28,
  714. };
  715. static inline bool has_flash_dma(struct brcmnand_controller *ctrl)
  716. {
  717. return ctrl->flash_dma_base;
  718. }
  719. static inline bool flash_dma_buf_ok(const void *buf)
  720. {
  721. return buf && !is_vmalloc_addr(buf) &&
  722. likely(IS_ALIGNED((uintptr_t)buf, 4));
  723. }
  724. static inline void flash_dma_writel(struct brcmnand_controller *ctrl, u8 offs,
  725. u32 val)
  726. {
  727. brcmnand_writel(val, ctrl->flash_dma_base + offs);
  728. }
  729. static inline u32 flash_dma_readl(struct brcmnand_controller *ctrl, u8 offs)
  730. {
  731. return brcmnand_readl(ctrl->flash_dma_base + offs);
  732. }
  733. /* Low-level operation types: command, address, write, or read */
  734. enum brcmnand_llop_type {
  735. LL_OP_CMD,
  736. LL_OP_ADDR,
  737. LL_OP_WR,
  738. LL_OP_RD,
  739. };
  740. /***********************************************************************
  741. * Internal support functions
  742. ***********************************************************************/
  743. static inline bool is_hamming_ecc(struct brcmnand_controller *ctrl,
  744. struct brcmnand_cfg *cfg)
  745. {
  746. if (ctrl->nand_version <= 0x0701)
  747. return cfg->sector_size_1k == 0 && cfg->spare_area_size == 16 &&
  748. cfg->ecc_level == 15;
  749. else
  750. return cfg->sector_size_1k == 0 && ((cfg->spare_area_size == 16 &&
  751. cfg->ecc_level == 15) ||
  752. (cfg->spare_area_size == 28 && cfg->ecc_level == 16));
  753. }
  754. /*
  755. * Set mtd->ooblayout to the appropriate mtd_ooblayout_ops given
  756. * the layout/configuration.
  757. * Returns -ERRCODE on failure.
  758. */
  759. static int brcmnand_hamming_ooblayout_ecc(struct mtd_info *mtd, int section,
  760. struct mtd_oob_region *oobregion)
  761. {
  762. struct nand_chip *chip = mtd_to_nand(mtd);
  763. struct brcmnand_host *host = nand_get_controller_data(chip);
  764. struct brcmnand_cfg *cfg = &host->hwcfg;
  765. int sas = cfg->spare_area_size << cfg->sector_size_1k;
  766. int sectors = cfg->page_size / (512 << cfg->sector_size_1k);
  767. if (section >= sectors)
  768. return -ERANGE;
  769. oobregion->offset = (section * sas) + 6;
  770. oobregion->length = 3;
  771. return 0;
  772. }
  773. static int brcmnand_hamming_ooblayout_free(struct mtd_info *mtd, int section,
  774. struct mtd_oob_region *oobregion)
  775. {
  776. struct nand_chip *chip = mtd_to_nand(mtd);
  777. struct brcmnand_host *host = nand_get_controller_data(chip);
  778. struct brcmnand_cfg *cfg = &host->hwcfg;
  779. int sas = cfg->spare_area_size << cfg->sector_size_1k;
  780. int sectors = cfg->page_size / (512 << cfg->sector_size_1k);
  781. if (section >= sectors * 2)
  782. return -ERANGE;
  783. oobregion->offset = (section / 2) * sas;
  784. if (section & 1) {
  785. oobregion->offset += 9;
  786. oobregion->length = 7;
  787. } else {
  788. oobregion->length = 6;
  789. /* First sector of each page may have BBI */
  790. if (!section) {
  791. /*
  792. * Small-page NAND use byte 6 for BBI while large-page
  793. * NAND use bytes 0 and 1.
  794. */
  795. if (cfg->page_size > 512) {
  796. oobregion->offset += 2;
  797. oobregion->length -= 2;
  798. } else {
  799. oobregion->length--;
  800. }
  801. }
  802. }
  803. return 0;
  804. }
  805. static const struct mtd_ooblayout_ops brcmnand_hamming_ooblayout_ops = {
  806. .ecc = brcmnand_hamming_ooblayout_ecc,
  807. .free = brcmnand_hamming_ooblayout_free,
  808. };
  809. static int brcmnand_bch_ooblayout_ecc(struct mtd_info *mtd, int section,
  810. struct mtd_oob_region *oobregion)
  811. {
  812. struct nand_chip *chip = mtd_to_nand(mtd);
  813. struct brcmnand_host *host = nand_get_controller_data(chip);
  814. struct brcmnand_cfg *cfg = &host->hwcfg;
  815. int sas = cfg->spare_area_size << cfg->sector_size_1k;
  816. int sectors = cfg->page_size / (512 << cfg->sector_size_1k);
  817. if (section >= sectors)
  818. return -ERANGE;
  819. oobregion->offset = (section * (sas + 1)) - chip->ecc.bytes;
  820. oobregion->length = chip->ecc.bytes;
  821. return 0;
  822. }
  823. static int brcmnand_bch_ooblayout_free_lp(struct mtd_info *mtd, int section,
  824. struct mtd_oob_region *oobregion)
  825. {
  826. struct nand_chip *chip = mtd_to_nand(mtd);
  827. struct brcmnand_host *host = nand_get_controller_data(chip);
  828. struct brcmnand_cfg *cfg = &host->hwcfg;
  829. int sas = cfg->spare_area_size << cfg->sector_size_1k;
  830. int sectors = cfg->page_size / (512 << cfg->sector_size_1k);
  831. if (section >= sectors)
  832. return -ERANGE;
  833. if (sas <= chip->ecc.bytes)
  834. return 0;
  835. oobregion->offset = section * sas;
  836. oobregion->length = sas - chip->ecc.bytes;
  837. if (!section) {
  838. oobregion->offset++;
  839. oobregion->length--;
  840. }
  841. return 0;
  842. }
  843. static int brcmnand_bch_ooblayout_free_sp(struct mtd_info *mtd, int section,
  844. struct mtd_oob_region *oobregion)
  845. {
  846. struct nand_chip *chip = mtd_to_nand(mtd);
  847. struct brcmnand_host *host = nand_get_controller_data(chip);
  848. struct brcmnand_cfg *cfg = &host->hwcfg;
  849. int sas = cfg->spare_area_size << cfg->sector_size_1k;
  850. if (section > 1 || sas - chip->ecc.bytes < 6 ||
  851. (section && sas - chip->ecc.bytes == 6))
  852. return -ERANGE;
  853. if (!section) {
  854. oobregion->offset = 0;
  855. oobregion->length = 5;
  856. } else {
  857. oobregion->offset = 6;
  858. oobregion->length = sas - chip->ecc.bytes - 6;
  859. }
  860. return 0;
  861. }
  862. static const struct mtd_ooblayout_ops brcmnand_bch_lp_ooblayout_ops = {
  863. .ecc = brcmnand_bch_ooblayout_ecc,
  864. .free = brcmnand_bch_ooblayout_free_lp,
  865. };
  866. static const struct mtd_ooblayout_ops brcmnand_bch_sp_ooblayout_ops = {
  867. .ecc = brcmnand_bch_ooblayout_ecc,
  868. .free = brcmnand_bch_ooblayout_free_sp,
  869. };
  870. static int brcmstb_choose_ecc_layout(struct brcmnand_host *host)
  871. {
  872. struct brcmnand_cfg *p = &host->hwcfg;
  873. struct mtd_info *mtd = nand_to_mtd(&host->chip);
  874. struct nand_ecc_ctrl *ecc = &host->chip.ecc;
  875. unsigned int ecc_level = p->ecc_level;
  876. int sas = p->spare_area_size << p->sector_size_1k;
  877. int sectors = p->page_size / (512 << p->sector_size_1k);
  878. if (p->sector_size_1k)
  879. ecc_level <<= 1;
  880. if (is_hamming_ecc(host->ctrl, p)) {
  881. ecc->bytes = 3 * sectors;
  882. mtd_set_ooblayout(mtd, &brcmnand_hamming_ooblayout_ops);
  883. return 0;
  884. }
  885. /*
  886. * CONTROLLER_VERSION:
  887. * < v5.0: ECC_REQ = ceil(BCH_T * 13/8)
  888. * >= v5.0: ECC_REQ = ceil(BCH_T * 14/8)
  889. * But we will just be conservative.
  890. */
  891. ecc->bytes = DIV_ROUND_UP(ecc_level * 14, 8);
  892. if (p->page_size == 512)
  893. mtd_set_ooblayout(mtd, &brcmnand_bch_sp_ooblayout_ops);
  894. else
  895. mtd_set_ooblayout(mtd, &brcmnand_bch_lp_ooblayout_ops);
  896. if (ecc->bytes >= sas) {
  897. dev_err(&host->pdev->dev,
  898. "error: ECC too large for OOB (ECC bytes %d, spare sector %d)\n",
  899. ecc->bytes, sas);
  900. return -EINVAL;
  901. }
  902. return 0;
  903. }
  904. static void brcmnand_wp(struct mtd_info *mtd, int wp)
  905. {
  906. struct nand_chip *chip = mtd_to_nand(mtd);
  907. struct brcmnand_host *host = nand_get_controller_data(chip);
  908. struct brcmnand_controller *ctrl = host->ctrl;
  909. if ((ctrl->features & BRCMNAND_HAS_WP) && wp_on == 1) {
  910. static int old_wp = -1;
  911. int ret;
  912. if (old_wp != wp) {
  913. dev_dbg(ctrl->dev, "WP %s\n", wp ? "on" : "off");
  914. old_wp = wp;
  915. }
  916. /*
  917. * make sure ctrl/flash ready before and after
  918. * changing state of #WP pin
  919. */
  920. ret = bcmnand_ctrl_poll_status(ctrl, NAND_CTRL_RDY |
  921. NAND_STATUS_READY,
  922. NAND_CTRL_RDY |
  923. NAND_STATUS_READY, 0);
  924. if (ret)
  925. return;
  926. brcmnand_set_wp(ctrl, wp);
  927. chip->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
  928. /* NAND_STATUS_WP 0x00 = protected, 0x80 = not protected */
  929. ret = bcmnand_ctrl_poll_status(ctrl,
  930. NAND_CTRL_RDY |
  931. NAND_STATUS_READY |
  932. NAND_STATUS_WP,
  933. NAND_CTRL_RDY |
  934. NAND_STATUS_READY |
  935. (wp ? 0 : NAND_STATUS_WP), 0);
  936. if (ret)
  937. dev_err_ratelimited(&host->pdev->dev,
  938. "nand #WP expected %s\n",
  939. wp ? "on" : "off");
  940. }
  941. }
  942. /* Helper functions for reading and writing OOB registers */
  943. static inline u8 oob_reg_read(struct brcmnand_controller *ctrl, u32 offs)
  944. {
  945. u16 offset0, offset10, reg_offs;
  946. offset0 = ctrl->reg_offsets[BRCMNAND_OOB_READ_BASE];
  947. offset10 = ctrl->reg_offsets[BRCMNAND_OOB_READ_10_BASE];
  948. if (offs >= ctrl->max_oob)
  949. return 0x77;
  950. if (offs >= 16 && offset10)
  951. reg_offs = offset10 + ((offs - 0x10) & ~0x03);
  952. else
  953. reg_offs = offset0 + (offs & ~0x03);
  954. return nand_readreg(ctrl, reg_offs) >> (24 - ((offs & 0x03) << 3));
  955. }
  956. static inline void oob_reg_write(struct brcmnand_controller *ctrl, u32 offs,
  957. u32 data)
  958. {
  959. u16 offset0, offset10, reg_offs;
  960. offset0 = ctrl->reg_offsets[BRCMNAND_OOB_WRITE_BASE];
  961. offset10 = ctrl->reg_offsets[BRCMNAND_OOB_WRITE_10_BASE];
  962. if (offs >= ctrl->max_oob)
  963. return;
  964. if (offs >= 16 && offset10)
  965. reg_offs = offset10 + ((offs - 0x10) & ~0x03);
  966. else
  967. reg_offs = offset0 + (offs & ~0x03);
  968. nand_writereg(ctrl, reg_offs, data);
  969. }
  970. /*
  971. * read_oob_from_regs - read data from OOB registers
  972. * @ctrl: NAND controller
  973. * @i: sub-page sector index
  974. * @oob: buffer to read to
  975. * @sas: spare area sector size (i.e., OOB size per FLASH_CACHE)
  976. * @sector_1k: 1 for 1KiB sectors, 0 for 512B, other values are illegal
  977. */
  978. static int read_oob_from_regs(struct brcmnand_controller *ctrl, int i, u8 *oob,
  979. int sas, int sector_1k)
  980. {
  981. int tbytes = sas << sector_1k;
  982. int j;
  983. /* Adjust OOB values for 1K sector size */
  984. if (sector_1k && (i & 0x01))
  985. tbytes = max(0, tbytes - (int)ctrl->max_oob);
  986. tbytes = min_t(int, tbytes, ctrl->max_oob);
  987. for (j = 0; j < tbytes; j++)
  988. oob[j] = oob_reg_read(ctrl, j);
  989. return tbytes;
  990. }
  991. /*
  992. * write_oob_to_regs - write data to OOB registers
  993. * @i: sub-page sector index
  994. * @oob: buffer to write from
  995. * @sas: spare area sector size (i.e., OOB size per FLASH_CACHE)
  996. * @sector_1k: 1 for 1KiB sectors, 0 for 512B, other values are illegal
  997. */
  998. static int write_oob_to_regs(struct brcmnand_controller *ctrl, int i,
  999. const u8 *oob, int sas, int sector_1k)
  1000. {
  1001. int tbytes = sas << sector_1k;
  1002. int j;
  1003. /* Adjust OOB values for 1K sector size */
  1004. if (sector_1k && (i & 0x01))
  1005. tbytes = max(0, tbytes - (int)ctrl->max_oob);
  1006. tbytes = min_t(int, tbytes, ctrl->max_oob);
  1007. for (j = 0; j < tbytes; j += 4)
  1008. oob_reg_write(ctrl, j,
  1009. (oob[j + 0] << 24) |
  1010. (oob[j + 1] << 16) |
  1011. (oob[j + 2] << 8) |
  1012. (oob[j + 3] << 0));
  1013. return tbytes;
  1014. }
  1015. static irqreturn_t brcmnand_ctlrdy_irq(int irq, void *data)
  1016. {
  1017. struct brcmnand_controller *ctrl = data;
  1018. /* Discard all NAND_CTLRDY interrupts during DMA */
  1019. if (ctrl->dma_pending)
  1020. return IRQ_HANDLED;
  1021. complete(&ctrl->done);
  1022. return IRQ_HANDLED;
  1023. }
  1024. /* Handle SoC-specific interrupt hardware */
  1025. static irqreturn_t brcmnand_irq(int irq, void *data)
  1026. {
  1027. struct brcmnand_controller *ctrl = data;
  1028. if (ctrl->soc->ctlrdy_ack(ctrl->soc))
  1029. return brcmnand_ctlrdy_irq(irq, data);
  1030. return IRQ_NONE;
  1031. }
  1032. static irqreturn_t brcmnand_dma_irq(int irq, void *data)
  1033. {
  1034. struct brcmnand_controller *ctrl = data;
  1035. complete(&ctrl->dma_done);
  1036. return IRQ_HANDLED;
  1037. }
  1038. static void brcmnand_send_cmd(struct brcmnand_host *host, int cmd)
  1039. {
  1040. struct brcmnand_controller *ctrl = host->ctrl;
  1041. int ret;
  1042. dev_dbg(ctrl->dev, "send native cmd %d addr_lo 0x%x\n", cmd,
  1043. brcmnand_read_reg(ctrl, BRCMNAND_CMD_ADDRESS));
  1044. BUG_ON(ctrl->cmd_pending != 0);
  1045. ctrl->cmd_pending = cmd;
  1046. ret = bcmnand_ctrl_poll_status(ctrl, NAND_CTRL_RDY, NAND_CTRL_RDY, 0);
  1047. WARN_ON(ret);
  1048. mb(); /* flush previous writes */
  1049. brcmnand_write_reg(ctrl, BRCMNAND_CMD_START,
  1050. cmd << brcmnand_cmd_shift(ctrl));
  1051. }
  1052. /***********************************************************************
  1053. * NAND MTD API: read/program/erase
  1054. ***********************************************************************/
  1055. static void brcmnand_cmd_ctrl(struct mtd_info *mtd, int dat,
  1056. unsigned int ctrl)
  1057. {
  1058. /* intentionally left blank */
  1059. }
  1060. static int brcmnand_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
  1061. {
  1062. struct nand_chip *chip = mtd_to_nand(mtd);
  1063. struct brcmnand_host *host = nand_get_controller_data(chip);
  1064. struct brcmnand_controller *ctrl = host->ctrl;
  1065. unsigned long timeo = msecs_to_jiffies(100);
  1066. dev_dbg(ctrl->dev, "wait on native cmd %d\n", ctrl->cmd_pending);
  1067. if (ctrl->cmd_pending &&
  1068. wait_for_completion_timeout(&ctrl->done, timeo) <= 0) {
  1069. u32 cmd = brcmnand_read_reg(ctrl, BRCMNAND_CMD_START)
  1070. >> brcmnand_cmd_shift(ctrl);
  1071. dev_err_ratelimited(ctrl->dev,
  1072. "timeout waiting for command %#02x\n", cmd);
  1073. dev_err_ratelimited(ctrl->dev, "intfc status %08x\n",
  1074. brcmnand_read_reg(ctrl, BRCMNAND_INTFC_STATUS));
  1075. }
  1076. ctrl->cmd_pending = 0;
  1077. return brcmnand_read_reg(ctrl, BRCMNAND_INTFC_STATUS) &
  1078. INTFC_FLASH_STATUS;
  1079. }
  1080. enum {
  1081. LLOP_RE = BIT(16),
  1082. LLOP_WE = BIT(17),
  1083. LLOP_ALE = BIT(18),
  1084. LLOP_CLE = BIT(19),
  1085. LLOP_RETURN_IDLE = BIT(31),
  1086. LLOP_DATA_MASK = GENMASK(15, 0),
  1087. };
  1088. static int brcmnand_low_level_op(struct brcmnand_host *host,
  1089. enum brcmnand_llop_type type, u32 data,
  1090. bool last_op)
  1091. {
  1092. struct mtd_info *mtd = nand_to_mtd(&host->chip);
  1093. struct nand_chip *chip = &host->chip;
  1094. struct brcmnand_controller *ctrl = host->ctrl;
  1095. u32 tmp;
  1096. tmp = data & LLOP_DATA_MASK;
  1097. switch (type) {
  1098. case LL_OP_CMD:
  1099. tmp |= LLOP_WE | LLOP_CLE;
  1100. break;
  1101. case LL_OP_ADDR:
  1102. /* WE | ALE */
  1103. tmp |= LLOP_WE | LLOP_ALE;
  1104. break;
  1105. case LL_OP_WR:
  1106. /* WE */
  1107. tmp |= LLOP_WE;
  1108. break;
  1109. case LL_OP_RD:
  1110. /* RE */
  1111. tmp |= LLOP_RE;
  1112. break;
  1113. }
  1114. if (last_op)
  1115. /* RETURN_IDLE */
  1116. tmp |= LLOP_RETURN_IDLE;
  1117. dev_dbg(ctrl->dev, "ll_op cmd %#x\n", tmp);
  1118. brcmnand_write_reg(ctrl, BRCMNAND_LL_OP, tmp);
  1119. (void)brcmnand_read_reg(ctrl, BRCMNAND_LL_OP);
  1120. brcmnand_send_cmd(host, CMD_LOW_LEVEL_OP);
  1121. return brcmnand_waitfunc(mtd, chip);
  1122. }
  1123. static void brcmnand_cmdfunc(struct mtd_info *mtd, unsigned command,
  1124. int column, int page_addr)
  1125. {
  1126. struct nand_chip *chip = mtd_to_nand(mtd);
  1127. struct brcmnand_host *host = nand_get_controller_data(chip);
  1128. struct brcmnand_controller *ctrl = host->ctrl;
  1129. u64 addr = (u64)page_addr << chip->page_shift;
  1130. int native_cmd = 0;
  1131. if (command == NAND_CMD_READID || command == NAND_CMD_PARAM ||
  1132. command == NAND_CMD_RNDOUT)
  1133. addr = (u64)column;
  1134. /* Avoid propagating a negative, don't-care address */
  1135. else if (page_addr < 0)
  1136. addr = 0;
  1137. dev_dbg(ctrl->dev, "cmd 0x%x addr 0x%llx\n", command,
  1138. (unsigned long long)addr);
  1139. host->last_cmd = command;
  1140. host->last_byte = 0;
  1141. host->last_addr = addr;
  1142. switch (command) {
  1143. case NAND_CMD_RESET:
  1144. native_cmd = CMD_FLASH_RESET;
  1145. break;
  1146. case NAND_CMD_STATUS:
  1147. native_cmd = CMD_STATUS_READ;
  1148. break;
  1149. case NAND_CMD_READID:
  1150. native_cmd = CMD_DEVICE_ID_READ;
  1151. break;
  1152. case NAND_CMD_READOOB:
  1153. native_cmd = CMD_SPARE_AREA_READ;
  1154. break;
  1155. case NAND_CMD_ERASE1:
  1156. native_cmd = CMD_BLOCK_ERASE;
  1157. brcmnand_wp(mtd, 0);
  1158. break;
  1159. case NAND_CMD_PARAM:
  1160. native_cmd = CMD_PARAMETER_READ;
  1161. break;
  1162. case NAND_CMD_SET_FEATURES:
  1163. case NAND_CMD_GET_FEATURES:
  1164. brcmnand_low_level_op(host, LL_OP_CMD, command, false);
  1165. brcmnand_low_level_op(host, LL_OP_ADDR, column, false);
  1166. break;
  1167. case NAND_CMD_RNDOUT:
  1168. native_cmd = CMD_PARAMETER_CHANGE_COL;
  1169. addr &= ~((u64)(FC_BYTES - 1));
  1170. /*
  1171. * HW quirk: PARAMETER_CHANGE_COL requires SECTOR_SIZE_1K=0
  1172. * NB: hwcfg.sector_size_1k may not be initialized yet
  1173. */
  1174. if (brcmnand_get_sector_size_1k(host)) {
  1175. host->hwcfg.sector_size_1k =
  1176. brcmnand_get_sector_size_1k(host);
  1177. brcmnand_set_sector_size_1k(host, 0);
  1178. }
  1179. break;
  1180. }
  1181. if (!native_cmd)
  1182. return;
  1183. brcmnand_write_reg(ctrl, BRCMNAND_CMD_EXT_ADDRESS,
  1184. (host->cs << 16) | ((addr >> 32) & 0xffff));
  1185. (void)brcmnand_read_reg(ctrl, BRCMNAND_CMD_EXT_ADDRESS);
  1186. brcmnand_write_reg(ctrl, BRCMNAND_CMD_ADDRESS, lower_32_bits(addr));
  1187. (void)brcmnand_read_reg(ctrl, BRCMNAND_CMD_ADDRESS);
  1188. brcmnand_send_cmd(host, native_cmd);
  1189. brcmnand_waitfunc(mtd, chip);
  1190. if (native_cmd == CMD_PARAMETER_READ ||
  1191. native_cmd == CMD_PARAMETER_CHANGE_COL) {
  1192. /* Copy flash cache word-wise */
  1193. u32 *flash_cache = (u32 *)ctrl->flash_cache;
  1194. int i;
  1195. brcmnand_soc_data_bus_prepare(ctrl->soc, true);
  1196. /*
  1197. * Must cache the FLASH_CACHE now, since changes in
  1198. * SECTOR_SIZE_1K may invalidate it
  1199. */
  1200. for (i = 0; i < FC_WORDS; i++)
  1201. /*
  1202. * Flash cache is big endian for parameter pages, at
  1203. * least on STB SoCs
  1204. */
  1205. flash_cache[i] = be32_to_cpu(brcmnand_read_fc(ctrl, i));
  1206. brcmnand_soc_data_bus_unprepare(ctrl->soc, true);
  1207. /* Cleanup from HW quirk: restore SECTOR_SIZE_1K */
  1208. if (host->hwcfg.sector_size_1k)
  1209. brcmnand_set_sector_size_1k(host,
  1210. host->hwcfg.sector_size_1k);
  1211. }
  1212. /* Re-enable protection is necessary only after erase */
  1213. if (command == NAND_CMD_ERASE1)
  1214. brcmnand_wp(mtd, 1);
  1215. }
  1216. static uint8_t brcmnand_read_byte(struct mtd_info *mtd)
  1217. {
  1218. struct nand_chip *chip = mtd_to_nand(mtd);
  1219. struct brcmnand_host *host = nand_get_controller_data(chip);
  1220. struct brcmnand_controller *ctrl = host->ctrl;
  1221. uint8_t ret = 0;
  1222. int addr, offs;
  1223. switch (host->last_cmd) {
  1224. case NAND_CMD_READID:
  1225. if (host->last_byte < 4)
  1226. ret = brcmnand_read_reg(ctrl, BRCMNAND_ID) >>
  1227. (24 - (host->last_byte << 3));
  1228. else if (host->last_byte < 8)
  1229. ret = brcmnand_read_reg(ctrl, BRCMNAND_ID_EXT) >>
  1230. (56 - (host->last_byte << 3));
  1231. break;
  1232. case NAND_CMD_READOOB:
  1233. ret = oob_reg_read(ctrl, host->last_byte);
  1234. break;
  1235. case NAND_CMD_STATUS:
  1236. ret = brcmnand_read_reg(ctrl, BRCMNAND_INTFC_STATUS) &
  1237. INTFC_FLASH_STATUS;
  1238. if (wp_on) /* hide WP status */
  1239. ret |= NAND_STATUS_WP;
  1240. break;
  1241. case NAND_CMD_PARAM:
  1242. case NAND_CMD_RNDOUT:
  1243. addr = host->last_addr + host->last_byte;
  1244. offs = addr & (FC_BYTES - 1);
  1245. /* At FC_BYTES boundary, switch to next column */
  1246. if (host->last_byte > 0 && offs == 0)
  1247. chip->cmdfunc(mtd, NAND_CMD_RNDOUT, addr, -1);
  1248. ret = ctrl->flash_cache[offs];
  1249. break;
  1250. case NAND_CMD_GET_FEATURES:
  1251. if (host->last_byte >= ONFI_SUBFEATURE_PARAM_LEN) {
  1252. ret = 0;
  1253. } else {
  1254. bool last = host->last_byte ==
  1255. ONFI_SUBFEATURE_PARAM_LEN - 1;
  1256. brcmnand_low_level_op(host, LL_OP_RD, 0, last);
  1257. ret = brcmnand_read_reg(ctrl, BRCMNAND_LL_RDATA) & 0xff;
  1258. }
  1259. }
  1260. dev_dbg(ctrl->dev, "read byte = 0x%02x\n", ret);
  1261. host->last_byte++;
  1262. return ret;
  1263. }
  1264. static void brcmnand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  1265. {
  1266. int i;
  1267. for (i = 0; i < len; i++, buf++)
  1268. *buf = brcmnand_read_byte(mtd);
  1269. }
  1270. static void brcmnand_write_buf(struct mtd_info *mtd, const uint8_t *buf,
  1271. int len)
  1272. {
  1273. int i;
  1274. struct nand_chip *chip = mtd_to_nand(mtd);
  1275. struct brcmnand_host *host = nand_get_controller_data(chip);
  1276. switch (host->last_cmd) {
  1277. case NAND_CMD_SET_FEATURES:
  1278. for (i = 0; i < len; i++)
  1279. brcmnand_low_level_op(host, LL_OP_WR, buf[i],
  1280. (i + 1) == len);
  1281. break;
  1282. default:
  1283. BUG();
  1284. break;
  1285. }
  1286. }
  1287. /**
  1288. * Construct a FLASH_DMA descriptor as part of a linked list. You must know the
  1289. * following ahead of time:
  1290. * - Is this descriptor the beginning or end of a linked list?
  1291. * - What is the (DMA) address of the next descriptor in the linked list?
  1292. */
  1293. static int brcmnand_fill_dma_desc(struct brcmnand_host *host,
  1294. struct brcm_nand_dma_desc *desc, u64 addr,
  1295. dma_addr_t buf, u32 len, u8 dma_cmd,
  1296. bool begin, bool end,
  1297. dma_addr_t next_desc)
  1298. {
  1299. memset(desc, 0, sizeof(*desc));
  1300. /* Descriptors are written in native byte order (wordwise) */
  1301. desc->next_desc = lower_32_bits(next_desc);
  1302. desc->next_desc_ext = upper_32_bits(next_desc);
  1303. desc->cmd_irq = (dma_cmd << 24) |
  1304. (end ? (0x03 << 8) : 0) | /* IRQ | STOP */
  1305. (!!begin) | ((!!end) << 1); /* head, tail */
  1306. #ifdef CONFIG_CPU_BIG_ENDIAN
  1307. desc->cmd_irq |= 0x01 << 12;
  1308. #endif
  1309. desc->dram_addr = lower_32_bits(buf);
  1310. desc->dram_addr_ext = upper_32_bits(buf);
  1311. desc->tfr_len = len;
  1312. desc->total_len = len;
  1313. desc->flash_addr = lower_32_bits(addr);
  1314. desc->flash_addr_ext = upper_32_bits(addr);
  1315. desc->cs = host->cs;
  1316. desc->status_valid = 0x01;
  1317. return 0;
  1318. }
  1319. /**
  1320. * Kick the FLASH_DMA engine, with a given DMA descriptor
  1321. */
  1322. static void brcmnand_dma_run(struct brcmnand_host *host, dma_addr_t desc)
  1323. {
  1324. struct brcmnand_controller *ctrl = host->ctrl;
  1325. unsigned long timeo = msecs_to_jiffies(100);
  1326. flash_dma_writel(ctrl, FLASH_DMA_FIRST_DESC, lower_32_bits(desc));
  1327. (void)flash_dma_readl(ctrl, FLASH_DMA_FIRST_DESC);
  1328. flash_dma_writel(ctrl, FLASH_DMA_FIRST_DESC_EXT, upper_32_bits(desc));
  1329. (void)flash_dma_readl(ctrl, FLASH_DMA_FIRST_DESC_EXT);
  1330. /* Start FLASH_DMA engine */
  1331. ctrl->dma_pending = true;
  1332. mb(); /* flush previous writes */
  1333. flash_dma_writel(ctrl, FLASH_DMA_CTRL, 0x03); /* wake | run */
  1334. if (wait_for_completion_timeout(&ctrl->dma_done, timeo) <= 0) {
  1335. dev_err(ctrl->dev,
  1336. "timeout waiting for DMA; status %#x, error status %#x\n",
  1337. flash_dma_readl(ctrl, FLASH_DMA_STATUS),
  1338. flash_dma_readl(ctrl, FLASH_DMA_ERROR_STATUS));
  1339. }
  1340. ctrl->dma_pending = false;
  1341. flash_dma_writel(ctrl, FLASH_DMA_CTRL, 0); /* force stop */
  1342. }
  1343. static int brcmnand_dma_trans(struct brcmnand_host *host, u64 addr, u32 *buf,
  1344. u32 len, u8 dma_cmd)
  1345. {
  1346. struct brcmnand_controller *ctrl = host->ctrl;
  1347. dma_addr_t buf_pa;
  1348. int dir = dma_cmd == CMD_PAGE_READ ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  1349. buf_pa = dma_map_single(ctrl->dev, buf, len, dir);
  1350. if (dma_mapping_error(ctrl->dev, buf_pa)) {
  1351. dev_err(ctrl->dev, "unable to map buffer for DMA\n");
  1352. return -ENOMEM;
  1353. }
  1354. brcmnand_fill_dma_desc(host, ctrl->dma_desc, addr, buf_pa, len,
  1355. dma_cmd, true, true, 0);
  1356. brcmnand_dma_run(host, ctrl->dma_pa);
  1357. dma_unmap_single(ctrl->dev, buf_pa, len, dir);
  1358. if (ctrl->dma_desc->status_valid & FLASH_DMA_ECC_ERROR)
  1359. return -EBADMSG;
  1360. else if (ctrl->dma_desc->status_valid & FLASH_DMA_CORR_ERROR)
  1361. return -EUCLEAN;
  1362. return 0;
  1363. }
  1364. /*
  1365. * Assumes proper CS is already set
  1366. */
  1367. static int brcmnand_read_by_pio(struct mtd_info *mtd, struct nand_chip *chip,
  1368. u64 addr, unsigned int trans, u32 *buf,
  1369. u8 *oob, u64 *err_addr)
  1370. {
  1371. struct brcmnand_host *host = nand_get_controller_data(chip);
  1372. struct brcmnand_controller *ctrl = host->ctrl;
  1373. int i, j, ret = 0;
  1374. /* Clear error addresses */
  1375. brcmnand_write_reg(ctrl, BRCMNAND_UNCORR_ADDR, 0);
  1376. brcmnand_write_reg(ctrl, BRCMNAND_CORR_ADDR, 0);
  1377. brcmnand_write_reg(ctrl, BRCMNAND_UNCORR_EXT_ADDR, 0);
  1378. brcmnand_write_reg(ctrl, BRCMNAND_CORR_EXT_ADDR, 0);
  1379. brcmnand_write_reg(ctrl, BRCMNAND_CMD_EXT_ADDRESS,
  1380. (host->cs << 16) | ((addr >> 32) & 0xffff));
  1381. (void)brcmnand_read_reg(ctrl, BRCMNAND_CMD_EXT_ADDRESS);
  1382. for (i = 0; i < trans; i++, addr += FC_BYTES) {
  1383. brcmnand_write_reg(ctrl, BRCMNAND_CMD_ADDRESS,
  1384. lower_32_bits(addr));
  1385. (void)brcmnand_read_reg(ctrl, BRCMNAND_CMD_ADDRESS);
  1386. /* SPARE_AREA_READ does not use ECC, so just use PAGE_READ */
  1387. brcmnand_send_cmd(host, CMD_PAGE_READ);
  1388. brcmnand_waitfunc(mtd, chip);
  1389. if (likely(buf)) {
  1390. brcmnand_soc_data_bus_prepare(ctrl->soc, false);
  1391. for (j = 0; j < FC_WORDS; j++, buf++)
  1392. *buf = brcmnand_read_fc(ctrl, j);
  1393. brcmnand_soc_data_bus_unprepare(ctrl->soc, false);
  1394. }
  1395. if (oob)
  1396. oob += read_oob_from_regs(ctrl, i, oob,
  1397. mtd->oobsize / trans,
  1398. host->hwcfg.sector_size_1k);
  1399. if (!ret) {
  1400. *err_addr = brcmnand_read_reg(ctrl,
  1401. BRCMNAND_UNCORR_ADDR) |
  1402. ((u64)(brcmnand_read_reg(ctrl,
  1403. BRCMNAND_UNCORR_EXT_ADDR)
  1404. & 0xffff) << 32);
  1405. if (*err_addr)
  1406. ret = -EBADMSG;
  1407. }
  1408. if (!ret) {
  1409. *err_addr = brcmnand_read_reg(ctrl,
  1410. BRCMNAND_CORR_ADDR) |
  1411. ((u64)(brcmnand_read_reg(ctrl,
  1412. BRCMNAND_CORR_EXT_ADDR)
  1413. & 0xffff) << 32);
  1414. if (*err_addr)
  1415. ret = -EUCLEAN;
  1416. }
  1417. }
  1418. return ret;
  1419. }
  1420. /*
  1421. * Check a page to see if it is erased (w/ bitflips) after an uncorrectable ECC
  1422. * error
  1423. *
  1424. * Because the HW ECC signals an ECC error if an erase paged has even a single
  1425. * bitflip, we must check each ECC error to see if it is actually an erased
  1426. * page with bitflips, not a truly corrupted page.
  1427. *
  1428. * On a real error, return a negative error code (-EBADMSG for ECC error), and
  1429. * buf will contain raw data.
  1430. * Otherwise, buf gets filled with 0xffs and return the maximum number of
  1431. * bitflips-per-ECC-sector to the caller.
  1432. *
  1433. */
  1434. static int brcmstb_nand_verify_erased_page(struct mtd_info *mtd,
  1435. struct nand_chip *chip, void *buf, u64 addr)
  1436. {
  1437. int i, sas;
  1438. void *oob = chip->oob_poi;
  1439. int bitflips = 0;
  1440. int page = addr >> chip->page_shift;
  1441. int ret;
  1442. if (!buf) {
  1443. buf = chip->buffers->databuf;
  1444. /* Invalidate page cache */
  1445. chip->pagebuf = -1;
  1446. }
  1447. sas = mtd->oobsize / chip->ecc.steps;
  1448. /* read without ecc for verification */
  1449. chip->cmdfunc(mtd, NAND_CMD_READ0, 0x00, page);
  1450. ret = chip->ecc.read_page_raw(mtd, chip, buf, true, page);
  1451. if (ret)
  1452. return ret;
  1453. for (i = 0; i < chip->ecc.steps; i++, oob += sas) {
  1454. ret = nand_check_erased_ecc_chunk(buf, chip->ecc.size,
  1455. oob, sas, NULL, 0,
  1456. chip->ecc.strength);
  1457. if (ret < 0)
  1458. return ret;
  1459. bitflips = max(bitflips, ret);
  1460. }
  1461. return bitflips;
  1462. }
  1463. static int brcmnand_read(struct mtd_info *mtd, struct nand_chip *chip,
  1464. u64 addr, unsigned int trans, u32 *buf, u8 *oob)
  1465. {
  1466. struct brcmnand_host *host = nand_get_controller_data(chip);
  1467. struct brcmnand_controller *ctrl = host->ctrl;
  1468. u64 err_addr = 0;
  1469. int err;
  1470. bool retry = true;
  1471. dev_dbg(ctrl->dev, "read %llx -> %p\n", (unsigned long long)addr, buf);
  1472. try_dmaread:
  1473. brcmnand_write_reg(ctrl, BRCMNAND_UNCORR_COUNT, 0);
  1474. if (has_flash_dma(ctrl) && !oob && flash_dma_buf_ok(buf)) {
  1475. err = brcmnand_dma_trans(host, addr, buf, trans * FC_BYTES,
  1476. CMD_PAGE_READ);
  1477. if (err) {
  1478. if (mtd_is_bitflip_or_eccerr(err))
  1479. err_addr = addr;
  1480. else
  1481. return -EIO;
  1482. }
  1483. } else {
  1484. if (oob)
  1485. memset(oob, 0x99, mtd->oobsize);
  1486. err = brcmnand_read_by_pio(mtd, chip, addr, trans, buf,
  1487. oob, &err_addr);
  1488. }
  1489. if (mtd_is_eccerr(err)) {
  1490. /*
  1491. * On controller version and 7.0, 7.1 , DMA read after a
  1492. * prior PIO read that reported uncorrectable error,
  1493. * the DMA engine captures this error following DMA read
  1494. * cleared only on subsequent DMA read, so just retry once
  1495. * to clear a possible false error reported for current DMA
  1496. * read
  1497. */
  1498. if ((ctrl->nand_version == 0x0700) ||
  1499. (ctrl->nand_version == 0x0701)) {
  1500. if (retry) {
  1501. retry = false;
  1502. goto try_dmaread;
  1503. }
  1504. }
  1505. /*
  1506. * Controller version 7.2 has hw encoder to detect erased page
  1507. * bitflips, apply sw verification for older controllers only
  1508. */
  1509. if (ctrl->nand_version < 0x0702) {
  1510. err = brcmstb_nand_verify_erased_page(mtd, chip, buf,
  1511. addr);
  1512. /* erased page bitflips corrected */
  1513. if (err >= 0)
  1514. return err;
  1515. }
  1516. dev_dbg(ctrl->dev, "uncorrectable error at 0x%llx\n",
  1517. (unsigned long long)err_addr);
  1518. mtd->ecc_stats.failed++;
  1519. /* NAND layer expects zero on ECC errors */
  1520. return 0;
  1521. }
  1522. if (mtd_is_bitflip(err)) {
  1523. unsigned int corrected = brcmnand_count_corrected(ctrl);
  1524. dev_dbg(ctrl->dev, "corrected error at 0x%llx\n",
  1525. (unsigned long long)err_addr);
  1526. mtd->ecc_stats.corrected += corrected;
  1527. /* Always exceed the software-imposed threshold */
  1528. return max(mtd->bitflip_threshold, corrected);
  1529. }
  1530. return 0;
  1531. }
  1532. static int brcmnand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
  1533. uint8_t *buf, int oob_required, int page)
  1534. {
  1535. struct brcmnand_host *host = nand_get_controller_data(chip);
  1536. u8 *oob = oob_required ? (u8 *)chip->oob_poi : NULL;
  1537. return brcmnand_read(mtd, chip, host->last_addr,
  1538. mtd->writesize >> FC_SHIFT, (u32 *)buf, oob);
  1539. }
  1540. static int brcmnand_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
  1541. uint8_t *buf, int oob_required, int page)
  1542. {
  1543. struct brcmnand_host *host = nand_get_controller_data(chip);
  1544. u8 *oob = oob_required ? (u8 *)chip->oob_poi : NULL;
  1545. int ret;
  1546. brcmnand_set_ecc_enabled(host, 0);
  1547. ret = brcmnand_read(mtd, chip, host->last_addr,
  1548. mtd->writesize >> FC_SHIFT, (u32 *)buf, oob);
  1549. brcmnand_set_ecc_enabled(host, 1);
  1550. return ret;
  1551. }
  1552. static int brcmnand_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
  1553. int page)
  1554. {
  1555. return brcmnand_read(mtd, chip, (u64)page << chip->page_shift,
  1556. mtd->writesize >> FC_SHIFT,
  1557. NULL, (u8 *)chip->oob_poi);
  1558. }
  1559. static int brcmnand_read_oob_raw(struct mtd_info *mtd, struct nand_chip *chip,
  1560. int page)
  1561. {
  1562. struct brcmnand_host *host = nand_get_controller_data(chip);
  1563. brcmnand_set_ecc_enabled(host, 0);
  1564. brcmnand_read(mtd, chip, (u64)page << chip->page_shift,
  1565. mtd->writesize >> FC_SHIFT,
  1566. NULL, (u8 *)chip->oob_poi);
  1567. brcmnand_set_ecc_enabled(host, 1);
  1568. return 0;
  1569. }
  1570. static int brcmnand_write(struct mtd_info *mtd, struct nand_chip *chip,
  1571. u64 addr, const u32 *buf, u8 *oob)
  1572. {
  1573. struct brcmnand_host *host = nand_get_controller_data(chip);
  1574. struct brcmnand_controller *ctrl = host->ctrl;
  1575. unsigned int i, j, trans = mtd->writesize >> FC_SHIFT;
  1576. int status, ret = 0;
  1577. dev_dbg(ctrl->dev, "write %llx <- %p\n", (unsigned long long)addr, buf);
  1578. if (unlikely((unsigned long)buf & 0x03)) {
  1579. dev_warn(ctrl->dev, "unaligned buffer: %p\n", buf);
  1580. buf = (u32 *)((unsigned long)buf & ~0x03);
  1581. }
  1582. brcmnand_wp(mtd, 0);
  1583. for (i = 0; i < ctrl->max_oob; i += 4)
  1584. oob_reg_write(ctrl, i, 0xffffffff);
  1585. if (has_flash_dma(ctrl) && !oob && flash_dma_buf_ok(buf)) {
  1586. if (brcmnand_dma_trans(host, addr, (u32 *)buf,
  1587. mtd->writesize, CMD_PROGRAM_PAGE))
  1588. ret = -EIO;
  1589. goto out;
  1590. }
  1591. brcmnand_write_reg(ctrl, BRCMNAND_CMD_EXT_ADDRESS,
  1592. (host->cs << 16) | ((addr >> 32) & 0xffff));
  1593. (void)brcmnand_read_reg(ctrl, BRCMNAND_CMD_EXT_ADDRESS);
  1594. for (i = 0; i < trans; i++, addr += FC_BYTES) {
  1595. /* full address MUST be set before populating FC */
  1596. brcmnand_write_reg(ctrl, BRCMNAND_CMD_ADDRESS,
  1597. lower_32_bits(addr));
  1598. (void)brcmnand_read_reg(ctrl, BRCMNAND_CMD_ADDRESS);
  1599. if (buf) {
  1600. brcmnand_soc_data_bus_prepare(ctrl->soc, false);
  1601. for (j = 0; j < FC_WORDS; j++, buf++)
  1602. brcmnand_write_fc(ctrl, j, *buf);
  1603. brcmnand_soc_data_bus_unprepare(ctrl->soc, false);
  1604. } else if (oob) {
  1605. for (j = 0; j < FC_WORDS; j++)
  1606. brcmnand_write_fc(ctrl, j, 0xffffffff);
  1607. }
  1608. if (oob) {
  1609. oob += write_oob_to_regs(ctrl, i, oob,
  1610. mtd->oobsize / trans,
  1611. host->hwcfg.sector_size_1k);
  1612. }
  1613. /* we cannot use SPARE_AREA_PROGRAM when PARTIAL_PAGE_EN=0 */
  1614. brcmnand_send_cmd(host, CMD_PROGRAM_PAGE);
  1615. status = brcmnand_waitfunc(mtd, chip);
  1616. if (status & NAND_STATUS_FAIL) {
  1617. dev_info(ctrl->dev, "program failed at %llx\n",
  1618. (unsigned long long)addr);
  1619. ret = -EIO;
  1620. goto out;
  1621. }
  1622. }
  1623. out:
  1624. brcmnand_wp(mtd, 1);
  1625. return ret;
  1626. }
  1627. static int brcmnand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  1628. const uint8_t *buf, int oob_required, int page)
  1629. {
  1630. struct brcmnand_host *host = nand_get_controller_data(chip);
  1631. void *oob = oob_required ? chip->oob_poi : NULL;
  1632. brcmnand_write(mtd, chip, host->last_addr, (const u32 *)buf, oob);
  1633. return 0;
  1634. }
  1635. static int brcmnand_write_page_raw(struct mtd_info *mtd,
  1636. struct nand_chip *chip, const uint8_t *buf,
  1637. int oob_required, int page)
  1638. {
  1639. struct brcmnand_host *host = nand_get_controller_data(chip);
  1640. void *oob = oob_required ? chip->oob_poi : NULL;
  1641. brcmnand_set_ecc_enabled(host, 0);
  1642. brcmnand_write(mtd, chip, host->last_addr, (const u32 *)buf, oob);
  1643. brcmnand_set_ecc_enabled(host, 1);
  1644. return 0;
  1645. }
  1646. static int brcmnand_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
  1647. int page)
  1648. {
  1649. return brcmnand_write(mtd, chip, (u64)page << chip->page_shift,
  1650. NULL, chip->oob_poi);
  1651. }
  1652. static int brcmnand_write_oob_raw(struct mtd_info *mtd, struct nand_chip *chip,
  1653. int page)
  1654. {
  1655. struct brcmnand_host *host = nand_get_controller_data(chip);
  1656. int ret;
  1657. brcmnand_set_ecc_enabled(host, 0);
  1658. ret = brcmnand_write(mtd, chip, (u64)page << chip->page_shift, NULL,
  1659. (u8 *)chip->oob_poi);
  1660. brcmnand_set_ecc_enabled(host, 1);
  1661. return ret;
  1662. }
  1663. /***********************************************************************
  1664. * Per-CS setup (1 NAND device)
  1665. ***********************************************************************/
  1666. static int brcmnand_set_cfg(struct brcmnand_host *host,
  1667. struct brcmnand_cfg *cfg)
  1668. {
  1669. struct brcmnand_controller *ctrl = host->ctrl;
  1670. struct nand_chip *chip = &host->chip;
  1671. u16 cfg_offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_CFG);
  1672. u16 cfg_ext_offs = brcmnand_cs_offset(ctrl, host->cs,
  1673. BRCMNAND_CS_CFG_EXT);
  1674. u16 acc_control_offs = brcmnand_cs_offset(ctrl, host->cs,
  1675. BRCMNAND_CS_ACC_CONTROL);
  1676. u8 block_size = 0, page_size = 0, device_size = 0;
  1677. u32 tmp;
  1678. if (ctrl->block_sizes) {
  1679. int i, found;
  1680. for (i = 0, found = 0; ctrl->block_sizes[i]; i++)
  1681. if (ctrl->block_sizes[i] * 1024 == cfg->block_size) {
  1682. block_size = i;
  1683. found = 1;
  1684. }
  1685. if (!found) {
  1686. dev_warn(ctrl->dev, "invalid block size %u\n",
  1687. cfg->block_size);
  1688. return -EINVAL;
  1689. }
  1690. } else {
  1691. block_size = ffs(cfg->block_size) - ffs(BRCMNAND_MIN_BLOCKSIZE);
  1692. }
  1693. if (cfg->block_size < BRCMNAND_MIN_BLOCKSIZE || (ctrl->max_block_size &&
  1694. cfg->block_size > ctrl->max_block_size)) {
  1695. dev_warn(ctrl->dev, "invalid block size %u\n",
  1696. cfg->block_size);
  1697. block_size = 0;
  1698. }
  1699. if (ctrl->page_sizes) {
  1700. int i, found;
  1701. for (i = 0, found = 0; ctrl->page_sizes[i]; i++)
  1702. if (ctrl->page_sizes[i] == cfg->page_size) {
  1703. page_size = i;
  1704. found = 1;
  1705. }
  1706. if (!found) {
  1707. dev_warn(ctrl->dev, "invalid page size %u\n",
  1708. cfg->page_size);
  1709. return -EINVAL;
  1710. }
  1711. } else {
  1712. page_size = ffs(cfg->page_size) - ffs(BRCMNAND_MIN_PAGESIZE);
  1713. }
  1714. if (cfg->page_size < BRCMNAND_MIN_PAGESIZE || (ctrl->max_page_size &&
  1715. cfg->page_size > ctrl->max_page_size)) {
  1716. dev_warn(ctrl->dev, "invalid page size %u\n", cfg->page_size);
  1717. return -EINVAL;
  1718. }
  1719. if (fls64(cfg->device_size) < fls64(BRCMNAND_MIN_DEVSIZE)) {
  1720. dev_warn(ctrl->dev, "invalid device size 0x%llx\n",
  1721. (unsigned long long)cfg->device_size);
  1722. return -EINVAL;
  1723. }
  1724. device_size = fls64(cfg->device_size) - fls64(BRCMNAND_MIN_DEVSIZE);
  1725. tmp = (cfg->blk_adr_bytes << CFG_BLK_ADR_BYTES_SHIFT) |
  1726. (cfg->col_adr_bytes << CFG_COL_ADR_BYTES_SHIFT) |
  1727. (cfg->ful_adr_bytes << CFG_FUL_ADR_BYTES_SHIFT) |
  1728. (!!(cfg->device_width == 16) << CFG_BUS_WIDTH_SHIFT) |
  1729. (device_size << CFG_DEVICE_SIZE_SHIFT);
  1730. if (cfg_offs == cfg_ext_offs) {
  1731. tmp |= (page_size << CFG_PAGE_SIZE_SHIFT) |
  1732. (block_size << CFG_BLK_SIZE_SHIFT);
  1733. nand_writereg(ctrl, cfg_offs, tmp);
  1734. } else {
  1735. nand_writereg(ctrl, cfg_offs, tmp);
  1736. tmp = (page_size << CFG_EXT_PAGE_SIZE_SHIFT) |
  1737. (block_size << CFG_EXT_BLK_SIZE_SHIFT);
  1738. nand_writereg(ctrl, cfg_ext_offs, tmp);
  1739. }
  1740. tmp = nand_readreg(ctrl, acc_control_offs);
  1741. tmp &= ~brcmnand_ecc_level_mask(ctrl);
  1742. tmp |= cfg->ecc_level << NAND_ACC_CONTROL_ECC_SHIFT;
  1743. tmp &= ~brcmnand_spare_area_mask(ctrl);
  1744. tmp |= cfg->spare_area_size;
  1745. nand_writereg(ctrl, acc_control_offs, tmp);
  1746. brcmnand_set_sector_size_1k(host, cfg->sector_size_1k);
  1747. /* threshold = ceil(BCH-level * 0.75) */
  1748. brcmnand_wr_corr_thresh(host, DIV_ROUND_UP(chip->ecc.strength * 3, 4));
  1749. return 0;
  1750. }
  1751. static void brcmnand_print_cfg(struct brcmnand_host *host,
  1752. char *buf, struct brcmnand_cfg *cfg)
  1753. {
  1754. buf += sprintf(buf,
  1755. "%lluMiB total, %uKiB blocks, %u%s pages, %uB OOB, %u-bit",
  1756. (unsigned long long)cfg->device_size >> 20,
  1757. cfg->block_size >> 10,
  1758. cfg->page_size >= 1024 ? cfg->page_size >> 10 : cfg->page_size,
  1759. cfg->page_size >= 1024 ? "KiB" : "B",
  1760. cfg->spare_area_size, cfg->device_width);
  1761. /* Account for Hamming ECC and for BCH 512B vs 1KiB sectors */
  1762. if (is_hamming_ecc(host->ctrl, cfg))
  1763. sprintf(buf, ", Hamming ECC");
  1764. else if (cfg->sector_size_1k)
  1765. sprintf(buf, ", BCH-%u (1KiB sector)", cfg->ecc_level << 1);
  1766. else
  1767. sprintf(buf, ", BCH-%u", cfg->ecc_level);
  1768. }
  1769. /*
  1770. * Minimum number of bytes to address a page. Calculated as:
  1771. * roundup(log2(size / page-size) / 8)
  1772. *
  1773. * NB: the following does not "round up" for non-power-of-2 'size'; but this is
  1774. * OK because many other things will break if 'size' is irregular...
  1775. */
  1776. static inline int get_blk_adr_bytes(u64 size, u32 writesize)
  1777. {
  1778. return ALIGN(ilog2(size) - ilog2(writesize), 8) >> 3;
  1779. }
  1780. static int brcmnand_setup_dev(struct brcmnand_host *host)
  1781. {
  1782. struct mtd_info *mtd = nand_to_mtd(&host->chip);
  1783. struct nand_chip *chip = &host->chip;
  1784. struct brcmnand_controller *ctrl = host->ctrl;
  1785. struct brcmnand_cfg *cfg = &host->hwcfg;
  1786. char msg[128];
  1787. u32 offs, tmp, oob_sector;
  1788. int ret;
  1789. memset(cfg, 0, sizeof(*cfg));
  1790. ret = of_property_read_u32(nand_get_flash_node(chip),
  1791. "brcm,nand-oob-sector-size",
  1792. &oob_sector);
  1793. if (ret) {
  1794. /* Use detected size */
  1795. cfg->spare_area_size = mtd->oobsize /
  1796. (mtd->writesize >> FC_SHIFT);
  1797. } else {
  1798. cfg->spare_area_size = oob_sector;
  1799. }
  1800. if (cfg->spare_area_size > ctrl->max_oob)
  1801. cfg->spare_area_size = ctrl->max_oob;
  1802. /*
  1803. * Set oobsize to be consistent with controller's spare_area_size, as
  1804. * the rest is inaccessible.
  1805. */
  1806. mtd->oobsize = cfg->spare_area_size * (mtd->writesize >> FC_SHIFT);
  1807. cfg->device_size = mtd->size;
  1808. cfg->block_size = mtd->erasesize;
  1809. cfg->page_size = mtd->writesize;
  1810. cfg->device_width = (chip->options & NAND_BUSWIDTH_16) ? 16 : 8;
  1811. cfg->col_adr_bytes = 2;
  1812. cfg->blk_adr_bytes = get_blk_adr_bytes(mtd->size, mtd->writesize);
  1813. if (chip->ecc.mode != NAND_ECC_HW) {
  1814. dev_err(ctrl->dev, "only HW ECC supported; selected: %d\n",
  1815. chip->ecc.mode);
  1816. return -EINVAL;
  1817. }
  1818. if (chip->ecc.algo == NAND_ECC_UNKNOWN) {
  1819. if (chip->ecc.strength == 1 && chip->ecc.size == 512)
  1820. /* Default to Hamming for 1-bit ECC, if unspecified */
  1821. chip->ecc.algo = NAND_ECC_HAMMING;
  1822. else
  1823. /* Otherwise, BCH */
  1824. chip->ecc.algo = NAND_ECC_BCH;
  1825. }
  1826. if (chip->ecc.algo == NAND_ECC_HAMMING && (chip->ecc.strength != 1 ||
  1827. chip->ecc.size != 512)) {
  1828. dev_err(ctrl->dev, "invalid Hamming params: %d bits per %d bytes\n",
  1829. chip->ecc.strength, chip->ecc.size);
  1830. return -EINVAL;
  1831. }
  1832. switch (chip->ecc.size) {
  1833. case 512:
  1834. if (chip->ecc.algo == NAND_ECC_HAMMING)
  1835. cfg->ecc_level = 15;
  1836. else
  1837. cfg->ecc_level = chip->ecc.strength;
  1838. cfg->sector_size_1k = 0;
  1839. break;
  1840. case 1024:
  1841. if (!(ctrl->features & BRCMNAND_HAS_1K_SECTORS)) {
  1842. dev_err(ctrl->dev, "1KB sectors not supported\n");
  1843. return -EINVAL;
  1844. }
  1845. if (chip->ecc.strength & 0x1) {
  1846. dev_err(ctrl->dev,
  1847. "odd ECC not supported with 1KB sectors\n");
  1848. return -EINVAL;
  1849. }
  1850. cfg->ecc_level = chip->ecc.strength >> 1;
  1851. cfg->sector_size_1k = 1;
  1852. break;
  1853. default:
  1854. dev_err(ctrl->dev, "unsupported ECC size: %d\n",
  1855. chip->ecc.size);
  1856. return -EINVAL;
  1857. }
  1858. cfg->ful_adr_bytes = cfg->blk_adr_bytes;
  1859. if (mtd->writesize > 512)
  1860. cfg->ful_adr_bytes += cfg->col_adr_bytes;
  1861. else
  1862. cfg->ful_adr_bytes += 1;
  1863. ret = brcmnand_set_cfg(host, cfg);
  1864. if (ret)
  1865. return ret;
  1866. brcmnand_set_ecc_enabled(host, 1);
  1867. brcmnand_print_cfg(host, msg, cfg);
  1868. dev_info(ctrl->dev, "detected %s\n", msg);
  1869. /* Configure ACC_CONTROL */
  1870. offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_ACC_CONTROL);
  1871. tmp = nand_readreg(ctrl, offs);
  1872. tmp &= ~ACC_CONTROL_PARTIAL_PAGE;
  1873. tmp &= ~ACC_CONTROL_RD_ERASED;
  1874. /* We need to turn on Read from erased paged protected by ECC */
  1875. if (ctrl->nand_version >= 0x0702)
  1876. tmp |= ACC_CONTROL_RD_ERASED;
  1877. tmp &= ~ACC_CONTROL_FAST_PGM_RDIN;
  1878. if (ctrl->features & BRCMNAND_HAS_PREFETCH)
  1879. tmp &= ~ACC_CONTROL_PREFETCH;
  1880. nand_writereg(ctrl, offs, tmp);
  1881. return 0;
  1882. }
  1883. static int brcmnand_init_cs(struct brcmnand_host *host, struct device_node *dn)
  1884. {
  1885. struct brcmnand_controller *ctrl = host->ctrl;
  1886. struct platform_device *pdev = host->pdev;
  1887. struct mtd_info *mtd;
  1888. struct nand_chip *chip;
  1889. int ret;
  1890. u16 cfg_offs;
  1891. ret = of_property_read_u32(dn, "reg", &host->cs);
  1892. if (ret) {
  1893. dev_err(&pdev->dev, "can't get chip-select\n");
  1894. return -ENXIO;
  1895. }
  1896. mtd = nand_to_mtd(&host->chip);
  1897. chip = &host->chip;
  1898. nand_set_flash_node(chip, dn);
  1899. nand_set_controller_data(chip, host);
  1900. mtd->name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "brcmnand.%d",
  1901. host->cs);
  1902. mtd->owner = THIS_MODULE;
  1903. mtd->dev.parent = &pdev->dev;
  1904. chip->IO_ADDR_R = (void __iomem *)0xdeadbeef;
  1905. chip->IO_ADDR_W = (void __iomem *)0xdeadbeef;
  1906. chip->cmd_ctrl = brcmnand_cmd_ctrl;
  1907. chip->cmdfunc = brcmnand_cmdfunc;
  1908. chip->waitfunc = brcmnand_waitfunc;
  1909. chip->read_byte = brcmnand_read_byte;
  1910. chip->read_buf = brcmnand_read_buf;
  1911. chip->write_buf = brcmnand_write_buf;
  1912. chip->ecc.mode = NAND_ECC_HW;
  1913. chip->ecc.read_page = brcmnand_read_page;
  1914. chip->ecc.write_page = brcmnand_write_page;
  1915. chip->ecc.read_page_raw = brcmnand_read_page_raw;
  1916. chip->ecc.write_page_raw = brcmnand_write_page_raw;
  1917. chip->ecc.write_oob_raw = brcmnand_write_oob_raw;
  1918. chip->ecc.read_oob_raw = brcmnand_read_oob_raw;
  1919. chip->ecc.read_oob = brcmnand_read_oob;
  1920. chip->ecc.write_oob = brcmnand_write_oob;
  1921. chip->controller = &ctrl->controller;
  1922. /*
  1923. * The bootloader might have configured 16bit mode but
  1924. * NAND READID command only works in 8bit mode. We force
  1925. * 8bit mode here to ensure that NAND READID commands works.
  1926. */
  1927. cfg_offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_CFG);
  1928. nand_writereg(ctrl, cfg_offs,
  1929. nand_readreg(ctrl, cfg_offs) & ~CFG_BUS_WIDTH);
  1930. ret = nand_scan_ident(mtd, 1, NULL);
  1931. if (ret)
  1932. return ret;
  1933. chip->options |= NAND_NO_SUBPAGE_WRITE;
  1934. /*
  1935. * Avoid (for instance) kmap()'d buffers from JFFS2, which we can't DMA
  1936. * to/from, and have nand_base pass us a bounce buffer instead, as
  1937. * needed.
  1938. */
  1939. chip->options |= NAND_USE_BOUNCE_BUFFER;
  1940. if (chip->bbt_options & NAND_BBT_USE_FLASH)
  1941. chip->bbt_options |= NAND_BBT_NO_OOB;
  1942. if (brcmnand_setup_dev(host))
  1943. return -ENXIO;
  1944. chip->ecc.size = host->hwcfg.sector_size_1k ? 1024 : 512;
  1945. /* only use our internal HW threshold */
  1946. mtd->bitflip_threshold = 1;
  1947. ret = brcmstb_choose_ecc_layout(host);
  1948. if (ret)
  1949. return ret;
  1950. ret = nand_scan_tail(mtd);
  1951. if (ret)
  1952. return ret;
  1953. return mtd_device_register(mtd, NULL, 0);
  1954. }
  1955. static void brcmnand_save_restore_cs_config(struct brcmnand_host *host,
  1956. int restore)
  1957. {
  1958. struct brcmnand_controller *ctrl = host->ctrl;
  1959. u16 cfg_offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_CFG);
  1960. u16 cfg_ext_offs = brcmnand_cs_offset(ctrl, host->cs,
  1961. BRCMNAND_CS_CFG_EXT);
  1962. u16 acc_control_offs = brcmnand_cs_offset(ctrl, host->cs,
  1963. BRCMNAND_CS_ACC_CONTROL);
  1964. u16 t1_offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_TIMING1);
  1965. u16 t2_offs = brcmnand_cs_offset(ctrl, host->cs, BRCMNAND_CS_TIMING2);
  1966. if (restore) {
  1967. nand_writereg(ctrl, cfg_offs, host->hwcfg.config);
  1968. if (cfg_offs != cfg_ext_offs)
  1969. nand_writereg(ctrl, cfg_ext_offs,
  1970. host->hwcfg.config_ext);
  1971. nand_writereg(ctrl, acc_control_offs, host->hwcfg.acc_control);
  1972. nand_writereg(ctrl, t1_offs, host->hwcfg.timing_1);
  1973. nand_writereg(ctrl, t2_offs, host->hwcfg.timing_2);
  1974. } else {
  1975. host->hwcfg.config = nand_readreg(ctrl, cfg_offs);
  1976. if (cfg_offs != cfg_ext_offs)
  1977. host->hwcfg.config_ext =
  1978. nand_readreg(ctrl, cfg_ext_offs);
  1979. host->hwcfg.acc_control = nand_readreg(ctrl, acc_control_offs);
  1980. host->hwcfg.timing_1 = nand_readreg(ctrl, t1_offs);
  1981. host->hwcfg.timing_2 = nand_readreg(ctrl, t2_offs);
  1982. }
  1983. }
  1984. static int brcmnand_suspend(struct device *dev)
  1985. {
  1986. struct brcmnand_controller *ctrl = dev_get_drvdata(dev);
  1987. struct brcmnand_host *host;
  1988. list_for_each_entry(host, &ctrl->host_list, node)
  1989. brcmnand_save_restore_cs_config(host, 0);
  1990. ctrl->nand_cs_nand_select = brcmnand_read_reg(ctrl, BRCMNAND_CS_SELECT);
  1991. ctrl->nand_cs_nand_xor = brcmnand_read_reg(ctrl, BRCMNAND_CS_XOR);
  1992. ctrl->corr_stat_threshold =
  1993. brcmnand_read_reg(ctrl, BRCMNAND_CORR_THRESHOLD);
  1994. if (has_flash_dma(ctrl))
  1995. ctrl->flash_dma_mode = flash_dma_readl(ctrl, FLASH_DMA_MODE);
  1996. return 0;
  1997. }
  1998. static int brcmnand_resume(struct device *dev)
  1999. {
  2000. struct brcmnand_controller *ctrl = dev_get_drvdata(dev);
  2001. struct brcmnand_host *host;
  2002. if (has_flash_dma(ctrl)) {
  2003. flash_dma_writel(ctrl, FLASH_DMA_MODE, ctrl->flash_dma_mode);
  2004. flash_dma_writel(ctrl, FLASH_DMA_ERROR_STATUS, 0);
  2005. }
  2006. brcmnand_write_reg(ctrl, BRCMNAND_CS_SELECT, ctrl->nand_cs_nand_select);
  2007. brcmnand_write_reg(ctrl, BRCMNAND_CS_XOR, ctrl->nand_cs_nand_xor);
  2008. brcmnand_write_reg(ctrl, BRCMNAND_CORR_THRESHOLD,
  2009. ctrl->corr_stat_threshold);
  2010. if (ctrl->soc) {
  2011. /* Clear/re-enable interrupt */
  2012. ctrl->soc->ctlrdy_ack(ctrl->soc);
  2013. ctrl->soc->ctlrdy_set_enabled(ctrl->soc, true);
  2014. }
  2015. list_for_each_entry(host, &ctrl->host_list, node) {
  2016. struct nand_chip *chip = &host->chip;
  2017. struct mtd_info *mtd = nand_to_mtd(chip);
  2018. brcmnand_save_restore_cs_config(host, 1);
  2019. /* Reset the chip, required by some chips after power-up */
  2020. chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
  2021. }
  2022. return 0;
  2023. }
  2024. const struct dev_pm_ops brcmnand_pm_ops = {
  2025. .suspend = brcmnand_suspend,
  2026. .resume = brcmnand_resume,
  2027. };
  2028. EXPORT_SYMBOL_GPL(brcmnand_pm_ops);
  2029. static const struct of_device_id brcmnand_of_match[] = {
  2030. { .compatible = "brcm,brcmnand-v4.0" },
  2031. { .compatible = "brcm,brcmnand-v5.0" },
  2032. { .compatible = "brcm,brcmnand-v6.0" },
  2033. { .compatible = "brcm,brcmnand-v6.1" },
  2034. { .compatible = "brcm,brcmnand-v6.2" },
  2035. { .compatible = "brcm,brcmnand-v7.0" },
  2036. { .compatible = "brcm,brcmnand-v7.1" },
  2037. { .compatible = "brcm,brcmnand-v7.2" },
  2038. {},
  2039. };
  2040. MODULE_DEVICE_TABLE(of, brcmnand_of_match);
  2041. /***********************************************************************
  2042. * Platform driver setup (per controller)
  2043. ***********************************************************************/
  2044. int brcmnand_probe(struct platform_device *pdev, struct brcmnand_soc *soc)
  2045. {
  2046. struct device *dev = &pdev->dev;
  2047. struct device_node *dn = dev->of_node, *child;
  2048. struct brcmnand_controller *ctrl;
  2049. struct resource *res;
  2050. int ret;
  2051. /* We only support device-tree instantiation */
  2052. if (!dn)
  2053. return -ENODEV;
  2054. if (!of_match_node(brcmnand_of_match, dn))
  2055. return -ENODEV;
  2056. ctrl = devm_kzalloc(dev, sizeof(*ctrl), GFP_KERNEL);
  2057. if (!ctrl)
  2058. return -ENOMEM;
  2059. dev_set_drvdata(dev, ctrl);
  2060. ctrl->dev = dev;
  2061. init_completion(&ctrl->done);
  2062. init_completion(&ctrl->dma_done);
  2063. nand_hw_control_init(&ctrl->controller);
  2064. INIT_LIST_HEAD(&ctrl->host_list);
  2065. /* NAND register range */
  2066. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2067. ctrl->nand_base = devm_ioremap_resource(dev, res);
  2068. if (IS_ERR(ctrl->nand_base))
  2069. return PTR_ERR(ctrl->nand_base);
  2070. /* Enable clock before using NAND registers */
  2071. ctrl->clk = devm_clk_get(dev, "nand");
  2072. if (!IS_ERR(ctrl->clk)) {
  2073. ret = clk_prepare_enable(ctrl->clk);
  2074. if (ret)
  2075. return ret;
  2076. } else {
  2077. ret = PTR_ERR(ctrl->clk);
  2078. if (ret == -EPROBE_DEFER)
  2079. return ret;
  2080. ctrl->clk = NULL;
  2081. }
  2082. /* Initialize NAND revision */
  2083. ret = brcmnand_revision_init(ctrl);
  2084. if (ret)
  2085. goto err;
  2086. /*
  2087. * Most chips have this cache at a fixed offset within 'nand' block.
  2088. * Some must specify this region separately.
  2089. */
  2090. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "nand-cache");
  2091. if (res) {
  2092. ctrl->nand_fc = devm_ioremap_resource(dev, res);
  2093. if (IS_ERR(ctrl->nand_fc)) {
  2094. ret = PTR_ERR(ctrl->nand_fc);
  2095. goto err;
  2096. }
  2097. } else {
  2098. ctrl->nand_fc = ctrl->nand_base +
  2099. ctrl->reg_offsets[BRCMNAND_FC_BASE];
  2100. }
  2101. /* FLASH_DMA */
  2102. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "flash-dma");
  2103. if (res) {
  2104. ctrl->flash_dma_base = devm_ioremap_resource(dev, res);
  2105. if (IS_ERR(ctrl->flash_dma_base)) {
  2106. ret = PTR_ERR(ctrl->flash_dma_base);
  2107. goto err;
  2108. }
  2109. flash_dma_writel(ctrl, FLASH_DMA_MODE, 1); /* linked-list */
  2110. flash_dma_writel(ctrl, FLASH_DMA_ERROR_STATUS, 0);
  2111. /* Allocate descriptor(s) */
  2112. ctrl->dma_desc = dmam_alloc_coherent(dev,
  2113. sizeof(*ctrl->dma_desc),
  2114. &ctrl->dma_pa, GFP_KERNEL);
  2115. if (!ctrl->dma_desc) {
  2116. ret = -ENOMEM;
  2117. goto err;
  2118. }
  2119. ctrl->dma_irq = platform_get_irq(pdev, 1);
  2120. if ((int)ctrl->dma_irq < 0) {
  2121. dev_err(dev, "missing FLASH_DMA IRQ\n");
  2122. ret = -ENODEV;
  2123. goto err;
  2124. }
  2125. ret = devm_request_irq(dev, ctrl->dma_irq,
  2126. brcmnand_dma_irq, 0, DRV_NAME,
  2127. ctrl);
  2128. if (ret < 0) {
  2129. dev_err(dev, "can't allocate IRQ %d: error %d\n",
  2130. ctrl->dma_irq, ret);
  2131. goto err;
  2132. }
  2133. dev_info(dev, "enabling FLASH_DMA\n");
  2134. }
  2135. /* Disable automatic device ID config, direct addressing */
  2136. brcmnand_rmw_reg(ctrl, BRCMNAND_CS_SELECT,
  2137. CS_SELECT_AUTO_DEVICE_ID_CFG | 0xff, 0, 0);
  2138. /* Disable XOR addressing */
  2139. brcmnand_rmw_reg(ctrl, BRCMNAND_CS_XOR, 0xff, 0, 0);
  2140. if (ctrl->features & BRCMNAND_HAS_WP) {
  2141. /* Permanently disable write protection */
  2142. if (wp_on == 2)
  2143. brcmnand_set_wp(ctrl, false);
  2144. } else {
  2145. wp_on = 0;
  2146. }
  2147. /* IRQ */
  2148. ctrl->irq = platform_get_irq(pdev, 0);
  2149. if ((int)ctrl->irq < 0) {
  2150. dev_err(dev, "no IRQ defined\n");
  2151. ret = -ENODEV;
  2152. goto err;
  2153. }
  2154. /*
  2155. * Some SoCs integrate this controller (e.g., its interrupt bits) in
  2156. * interesting ways
  2157. */
  2158. if (soc) {
  2159. ctrl->soc = soc;
  2160. ret = devm_request_irq(dev, ctrl->irq, brcmnand_irq, 0,
  2161. DRV_NAME, ctrl);
  2162. /* Enable interrupt */
  2163. ctrl->soc->ctlrdy_ack(ctrl->soc);
  2164. ctrl->soc->ctlrdy_set_enabled(ctrl->soc, true);
  2165. } else {
  2166. /* Use standard interrupt infrastructure */
  2167. ret = devm_request_irq(dev, ctrl->irq, brcmnand_ctlrdy_irq, 0,
  2168. DRV_NAME, ctrl);
  2169. }
  2170. if (ret < 0) {
  2171. dev_err(dev, "can't allocate IRQ %d: error %d\n",
  2172. ctrl->irq, ret);
  2173. goto err;
  2174. }
  2175. for_each_available_child_of_node(dn, child) {
  2176. if (of_device_is_compatible(child, "brcm,nandcs")) {
  2177. struct brcmnand_host *host;
  2178. host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
  2179. if (!host) {
  2180. of_node_put(child);
  2181. ret = -ENOMEM;
  2182. goto err;
  2183. }
  2184. host->pdev = pdev;
  2185. host->ctrl = ctrl;
  2186. ret = brcmnand_init_cs(host, child);
  2187. if (ret) {
  2188. devm_kfree(dev, host);
  2189. continue; /* Try all chip-selects */
  2190. }
  2191. list_add_tail(&host->node, &ctrl->host_list);
  2192. }
  2193. }
  2194. /* No chip-selects could initialize properly */
  2195. if (list_empty(&ctrl->host_list)) {
  2196. ret = -ENODEV;
  2197. goto err;
  2198. }
  2199. return 0;
  2200. err:
  2201. clk_disable_unprepare(ctrl->clk);
  2202. return ret;
  2203. }
  2204. EXPORT_SYMBOL_GPL(brcmnand_probe);
  2205. int brcmnand_remove(struct platform_device *pdev)
  2206. {
  2207. struct brcmnand_controller *ctrl = dev_get_drvdata(&pdev->dev);
  2208. struct brcmnand_host *host;
  2209. list_for_each_entry(host, &ctrl->host_list, node)
  2210. nand_release(&host->chip);
  2211. clk_disable_unprepare(ctrl->clk);
  2212. dev_set_drvdata(&pdev->dev, NULL);
  2213. return 0;
  2214. }
  2215. EXPORT_SYMBOL_GPL(brcmnand_remove);
  2216. MODULE_LICENSE("GPL v2");
  2217. MODULE_AUTHOR("Kevin Cernekee");
  2218. MODULE_AUTHOR("Brian Norris");
  2219. MODULE_DESCRIPTION("NAND driver for Broadcom chips");
  2220. MODULE_ALIAS("platform:brcmnand");