brcmnand.c 70 KB

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