omap-gpmc.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490
  1. /*
  2. * GPMC support functions
  3. *
  4. * Copyright (C) 2005-2006 Nokia Corporation
  5. *
  6. * Author: Juha Yrjola
  7. *
  8. * Copyright (C) 2009 Texas Instruments
  9. * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #include <linux/irq.h>
  16. #include <linux/kernel.h>
  17. #include <linux/init.h>
  18. #include <linux/err.h>
  19. #include <linux/clk.h>
  20. #include <linux/ioport.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/io.h>
  23. #include <linux/gpio/driver.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/irqdomain.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/of.h>
  28. #include <linux/of_address.h>
  29. #include <linux/of_device.h>
  30. #include <linux/of_platform.h>
  31. #include <linux/omap-gpmc.h>
  32. #include <linux/pm_runtime.h>
  33. #include <linux/platform_data/mtd-nand-omap2.h>
  34. #include <linux/platform_data/mtd-onenand-omap2.h>
  35. #include <asm/mach-types.h>
  36. #define DEVICE_NAME "omap-gpmc"
  37. /* GPMC register offsets */
  38. #define GPMC_REVISION 0x00
  39. #define GPMC_SYSCONFIG 0x10
  40. #define GPMC_SYSSTATUS 0x14
  41. #define GPMC_IRQSTATUS 0x18
  42. #define GPMC_IRQENABLE 0x1c
  43. #define GPMC_TIMEOUT_CONTROL 0x40
  44. #define GPMC_ERR_ADDRESS 0x44
  45. #define GPMC_ERR_TYPE 0x48
  46. #define GPMC_CONFIG 0x50
  47. #define GPMC_STATUS 0x54
  48. #define GPMC_PREFETCH_CONFIG1 0x1e0
  49. #define GPMC_PREFETCH_CONFIG2 0x1e4
  50. #define GPMC_PREFETCH_CONTROL 0x1ec
  51. #define GPMC_PREFETCH_STATUS 0x1f0
  52. #define GPMC_ECC_CONFIG 0x1f4
  53. #define GPMC_ECC_CONTROL 0x1f8
  54. #define GPMC_ECC_SIZE_CONFIG 0x1fc
  55. #define GPMC_ECC1_RESULT 0x200
  56. #define GPMC_ECC_BCH_RESULT_0 0x240 /* not available on OMAP2 */
  57. #define GPMC_ECC_BCH_RESULT_1 0x244 /* not available on OMAP2 */
  58. #define GPMC_ECC_BCH_RESULT_2 0x248 /* not available on OMAP2 */
  59. #define GPMC_ECC_BCH_RESULT_3 0x24c /* not available on OMAP2 */
  60. #define GPMC_ECC_BCH_RESULT_4 0x300 /* not available on OMAP2 */
  61. #define GPMC_ECC_BCH_RESULT_5 0x304 /* not available on OMAP2 */
  62. #define GPMC_ECC_BCH_RESULT_6 0x308 /* not available on OMAP2 */
  63. /* GPMC ECC control settings */
  64. #define GPMC_ECC_CTRL_ECCCLEAR 0x100
  65. #define GPMC_ECC_CTRL_ECCDISABLE 0x000
  66. #define GPMC_ECC_CTRL_ECCREG1 0x001
  67. #define GPMC_ECC_CTRL_ECCREG2 0x002
  68. #define GPMC_ECC_CTRL_ECCREG3 0x003
  69. #define GPMC_ECC_CTRL_ECCREG4 0x004
  70. #define GPMC_ECC_CTRL_ECCREG5 0x005
  71. #define GPMC_ECC_CTRL_ECCREG6 0x006
  72. #define GPMC_ECC_CTRL_ECCREG7 0x007
  73. #define GPMC_ECC_CTRL_ECCREG8 0x008
  74. #define GPMC_ECC_CTRL_ECCREG9 0x009
  75. #define GPMC_CONFIG_LIMITEDADDRESS BIT(1)
  76. #define GPMC_STATUS_EMPTYWRITEBUFFERSTATUS BIT(0)
  77. #define GPMC_CONFIG2_CSEXTRADELAY BIT(7)
  78. #define GPMC_CONFIG3_ADVEXTRADELAY BIT(7)
  79. #define GPMC_CONFIG4_OEEXTRADELAY BIT(7)
  80. #define GPMC_CONFIG4_WEEXTRADELAY BIT(23)
  81. #define GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN BIT(6)
  82. #define GPMC_CONFIG6_CYCLE2CYCLESAMECSEN BIT(7)
  83. #define GPMC_CS0_OFFSET 0x60
  84. #define GPMC_CS_SIZE 0x30
  85. #define GPMC_BCH_SIZE 0x10
  86. /*
  87. * The first 1MB of GPMC address space is typically mapped to
  88. * the internal ROM. Never allocate the first page, to
  89. * facilitate bug detection; even if we didn't boot from ROM.
  90. * As GPMC minimum partition size is 16MB we can only start from
  91. * there.
  92. */
  93. #define GPMC_MEM_START 0x1000000
  94. #define GPMC_MEM_END 0x3FFFFFFF
  95. #define GPMC_CHUNK_SHIFT 24 /* 16 MB */
  96. #define GPMC_SECTION_SHIFT 28 /* 128 MB */
  97. #define CS_NUM_SHIFT 24
  98. #define ENABLE_PREFETCH (0x1 << 7)
  99. #define DMA_MPU_MODE 2
  100. #define GPMC_REVISION_MAJOR(l) ((l >> 4) & 0xf)
  101. #define GPMC_REVISION_MINOR(l) (l & 0xf)
  102. #define GPMC_HAS_WR_ACCESS 0x1
  103. #define GPMC_HAS_WR_DATA_MUX_BUS 0x2
  104. #define GPMC_HAS_MUX_AAD 0x4
  105. #define GPMC_NR_WAITPINS 4
  106. #define GPMC_CS_CONFIG1 0x00
  107. #define GPMC_CS_CONFIG2 0x04
  108. #define GPMC_CS_CONFIG3 0x08
  109. #define GPMC_CS_CONFIG4 0x0c
  110. #define GPMC_CS_CONFIG5 0x10
  111. #define GPMC_CS_CONFIG6 0x14
  112. #define GPMC_CS_CONFIG7 0x18
  113. #define GPMC_CS_NAND_COMMAND 0x1c
  114. #define GPMC_CS_NAND_ADDRESS 0x20
  115. #define GPMC_CS_NAND_DATA 0x24
  116. /* Control Commands */
  117. #define GPMC_CONFIG_RDY_BSY 0x00000001
  118. #define GPMC_CONFIG_DEV_SIZE 0x00000002
  119. #define GPMC_CONFIG_DEV_TYPE 0x00000003
  120. #define GPMC_CONFIG1_WRAPBURST_SUPP (1 << 31)
  121. #define GPMC_CONFIG1_READMULTIPLE_SUPP (1 << 30)
  122. #define GPMC_CONFIG1_READTYPE_ASYNC (0 << 29)
  123. #define GPMC_CONFIG1_READTYPE_SYNC (1 << 29)
  124. #define GPMC_CONFIG1_WRITEMULTIPLE_SUPP (1 << 28)
  125. #define GPMC_CONFIG1_WRITETYPE_ASYNC (0 << 27)
  126. #define GPMC_CONFIG1_WRITETYPE_SYNC (1 << 27)
  127. #define GPMC_CONFIG1_CLKACTIVATIONTIME(val) ((val & 3) << 25)
  128. /** CLKACTIVATIONTIME Max Ticks */
  129. #define GPMC_CONFIG1_CLKACTIVATIONTIME_MAX 2
  130. #define GPMC_CONFIG1_PAGE_LEN(val) ((val & 3) << 23)
  131. /** ATTACHEDDEVICEPAGELENGTH Max Value */
  132. #define GPMC_CONFIG1_ATTACHEDDEVICEPAGELENGTH_MAX 2
  133. #define GPMC_CONFIG1_WAIT_READ_MON (1 << 22)
  134. #define GPMC_CONFIG1_WAIT_WRITE_MON (1 << 21)
  135. #define GPMC_CONFIG1_WAIT_MON_TIME(val) ((val & 3) << 18)
  136. /** WAITMONITORINGTIME Max Ticks */
  137. #define GPMC_CONFIG1_WAITMONITORINGTIME_MAX 2
  138. #define GPMC_CONFIG1_WAIT_PIN_SEL(val) ((val & 3) << 16)
  139. #define GPMC_CONFIG1_DEVICESIZE(val) ((val & 3) << 12)
  140. #define GPMC_CONFIG1_DEVICESIZE_16 GPMC_CONFIG1_DEVICESIZE(1)
  141. /** DEVICESIZE Max Value */
  142. #define GPMC_CONFIG1_DEVICESIZE_MAX 1
  143. #define GPMC_CONFIG1_DEVICETYPE(val) ((val & 3) << 10)
  144. #define GPMC_CONFIG1_DEVICETYPE_NOR GPMC_CONFIG1_DEVICETYPE(0)
  145. #define GPMC_CONFIG1_MUXTYPE(val) ((val & 3) << 8)
  146. #define GPMC_CONFIG1_TIME_PARA_GRAN (1 << 4)
  147. #define GPMC_CONFIG1_FCLK_DIV(val) (val & 3)
  148. #define GPMC_CONFIG1_FCLK_DIV2 (GPMC_CONFIG1_FCLK_DIV(1))
  149. #define GPMC_CONFIG1_FCLK_DIV3 (GPMC_CONFIG1_FCLK_DIV(2))
  150. #define GPMC_CONFIG1_FCLK_DIV4 (GPMC_CONFIG1_FCLK_DIV(3))
  151. #define GPMC_CONFIG7_CSVALID (1 << 6)
  152. #define GPMC_CONFIG7_BASEADDRESS_MASK 0x3f
  153. #define GPMC_CONFIG7_CSVALID_MASK BIT(6)
  154. #define GPMC_CONFIG7_MASKADDRESS_OFFSET 8
  155. #define GPMC_CONFIG7_MASKADDRESS_MASK (0xf << GPMC_CONFIG7_MASKADDRESS_OFFSET)
  156. /* All CONFIG7 bits except reserved bits */
  157. #define GPMC_CONFIG7_MASK (GPMC_CONFIG7_BASEADDRESS_MASK | \
  158. GPMC_CONFIG7_CSVALID_MASK | \
  159. GPMC_CONFIG7_MASKADDRESS_MASK)
  160. #define GPMC_DEVICETYPE_NOR 0
  161. #define GPMC_DEVICETYPE_NAND 2
  162. #define GPMC_CONFIG_WRITEPROTECT 0x00000010
  163. #define WR_RD_PIN_MONITORING 0x00600000
  164. /* ECC commands */
  165. #define GPMC_ECC_READ 0 /* Reset Hardware ECC for read */
  166. #define GPMC_ECC_WRITE 1 /* Reset Hardware ECC for write */
  167. #define GPMC_ECC_READSYN 2 /* Reset before syndrom is read back */
  168. #define GPMC_NR_NAND_IRQS 2 /* number of NAND specific IRQs */
  169. enum gpmc_clk_domain {
  170. GPMC_CD_FCLK,
  171. GPMC_CD_CLK
  172. };
  173. struct gpmc_cs_data {
  174. const char *name;
  175. #define GPMC_CS_RESERVED (1 << 0)
  176. u32 flags;
  177. struct resource mem;
  178. };
  179. /* Structure to save gpmc cs context */
  180. struct gpmc_cs_config {
  181. u32 config1;
  182. u32 config2;
  183. u32 config3;
  184. u32 config4;
  185. u32 config5;
  186. u32 config6;
  187. u32 config7;
  188. int is_valid;
  189. };
  190. /*
  191. * Structure to save/restore gpmc context
  192. * to support core off on OMAP3
  193. */
  194. struct omap3_gpmc_regs {
  195. u32 sysconfig;
  196. u32 irqenable;
  197. u32 timeout_ctrl;
  198. u32 config;
  199. u32 prefetch_config1;
  200. u32 prefetch_config2;
  201. u32 prefetch_control;
  202. struct gpmc_cs_config cs_context[GPMC_CS_NUM];
  203. };
  204. struct gpmc_device {
  205. struct device *dev;
  206. int irq;
  207. struct irq_chip irq_chip;
  208. struct gpio_chip gpio_chip;
  209. int nirqs;
  210. };
  211. static struct irq_domain *gpmc_irq_domain;
  212. static struct resource gpmc_mem_root;
  213. static struct gpmc_cs_data gpmc_cs[GPMC_CS_NUM];
  214. static DEFINE_SPINLOCK(gpmc_mem_lock);
  215. /* Define chip-selects as reserved by default until probe completes */
  216. static unsigned int gpmc_cs_num = GPMC_CS_NUM;
  217. static unsigned int gpmc_nr_waitpins;
  218. static resource_size_t phys_base, mem_size;
  219. static unsigned gpmc_capability;
  220. static void __iomem *gpmc_base;
  221. static struct clk *gpmc_l3_clk;
  222. static irqreturn_t gpmc_handle_irq(int irq, void *dev);
  223. static void gpmc_write_reg(int idx, u32 val)
  224. {
  225. writel_relaxed(val, gpmc_base + idx);
  226. }
  227. static u32 gpmc_read_reg(int idx)
  228. {
  229. return readl_relaxed(gpmc_base + idx);
  230. }
  231. void gpmc_cs_write_reg(int cs, int idx, u32 val)
  232. {
  233. void __iomem *reg_addr;
  234. reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
  235. writel_relaxed(val, reg_addr);
  236. }
  237. static u32 gpmc_cs_read_reg(int cs, int idx)
  238. {
  239. void __iomem *reg_addr;
  240. reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
  241. return readl_relaxed(reg_addr);
  242. }
  243. /* TODO: Add support for gpmc_fck to clock framework and use it */
  244. static unsigned long gpmc_get_fclk_period(void)
  245. {
  246. unsigned long rate = clk_get_rate(gpmc_l3_clk);
  247. rate /= 1000;
  248. rate = 1000000000 / rate; /* In picoseconds */
  249. return rate;
  250. }
  251. /**
  252. * gpmc_get_clk_period - get period of selected clock domain in ps
  253. * @cs Chip Select Region.
  254. * @cd Clock Domain.
  255. *
  256. * GPMC_CS_CONFIG1 GPMCFCLKDIVIDER for cs has to be setup
  257. * prior to calling this function with GPMC_CD_CLK.
  258. */
  259. static unsigned long gpmc_get_clk_period(int cs, enum gpmc_clk_domain cd)
  260. {
  261. unsigned long tick_ps = gpmc_get_fclk_period();
  262. u32 l;
  263. int div;
  264. switch (cd) {
  265. case GPMC_CD_CLK:
  266. /* get current clk divider */
  267. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
  268. div = (l & 0x03) + 1;
  269. /* get GPMC_CLK period */
  270. tick_ps *= div;
  271. break;
  272. case GPMC_CD_FCLK:
  273. /* FALL-THROUGH */
  274. default:
  275. break;
  276. }
  277. return tick_ps;
  278. }
  279. static unsigned int gpmc_ns_to_clk_ticks(unsigned int time_ns, int cs,
  280. enum gpmc_clk_domain cd)
  281. {
  282. unsigned long tick_ps;
  283. /* Calculate in picosecs to yield more exact results */
  284. tick_ps = gpmc_get_clk_period(cs, cd);
  285. return (time_ns * 1000 + tick_ps - 1) / tick_ps;
  286. }
  287. static unsigned int gpmc_ns_to_ticks(unsigned int time_ns)
  288. {
  289. return gpmc_ns_to_clk_ticks(time_ns, /* any CS */ 0, GPMC_CD_FCLK);
  290. }
  291. static unsigned int gpmc_ps_to_ticks(unsigned int time_ps)
  292. {
  293. unsigned long tick_ps;
  294. /* Calculate in picosecs to yield more exact results */
  295. tick_ps = gpmc_get_fclk_period();
  296. return (time_ps + tick_ps - 1) / tick_ps;
  297. }
  298. static unsigned int gpmc_clk_ticks_to_ns(unsigned int ticks, int cs,
  299. enum gpmc_clk_domain cd)
  300. {
  301. return ticks * gpmc_get_clk_period(cs, cd) / 1000;
  302. }
  303. unsigned int gpmc_ticks_to_ns(unsigned int ticks)
  304. {
  305. return gpmc_clk_ticks_to_ns(ticks, /* any CS */ 0, GPMC_CD_FCLK);
  306. }
  307. static unsigned int gpmc_ticks_to_ps(unsigned int ticks)
  308. {
  309. return ticks * gpmc_get_fclk_period();
  310. }
  311. static unsigned int gpmc_round_ps_to_ticks(unsigned int time_ps)
  312. {
  313. unsigned long ticks = gpmc_ps_to_ticks(time_ps);
  314. return ticks * gpmc_get_fclk_period();
  315. }
  316. static inline void gpmc_cs_modify_reg(int cs, int reg, u32 mask, bool value)
  317. {
  318. u32 l;
  319. l = gpmc_cs_read_reg(cs, reg);
  320. if (value)
  321. l |= mask;
  322. else
  323. l &= ~mask;
  324. gpmc_cs_write_reg(cs, reg, l);
  325. }
  326. static void gpmc_cs_bool_timings(int cs, const struct gpmc_bool_timings *p)
  327. {
  328. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG1,
  329. GPMC_CONFIG1_TIME_PARA_GRAN,
  330. p->time_para_granularity);
  331. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG2,
  332. GPMC_CONFIG2_CSEXTRADELAY, p->cs_extra_delay);
  333. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG3,
  334. GPMC_CONFIG3_ADVEXTRADELAY, p->adv_extra_delay);
  335. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4,
  336. GPMC_CONFIG4_OEEXTRADELAY, p->oe_extra_delay);
  337. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4,
  338. GPMC_CONFIG4_WEEXTRADELAY, p->we_extra_delay);
  339. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6,
  340. GPMC_CONFIG6_CYCLE2CYCLESAMECSEN,
  341. p->cycle2cyclesamecsen);
  342. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6,
  343. GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN,
  344. p->cycle2cyclediffcsen);
  345. }
  346. #ifdef CONFIG_OMAP_GPMC_DEBUG
  347. /**
  348. * get_gpmc_timing_reg - read a timing parameter and print DTS settings for it.
  349. * @cs: Chip Select Region
  350. * @reg: GPMC_CS_CONFIGn register offset.
  351. * @st_bit: Start Bit
  352. * @end_bit: End Bit. Must be >= @st_bit.
  353. * @ma:x Maximum parameter value (before optional @shift).
  354. * If 0, maximum is as high as @st_bit and @end_bit allow.
  355. * @name: DTS node name, w/o "gpmc,"
  356. * @cd: Clock Domain of timing parameter.
  357. * @shift: Parameter value left shifts @shift, which is then printed instead of value.
  358. * @raw: Raw Format Option.
  359. * raw format: gpmc,name = <value>
  360. * tick format: gpmc,name = <value> /&zwj;* x ns -- y ns; x ticks *&zwj;/
  361. * Where x ns -- y ns result in the same tick value.
  362. * When @max is exceeded, "invalid" is printed inside comment.
  363. * @noval: Parameter values equal to 0 are not printed.
  364. * @return: Specified timing parameter (after optional @shift).
  365. *
  366. */
  367. static int get_gpmc_timing_reg(
  368. /* timing specifiers */
  369. int cs, int reg, int st_bit, int end_bit, int max,
  370. const char *name, const enum gpmc_clk_domain cd,
  371. /* value transform */
  372. int shift,
  373. /* format specifiers */
  374. bool raw, bool noval)
  375. {
  376. u32 l;
  377. int nr_bits;
  378. int mask;
  379. bool invalid;
  380. l = gpmc_cs_read_reg(cs, reg);
  381. nr_bits = end_bit - st_bit + 1;
  382. mask = (1 << nr_bits) - 1;
  383. l = (l >> st_bit) & mask;
  384. if (!max)
  385. max = mask;
  386. invalid = l > max;
  387. if (shift)
  388. l = (shift << l);
  389. if (noval && (l == 0))
  390. return 0;
  391. if (!raw) {
  392. /* DTS tick format for timings in ns */
  393. unsigned int time_ns;
  394. unsigned int time_ns_min = 0;
  395. if (l)
  396. time_ns_min = gpmc_clk_ticks_to_ns(l - 1, cs, cd) + 1;
  397. time_ns = gpmc_clk_ticks_to_ns(l, cs, cd);
  398. pr_info("gpmc,%s = <%u> /* %u ns - %u ns; %i ticks%s*/\n",
  399. name, time_ns, time_ns_min, time_ns, l,
  400. invalid ? "; invalid " : " ");
  401. } else {
  402. /* raw format */
  403. pr_info("gpmc,%s = <%u>%s\n", name, l,
  404. invalid ? " /* invalid */" : "");
  405. }
  406. return l;
  407. }
  408. #define GPMC_PRINT_CONFIG(cs, config) \
  409. pr_info("cs%i %s: 0x%08x\n", cs, #config, \
  410. gpmc_cs_read_reg(cs, config))
  411. #define GPMC_GET_RAW(reg, st, end, field) \
  412. get_gpmc_timing_reg(cs, (reg), (st), (end), 0, field, GPMC_CD_FCLK, 0, 1, 0)
  413. #define GPMC_GET_RAW_MAX(reg, st, end, max, field) \
  414. get_gpmc_timing_reg(cs, (reg), (st), (end), (max), field, GPMC_CD_FCLK, 0, 1, 0)
  415. #define GPMC_GET_RAW_BOOL(reg, st, end, field) \
  416. get_gpmc_timing_reg(cs, (reg), (st), (end), 0, field, GPMC_CD_FCLK, 0, 1, 1)
  417. #define GPMC_GET_RAW_SHIFT_MAX(reg, st, end, shift, max, field) \
  418. get_gpmc_timing_reg(cs, (reg), (st), (end), (max), field, GPMC_CD_FCLK, (shift), 1, 1)
  419. #define GPMC_GET_TICKS(reg, st, end, field) \
  420. get_gpmc_timing_reg(cs, (reg), (st), (end), 0, field, GPMC_CD_FCLK, 0, 0, 0)
  421. #define GPMC_GET_TICKS_CD(reg, st, end, field, cd) \
  422. get_gpmc_timing_reg(cs, (reg), (st), (end), 0, field, (cd), 0, 0, 0)
  423. #define GPMC_GET_TICKS_CD_MAX(reg, st, end, max, field, cd) \
  424. get_gpmc_timing_reg(cs, (reg), (st), (end), (max), field, (cd), 0, 0, 0)
  425. static void gpmc_show_regs(int cs, const char *desc)
  426. {
  427. pr_info("gpmc cs%i %s:\n", cs, desc);
  428. GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG1);
  429. GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG2);
  430. GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG3);
  431. GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG4);
  432. GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG5);
  433. GPMC_PRINT_CONFIG(cs, GPMC_CS_CONFIG6);
  434. }
  435. /*
  436. * Note that gpmc,wait-pin handing wrongly assumes bit 8 is available,
  437. * see commit c9fb809.
  438. */
  439. static void gpmc_cs_show_timings(int cs, const char *desc)
  440. {
  441. gpmc_show_regs(cs, desc);
  442. pr_info("gpmc cs%i access configuration:\n", cs);
  443. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 4, 4, "time-para-granularity");
  444. GPMC_GET_RAW(GPMC_CS_CONFIG1, 8, 9, "mux-add-data");
  445. GPMC_GET_RAW_MAX(GPMC_CS_CONFIG1, 12, 13,
  446. GPMC_CONFIG1_DEVICESIZE_MAX, "device-width");
  447. GPMC_GET_RAW(GPMC_CS_CONFIG1, 16, 17, "wait-pin");
  448. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 21, 21, "wait-on-write");
  449. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 22, 22, "wait-on-read");
  450. GPMC_GET_RAW_SHIFT_MAX(GPMC_CS_CONFIG1, 23, 24, 4,
  451. GPMC_CONFIG1_ATTACHEDDEVICEPAGELENGTH_MAX,
  452. "burst-length");
  453. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 27, 27, "sync-write");
  454. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 28, 28, "burst-write");
  455. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 29, 29, "gpmc,sync-read");
  456. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 30, 30, "burst-read");
  457. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG1, 31, 31, "burst-wrap");
  458. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG2, 7, 7, "cs-extra-delay");
  459. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG3, 7, 7, "adv-extra-delay");
  460. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG4, 23, 23, "we-extra-delay");
  461. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG4, 7, 7, "oe-extra-delay");
  462. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG6, 7, 7, "cycle2cycle-samecsen");
  463. GPMC_GET_RAW_BOOL(GPMC_CS_CONFIG6, 6, 6, "cycle2cycle-diffcsen");
  464. pr_info("gpmc cs%i timings configuration:\n", cs);
  465. GPMC_GET_TICKS(GPMC_CS_CONFIG2, 0, 3, "cs-on-ns");
  466. GPMC_GET_TICKS(GPMC_CS_CONFIG2, 8, 12, "cs-rd-off-ns");
  467. GPMC_GET_TICKS(GPMC_CS_CONFIG2, 16, 20, "cs-wr-off-ns");
  468. GPMC_GET_TICKS(GPMC_CS_CONFIG3, 0, 3, "adv-on-ns");
  469. GPMC_GET_TICKS(GPMC_CS_CONFIG3, 8, 12, "adv-rd-off-ns");
  470. GPMC_GET_TICKS(GPMC_CS_CONFIG3, 16, 20, "adv-wr-off-ns");
  471. if (gpmc_capability & GPMC_HAS_MUX_AAD) {
  472. GPMC_GET_TICKS(GPMC_CS_CONFIG3, 4, 6, "adv-aad-mux-on-ns");
  473. GPMC_GET_TICKS(GPMC_CS_CONFIG3, 24, 26,
  474. "adv-aad-mux-rd-off-ns");
  475. GPMC_GET_TICKS(GPMC_CS_CONFIG3, 28, 30,
  476. "adv-aad-mux-wr-off-ns");
  477. }
  478. GPMC_GET_TICKS(GPMC_CS_CONFIG4, 0, 3, "oe-on-ns");
  479. GPMC_GET_TICKS(GPMC_CS_CONFIG4, 8, 12, "oe-off-ns");
  480. if (gpmc_capability & GPMC_HAS_MUX_AAD) {
  481. GPMC_GET_TICKS(GPMC_CS_CONFIG4, 4, 6, "oe-aad-mux-on-ns");
  482. GPMC_GET_TICKS(GPMC_CS_CONFIG4, 13, 15, "oe-aad-mux-off-ns");
  483. }
  484. GPMC_GET_TICKS(GPMC_CS_CONFIG4, 16, 19, "we-on-ns");
  485. GPMC_GET_TICKS(GPMC_CS_CONFIG4, 24, 28, "we-off-ns");
  486. GPMC_GET_TICKS(GPMC_CS_CONFIG5, 0, 4, "rd-cycle-ns");
  487. GPMC_GET_TICKS(GPMC_CS_CONFIG5, 8, 12, "wr-cycle-ns");
  488. GPMC_GET_TICKS(GPMC_CS_CONFIG5, 16, 20, "access-ns");
  489. GPMC_GET_TICKS(GPMC_CS_CONFIG5, 24, 27, "page-burst-access-ns");
  490. GPMC_GET_TICKS(GPMC_CS_CONFIG6, 0, 3, "bus-turnaround-ns");
  491. GPMC_GET_TICKS(GPMC_CS_CONFIG6, 8, 11, "cycle2cycle-delay-ns");
  492. GPMC_GET_TICKS_CD_MAX(GPMC_CS_CONFIG1, 18, 19,
  493. GPMC_CONFIG1_WAITMONITORINGTIME_MAX,
  494. "wait-monitoring-ns", GPMC_CD_CLK);
  495. GPMC_GET_TICKS_CD_MAX(GPMC_CS_CONFIG1, 25, 26,
  496. GPMC_CONFIG1_CLKACTIVATIONTIME_MAX,
  497. "clk-activation-ns", GPMC_CD_FCLK);
  498. GPMC_GET_TICKS(GPMC_CS_CONFIG6, 16, 19, "wr-data-mux-bus-ns");
  499. GPMC_GET_TICKS(GPMC_CS_CONFIG6, 24, 28, "wr-access-ns");
  500. }
  501. #else
  502. static inline void gpmc_cs_show_timings(int cs, const char *desc)
  503. {
  504. }
  505. #endif
  506. /**
  507. * set_gpmc_timing_reg - set a single timing parameter for Chip Select Region.
  508. * Caller is expected to have initialized CONFIG1 GPMCFCLKDIVIDER
  509. * prior to calling this function with @cd equal to GPMC_CD_CLK.
  510. *
  511. * @cs: Chip Select Region.
  512. * @reg: GPMC_CS_CONFIGn register offset.
  513. * @st_bit: Start Bit
  514. * @end_bit: End Bit. Must be >= @st_bit.
  515. * @max: Maximum parameter value.
  516. * If 0, maximum is as high as @st_bit and @end_bit allow.
  517. * @time: Timing parameter in ns.
  518. * @cd: Timing parameter clock domain.
  519. * @name: Timing parameter name.
  520. * @return: 0 on success, -1 on error.
  521. */
  522. static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit, int max,
  523. int time, enum gpmc_clk_domain cd, const char *name)
  524. {
  525. u32 l;
  526. int ticks, mask, nr_bits;
  527. if (time == 0)
  528. ticks = 0;
  529. else
  530. ticks = gpmc_ns_to_clk_ticks(time, cs, cd);
  531. nr_bits = end_bit - st_bit + 1;
  532. mask = (1 << nr_bits) - 1;
  533. if (!max)
  534. max = mask;
  535. if (ticks > max) {
  536. pr_err("%s: GPMC CS%d: %s %d ns, %d ticks > %d ticks\n",
  537. __func__, cs, name, time, ticks, max);
  538. return -1;
  539. }
  540. l = gpmc_cs_read_reg(cs, reg);
  541. #ifdef CONFIG_OMAP_GPMC_DEBUG
  542. pr_info(
  543. "GPMC CS%d: %-17s: %3d ticks, %3lu ns (was %3i ticks) %3d ns\n",
  544. cs, name, ticks, gpmc_get_clk_period(cs, cd) * ticks / 1000,
  545. (l >> st_bit) & mask, time);
  546. #endif
  547. l &= ~(mask << st_bit);
  548. l |= ticks << st_bit;
  549. gpmc_cs_write_reg(cs, reg, l);
  550. return 0;
  551. }
  552. #define GPMC_SET_ONE_CD_MAX(reg, st, end, max, field, cd) \
  553. if (set_gpmc_timing_reg(cs, (reg), (st), (end), (max), \
  554. t->field, (cd), #field) < 0) \
  555. return -1
  556. #define GPMC_SET_ONE(reg, st, end, field) \
  557. GPMC_SET_ONE_CD_MAX(reg, st, end, 0, field, GPMC_CD_FCLK)
  558. /**
  559. * gpmc_calc_waitmonitoring_divider - calculate proper GPMCFCLKDIVIDER based on WAITMONITORINGTIME
  560. * WAITMONITORINGTIME will be _at least_ as long as desired, i.e.
  561. * read --> don't sample bus too early
  562. * write --> data is longer on bus
  563. *
  564. * Formula:
  565. * gpmc_clk_div + 1 = ceil(ceil(waitmonitoringtime_ns / gpmc_fclk_ns)
  566. * / waitmonitoring_ticks)
  567. * WAITMONITORINGTIME resulting in 0 or 1 tick with div = 1 are caught by
  568. * div <= 0 check.
  569. *
  570. * @wait_monitoring: WAITMONITORINGTIME in ns.
  571. * @return: -1 on failure to scale, else proper divider > 0.
  572. */
  573. static int gpmc_calc_waitmonitoring_divider(unsigned int wait_monitoring)
  574. {
  575. int div = gpmc_ns_to_ticks(wait_monitoring);
  576. div += GPMC_CONFIG1_WAITMONITORINGTIME_MAX - 1;
  577. div /= GPMC_CONFIG1_WAITMONITORINGTIME_MAX;
  578. if (div > 4)
  579. return -1;
  580. if (div <= 0)
  581. div = 1;
  582. return div;
  583. }
  584. /**
  585. * gpmc_calc_divider - calculate GPMC_FCLK divider for sync_clk GPMC_CLK period.
  586. * @sync_clk: GPMC_CLK period in ps.
  587. * @return: Returns at least 1 if GPMC_FCLK can be divided to GPMC_CLK.
  588. * Else, returns -1.
  589. */
  590. int gpmc_calc_divider(unsigned int sync_clk)
  591. {
  592. int div = gpmc_ps_to_ticks(sync_clk);
  593. if (div > 4)
  594. return -1;
  595. if (div <= 0)
  596. div = 1;
  597. return div;
  598. }
  599. /**
  600. * gpmc_cs_set_timings - program timing parameters for Chip Select Region.
  601. * @cs: Chip Select Region.
  602. * @t: GPMC timing parameters.
  603. * @s: GPMC timing settings.
  604. * @return: 0 on success, -1 on error.
  605. */
  606. int gpmc_cs_set_timings(int cs, const struct gpmc_timings *t,
  607. const struct gpmc_settings *s)
  608. {
  609. int div;
  610. u32 l;
  611. div = gpmc_calc_divider(t->sync_clk);
  612. if (div < 0)
  613. return div;
  614. /*
  615. * See if we need to change the divider for waitmonitoringtime.
  616. *
  617. * Calculate GPMCFCLKDIVIDER independent of gpmc,sync-clk-ps in DT for
  618. * pure asynchronous accesses, i.e. both read and write asynchronous.
  619. * However, only do so if WAITMONITORINGTIME is actually used, i.e.
  620. * either WAITREADMONITORING or WAITWRITEMONITORING is set.
  621. *
  622. * This statement must not change div to scale async WAITMONITORINGTIME
  623. * to protect mixed synchronous and asynchronous accesses.
  624. *
  625. * We raise an error later if WAITMONITORINGTIME does not fit.
  626. */
  627. if (!s->sync_read && !s->sync_write &&
  628. (s->wait_on_read || s->wait_on_write)
  629. ) {
  630. div = gpmc_calc_waitmonitoring_divider(t->wait_monitoring);
  631. if (div < 0) {
  632. pr_err("%s: waitmonitoringtime %3d ns too large for greatest gpmcfclkdivider.\n",
  633. __func__,
  634. t->wait_monitoring
  635. );
  636. return -1;
  637. }
  638. }
  639. GPMC_SET_ONE(GPMC_CS_CONFIG2, 0, 3, cs_on);
  640. GPMC_SET_ONE(GPMC_CS_CONFIG2, 8, 12, cs_rd_off);
  641. GPMC_SET_ONE(GPMC_CS_CONFIG2, 16, 20, cs_wr_off);
  642. GPMC_SET_ONE(GPMC_CS_CONFIG3, 0, 3, adv_on);
  643. GPMC_SET_ONE(GPMC_CS_CONFIG3, 8, 12, adv_rd_off);
  644. GPMC_SET_ONE(GPMC_CS_CONFIG3, 16, 20, adv_wr_off);
  645. if (gpmc_capability & GPMC_HAS_MUX_AAD) {
  646. GPMC_SET_ONE(GPMC_CS_CONFIG3, 4, 6, adv_aad_mux_on);
  647. GPMC_SET_ONE(GPMC_CS_CONFIG3, 24, 26, adv_aad_mux_rd_off);
  648. GPMC_SET_ONE(GPMC_CS_CONFIG3, 28, 30, adv_aad_mux_wr_off);
  649. }
  650. GPMC_SET_ONE(GPMC_CS_CONFIG4, 0, 3, oe_on);
  651. GPMC_SET_ONE(GPMC_CS_CONFIG4, 8, 12, oe_off);
  652. if (gpmc_capability & GPMC_HAS_MUX_AAD) {
  653. GPMC_SET_ONE(GPMC_CS_CONFIG4, 4, 6, oe_aad_mux_on);
  654. GPMC_SET_ONE(GPMC_CS_CONFIG4, 13, 15, oe_aad_mux_off);
  655. }
  656. GPMC_SET_ONE(GPMC_CS_CONFIG4, 16, 19, we_on);
  657. GPMC_SET_ONE(GPMC_CS_CONFIG4, 24, 28, we_off);
  658. GPMC_SET_ONE(GPMC_CS_CONFIG5, 0, 4, rd_cycle);
  659. GPMC_SET_ONE(GPMC_CS_CONFIG5, 8, 12, wr_cycle);
  660. GPMC_SET_ONE(GPMC_CS_CONFIG5, 16, 20, access);
  661. GPMC_SET_ONE(GPMC_CS_CONFIG5, 24, 27, page_burst_access);
  662. GPMC_SET_ONE(GPMC_CS_CONFIG6, 0, 3, bus_turnaround);
  663. GPMC_SET_ONE(GPMC_CS_CONFIG6, 8, 11, cycle2cycle_delay);
  664. if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
  665. GPMC_SET_ONE(GPMC_CS_CONFIG6, 16, 19, wr_data_mux_bus);
  666. if (gpmc_capability & GPMC_HAS_WR_ACCESS)
  667. GPMC_SET_ONE(GPMC_CS_CONFIG6, 24, 28, wr_access);
  668. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
  669. l &= ~0x03;
  670. l |= (div - 1);
  671. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, l);
  672. GPMC_SET_ONE_CD_MAX(GPMC_CS_CONFIG1, 18, 19,
  673. GPMC_CONFIG1_WAITMONITORINGTIME_MAX,
  674. wait_monitoring, GPMC_CD_CLK);
  675. GPMC_SET_ONE_CD_MAX(GPMC_CS_CONFIG1, 25, 26,
  676. GPMC_CONFIG1_CLKACTIVATIONTIME_MAX,
  677. clk_activation, GPMC_CD_FCLK);
  678. #ifdef CONFIG_OMAP_GPMC_DEBUG
  679. pr_info("GPMC CS%d CLK period is %lu ns (div %d)\n",
  680. cs, (div * gpmc_get_fclk_period()) / 1000, div);
  681. #endif
  682. gpmc_cs_bool_timings(cs, &t->bool_timings);
  683. gpmc_cs_show_timings(cs, "after gpmc_cs_set_timings");
  684. return 0;
  685. }
  686. static int gpmc_cs_set_memconf(int cs, u32 base, u32 size)
  687. {
  688. u32 l;
  689. u32 mask;
  690. /*
  691. * Ensure that base address is aligned on a
  692. * boundary equal to or greater than size.
  693. */
  694. if (base & (size - 1))
  695. return -EINVAL;
  696. base >>= GPMC_CHUNK_SHIFT;
  697. mask = (1 << GPMC_SECTION_SHIFT) - size;
  698. mask >>= GPMC_CHUNK_SHIFT;
  699. mask <<= GPMC_CONFIG7_MASKADDRESS_OFFSET;
  700. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  701. l &= ~GPMC_CONFIG7_MASK;
  702. l |= base & GPMC_CONFIG7_BASEADDRESS_MASK;
  703. l |= mask & GPMC_CONFIG7_MASKADDRESS_MASK;
  704. l |= GPMC_CONFIG7_CSVALID;
  705. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
  706. return 0;
  707. }
  708. static void gpmc_cs_enable_mem(int cs)
  709. {
  710. u32 l;
  711. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  712. l |= GPMC_CONFIG7_CSVALID;
  713. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
  714. }
  715. static void gpmc_cs_disable_mem(int cs)
  716. {
  717. u32 l;
  718. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  719. l &= ~GPMC_CONFIG7_CSVALID;
  720. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
  721. }
  722. static void gpmc_cs_get_memconf(int cs, u32 *base, u32 *size)
  723. {
  724. u32 l;
  725. u32 mask;
  726. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  727. *base = (l & 0x3f) << GPMC_CHUNK_SHIFT;
  728. mask = (l >> 8) & 0x0f;
  729. *size = (1 << GPMC_SECTION_SHIFT) - (mask << GPMC_CHUNK_SHIFT);
  730. }
  731. static int gpmc_cs_mem_enabled(int cs)
  732. {
  733. u32 l;
  734. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  735. return l & GPMC_CONFIG7_CSVALID;
  736. }
  737. static void gpmc_cs_set_reserved(int cs, int reserved)
  738. {
  739. struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
  740. gpmc->flags |= GPMC_CS_RESERVED;
  741. }
  742. static bool gpmc_cs_reserved(int cs)
  743. {
  744. struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
  745. return gpmc->flags & GPMC_CS_RESERVED;
  746. }
  747. static void gpmc_cs_set_name(int cs, const char *name)
  748. {
  749. struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
  750. gpmc->name = name;
  751. }
  752. static const char *gpmc_cs_get_name(int cs)
  753. {
  754. struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
  755. return gpmc->name;
  756. }
  757. static unsigned long gpmc_mem_align(unsigned long size)
  758. {
  759. int order;
  760. size = (size - 1) >> (GPMC_CHUNK_SHIFT - 1);
  761. order = GPMC_CHUNK_SHIFT - 1;
  762. do {
  763. size >>= 1;
  764. order++;
  765. } while (size);
  766. size = 1 << order;
  767. return size;
  768. }
  769. static int gpmc_cs_insert_mem(int cs, unsigned long base, unsigned long size)
  770. {
  771. struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
  772. struct resource *res = &gpmc->mem;
  773. int r;
  774. size = gpmc_mem_align(size);
  775. spin_lock(&gpmc_mem_lock);
  776. res->start = base;
  777. res->end = base + size - 1;
  778. r = request_resource(&gpmc_mem_root, res);
  779. spin_unlock(&gpmc_mem_lock);
  780. return r;
  781. }
  782. static int gpmc_cs_delete_mem(int cs)
  783. {
  784. struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
  785. struct resource *res = &gpmc->mem;
  786. int r;
  787. spin_lock(&gpmc_mem_lock);
  788. r = release_resource(res);
  789. res->start = 0;
  790. res->end = 0;
  791. spin_unlock(&gpmc_mem_lock);
  792. return r;
  793. }
  794. /**
  795. * gpmc_cs_remap - remaps a chip-select physical base address
  796. * @cs: chip-select to remap
  797. * @base: physical base address to re-map chip-select to
  798. *
  799. * Re-maps a chip-select to a new physical base address specified by
  800. * "base". Returns 0 on success and appropriate negative error code
  801. * on failure.
  802. */
  803. static int gpmc_cs_remap(int cs, u32 base)
  804. {
  805. int ret;
  806. u32 old_base, size;
  807. if (cs > gpmc_cs_num) {
  808. pr_err("%s: requested chip-select is disabled\n", __func__);
  809. return -ENODEV;
  810. }
  811. /*
  812. * Make sure we ignore any device offsets from the GPMC partition
  813. * allocated for the chip select and that the new base confirms
  814. * to the GPMC 16MB minimum granularity.
  815. */
  816. base &= ~(SZ_16M - 1);
  817. gpmc_cs_get_memconf(cs, &old_base, &size);
  818. if (base == old_base)
  819. return 0;
  820. ret = gpmc_cs_delete_mem(cs);
  821. if (ret < 0)
  822. return ret;
  823. ret = gpmc_cs_insert_mem(cs, base, size);
  824. if (ret < 0)
  825. return ret;
  826. ret = gpmc_cs_set_memconf(cs, base, size);
  827. return ret;
  828. }
  829. int gpmc_cs_request(int cs, unsigned long size, unsigned long *base)
  830. {
  831. struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
  832. struct resource *res = &gpmc->mem;
  833. int r = -1;
  834. if (cs > gpmc_cs_num) {
  835. pr_err("%s: requested chip-select is disabled\n", __func__);
  836. return -ENODEV;
  837. }
  838. size = gpmc_mem_align(size);
  839. if (size > (1 << GPMC_SECTION_SHIFT))
  840. return -ENOMEM;
  841. spin_lock(&gpmc_mem_lock);
  842. if (gpmc_cs_reserved(cs)) {
  843. r = -EBUSY;
  844. goto out;
  845. }
  846. if (gpmc_cs_mem_enabled(cs))
  847. r = adjust_resource(res, res->start & ~(size - 1), size);
  848. if (r < 0)
  849. r = allocate_resource(&gpmc_mem_root, res, size, 0, ~0,
  850. size, NULL, NULL);
  851. if (r < 0)
  852. goto out;
  853. /* Disable CS while changing base address and size mask */
  854. gpmc_cs_disable_mem(cs);
  855. r = gpmc_cs_set_memconf(cs, res->start, resource_size(res));
  856. if (r < 0) {
  857. release_resource(res);
  858. goto out;
  859. }
  860. /* Enable CS */
  861. gpmc_cs_enable_mem(cs);
  862. *base = res->start;
  863. gpmc_cs_set_reserved(cs, 1);
  864. out:
  865. spin_unlock(&gpmc_mem_lock);
  866. return r;
  867. }
  868. EXPORT_SYMBOL(gpmc_cs_request);
  869. void gpmc_cs_free(int cs)
  870. {
  871. struct gpmc_cs_data *gpmc = &gpmc_cs[cs];
  872. struct resource *res = &gpmc->mem;
  873. spin_lock(&gpmc_mem_lock);
  874. if (cs >= gpmc_cs_num || cs < 0 || !gpmc_cs_reserved(cs)) {
  875. printk(KERN_ERR "Trying to free non-reserved GPMC CS%d\n", cs);
  876. BUG();
  877. spin_unlock(&gpmc_mem_lock);
  878. return;
  879. }
  880. gpmc_cs_disable_mem(cs);
  881. if (res->flags)
  882. release_resource(res);
  883. gpmc_cs_set_reserved(cs, 0);
  884. spin_unlock(&gpmc_mem_lock);
  885. }
  886. EXPORT_SYMBOL(gpmc_cs_free);
  887. /**
  888. * gpmc_configure - write request to configure gpmc
  889. * @cmd: command type
  890. * @wval: value to write
  891. * @return status of the operation
  892. */
  893. int gpmc_configure(int cmd, int wval)
  894. {
  895. u32 regval;
  896. switch (cmd) {
  897. case GPMC_CONFIG_WP:
  898. regval = gpmc_read_reg(GPMC_CONFIG);
  899. if (wval)
  900. regval &= ~GPMC_CONFIG_WRITEPROTECT; /* WP is ON */
  901. else
  902. regval |= GPMC_CONFIG_WRITEPROTECT; /* WP is OFF */
  903. gpmc_write_reg(GPMC_CONFIG, regval);
  904. break;
  905. default:
  906. pr_err("%s: command not supported\n", __func__);
  907. return -EINVAL;
  908. }
  909. return 0;
  910. }
  911. EXPORT_SYMBOL(gpmc_configure);
  912. void gpmc_update_nand_reg(struct gpmc_nand_regs *reg, int cs)
  913. {
  914. int i;
  915. reg->gpmc_status = NULL; /* deprecated */
  916. reg->gpmc_nand_command = gpmc_base + GPMC_CS0_OFFSET +
  917. GPMC_CS_NAND_COMMAND + GPMC_CS_SIZE * cs;
  918. reg->gpmc_nand_address = gpmc_base + GPMC_CS0_OFFSET +
  919. GPMC_CS_NAND_ADDRESS + GPMC_CS_SIZE * cs;
  920. reg->gpmc_nand_data = gpmc_base + GPMC_CS0_OFFSET +
  921. GPMC_CS_NAND_DATA + GPMC_CS_SIZE * cs;
  922. reg->gpmc_prefetch_config1 = gpmc_base + GPMC_PREFETCH_CONFIG1;
  923. reg->gpmc_prefetch_config2 = gpmc_base + GPMC_PREFETCH_CONFIG2;
  924. reg->gpmc_prefetch_control = gpmc_base + GPMC_PREFETCH_CONTROL;
  925. reg->gpmc_prefetch_status = gpmc_base + GPMC_PREFETCH_STATUS;
  926. reg->gpmc_ecc_config = gpmc_base + GPMC_ECC_CONFIG;
  927. reg->gpmc_ecc_control = gpmc_base + GPMC_ECC_CONTROL;
  928. reg->gpmc_ecc_size_config = gpmc_base + GPMC_ECC_SIZE_CONFIG;
  929. reg->gpmc_ecc1_result = gpmc_base + GPMC_ECC1_RESULT;
  930. for (i = 0; i < GPMC_BCH_NUM_REMAINDER; i++) {
  931. reg->gpmc_bch_result0[i] = gpmc_base + GPMC_ECC_BCH_RESULT_0 +
  932. GPMC_BCH_SIZE * i;
  933. reg->gpmc_bch_result1[i] = gpmc_base + GPMC_ECC_BCH_RESULT_1 +
  934. GPMC_BCH_SIZE * i;
  935. reg->gpmc_bch_result2[i] = gpmc_base + GPMC_ECC_BCH_RESULT_2 +
  936. GPMC_BCH_SIZE * i;
  937. reg->gpmc_bch_result3[i] = gpmc_base + GPMC_ECC_BCH_RESULT_3 +
  938. GPMC_BCH_SIZE * i;
  939. reg->gpmc_bch_result4[i] = gpmc_base + GPMC_ECC_BCH_RESULT_4 +
  940. i * GPMC_BCH_SIZE;
  941. reg->gpmc_bch_result5[i] = gpmc_base + GPMC_ECC_BCH_RESULT_5 +
  942. i * GPMC_BCH_SIZE;
  943. reg->gpmc_bch_result6[i] = gpmc_base + GPMC_ECC_BCH_RESULT_6 +
  944. i * GPMC_BCH_SIZE;
  945. }
  946. }
  947. static bool gpmc_nand_writebuffer_empty(void)
  948. {
  949. if (gpmc_read_reg(GPMC_STATUS) & GPMC_STATUS_EMPTYWRITEBUFFERSTATUS)
  950. return true;
  951. return false;
  952. }
  953. static struct gpmc_nand_ops nand_ops = {
  954. .nand_writebuffer_empty = gpmc_nand_writebuffer_empty,
  955. };
  956. /**
  957. * gpmc_omap_get_nand_ops - Get the GPMC NAND interface
  958. * @regs: the GPMC NAND register map exclusive for NAND use.
  959. * @cs: GPMC chip select number on which the NAND sits. The
  960. * register map returned will be specific to this chip select.
  961. *
  962. * Returns NULL on error e.g. invalid cs.
  963. */
  964. struct gpmc_nand_ops *gpmc_omap_get_nand_ops(struct gpmc_nand_regs *reg, int cs)
  965. {
  966. if (cs >= gpmc_cs_num)
  967. return NULL;
  968. gpmc_update_nand_reg(reg, cs);
  969. return &nand_ops;
  970. }
  971. EXPORT_SYMBOL_GPL(gpmc_omap_get_nand_ops);
  972. int gpmc_get_client_irq(unsigned irq_config)
  973. {
  974. if (!gpmc_irq_domain) {
  975. pr_warn("%s called before GPMC IRQ domain available\n",
  976. __func__);
  977. return 0;
  978. }
  979. /* we restrict this to NAND IRQs only */
  980. if (irq_config >= GPMC_NR_NAND_IRQS)
  981. return 0;
  982. return irq_create_mapping(gpmc_irq_domain, irq_config);
  983. }
  984. static int gpmc_irq_endis(unsigned long hwirq, bool endis)
  985. {
  986. u32 regval;
  987. /* bits GPMC_NR_NAND_IRQS to 8 are reserved */
  988. if (hwirq >= GPMC_NR_NAND_IRQS)
  989. hwirq += 8 - GPMC_NR_NAND_IRQS;
  990. regval = gpmc_read_reg(GPMC_IRQENABLE);
  991. if (endis)
  992. regval |= BIT(hwirq);
  993. else
  994. regval &= ~BIT(hwirq);
  995. gpmc_write_reg(GPMC_IRQENABLE, regval);
  996. return 0;
  997. }
  998. static void gpmc_irq_disable(struct irq_data *p)
  999. {
  1000. gpmc_irq_endis(p->hwirq, false);
  1001. }
  1002. static void gpmc_irq_enable(struct irq_data *p)
  1003. {
  1004. gpmc_irq_endis(p->hwirq, true);
  1005. }
  1006. static void gpmc_irq_mask(struct irq_data *d)
  1007. {
  1008. gpmc_irq_endis(d->hwirq, false);
  1009. }
  1010. static void gpmc_irq_unmask(struct irq_data *d)
  1011. {
  1012. gpmc_irq_endis(d->hwirq, true);
  1013. }
  1014. static void gpmc_irq_edge_config(unsigned long hwirq, bool rising_edge)
  1015. {
  1016. u32 regval;
  1017. /* NAND IRQs polarity is not configurable */
  1018. if (hwirq < GPMC_NR_NAND_IRQS)
  1019. return;
  1020. /* WAITPIN starts at BIT 8 */
  1021. hwirq += 8 - GPMC_NR_NAND_IRQS;
  1022. regval = gpmc_read_reg(GPMC_CONFIG);
  1023. if (rising_edge)
  1024. regval &= ~BIT(hwirq);
  1025. else
  1026. regval |= BIT(hwirq);
  1027. gpmc_write_reg(GPMC_CONFIG, regval);
  1028. }
  1029. static void gpmc_irq_ack(struct irq_data *d)
  1030. {
  1031. unsigned int hwirq = d->hwirq;
  1032. /* skip reserved bits */
  1033. if (hwirq >= GPMC_NR_NAND_IRQS)
  1034. hwirq += 8 - GPMC_NR_NAND_IRQS;
  1035. /* Setting bit to 1 clears (or Acks) the interrupt */
  1036. gpmc_write_reg(GPMC_IRQSTATUS, BIT(hwirq));
  1037. }
  1038. static int gpmc_irq_set_type(struct irq_data *d, unsigned int trigger)
  1039. {
  1040. /* can't set type for NAND IRQs */
  1041. if (d->hwirq < GPMC_NR_NAND_IRQS)
  1042. return -EINVAL;
  1043. /* We can support either rising or falling edge at a time */
  1044. if (trigger == IRQ_TYPE_EDGE_FALLING)
  1045. gpmc_irq_edge_config(d->hwirq, false);
  1046. else if (trigger == IRQ_TYPE_EDGE_RISING)
  1047. gpmc_irq_edge_config(d->hwirq, true);
  1048. else
  1049. return -EINVAL;
  1050. return 0;
  1051. }
  1052. static int gpmc_irq_map(struct irq_domain *d, unsigned int virq,
  1053. irq_hw_number_t hw)
  1054. {
  1055. struct gpmc_device *gpmc = d->host_data;
  1056. irq_set_chip_data(virq, gpmc);
  1057. if (hw < GPMC_NR_NAND_IRQS) {
  1058. irq_modify_status(virq, IRQ_NOREQUEST, IRQ_NOAUTOEN);
  1059. irq_set_chip_and_handler(virq, &gpmc->irq_chip,
  1060. handle_simple_irq);
  1061. } else {
  1062. irq_set_chip_and_handler(virq, &gpmc->irq_chip,
  1063. handle_edge_irq);
  1064. }
  1065. return 0;
  1066. }
  1067. static const struct irq_domain_ops gpmc_irq_domain_ops = {
  1068. .map = gpmc_irq_map,
  1069. .xlate = irq_domain_xlate_twocell,
  1070. };
  1071. static irqreturn_t gpmc_handle_irq(int irq, void *data)
  1072. {
  1073. int hwirq, virq;
  1074. u32 regval, regvalx;
  1075. struct gpmc_device *gpmc = data;
  1076. regval = gpmc_read_reg(GPMC_IRQSTATUS);
  1077. regvalx = regval;
  1078. if (!regval)
  1079. return IRQ_NONE;
  1080. for (hwirq = 0; hwirq < gpmc->nirqs; hwirq++) {
  1081. /* skip reserved status bits */
  1082. if (hwirq == GPMC_NR_NAND_IRQS)
  1083. regvalx >>= 8 - GPMC_NR_NAND_IRQS;
  1084. if (regvalx & BIT(hwirq)) {
  1085. virq = irq_find_mapping(gpmc_irq_domain, hwirq);
  1086. if (!virq) {
  1087. dev_warn(gpmc->dev,
  1088. "spurious irq detected hwirq %d, virq %d\n",
  1089. hwirq, virq);
  1090. }
  1091. generic_handle_irq(virq);
  1092. }
  1093. }
  1094. gpmc_write_reg(GPMC_IRQSTATUS, regval);
  1095. return IRQ_HANDLED;
  1096. }
  1097. static int gpmc_setup_irq(struct gpmc_device *gpmc)
  1098. {
  1099. u32 regval;
  1100. int rc;
  1101. /* Disable interrupts */
  1102. gpmc_write_reg(GPMC_IRQENABLE, 0);
  1103. /* clear interrupts */
  1104. regval = gpmc_read_reg(GPMC_IRQSTATUS);
  1105. gpmc_write_reg(GPMC_IRQSTATUS, regval);
  1106. gpmc->irq_chip.name = "gpmc";
  1107. gpmc->irq_chip.irq_enable = gpmc_irq_enable;
  1108. gpmc->irq_chip.irq_disable = gpmc_irq_disable;
  1109. gpmc->irq_chip.irq_ack = gpmc_irq_ack;
  1110. gpmc->irq_chip.irq_mask = gpmc_irq_mask;
  1111. gpmc->irq_chip.irq_unmask = gpmc_irq_unmask;
  1112. gpmc->irq_chip.irq_set_type = gpmc_irq_set_type;
  1113. gpmc_irq_domain = irq_domain_add_linear(gpmc->dev->of_node,
  1114. gpmc->nirqs,
  1115. &gpmc_irq_domain_ops,
  1116. gpmc);
  1117. if (!gpmc_irq_domain) {
  1118. dev_err(gpmc->dev, "IRQ domain add failed\n");
  1119. return -ENODEV;
  1120. }
  1121. rc = request_irq(gpmc->irq, gpmc_handle_irq, 0, "gpmc", gpmc);
  1122. if (rc) {
  1123. dev_err(gpmc->dev, "failed to request irq %d: %d\n",
  1124. gpmc->irq, rc);
  1125. irq_domain_remove(gpmc_irq_domain);
  1126. gpmc_irq_domain = NULL;
  1127. }
  1128. return rc;
  1129. }
  1130. static int gpmc_free_irq(struct gpmc_device *gpmc)
  1131. {
  1132. int hwirq;
  1133. free_irq(gpmc->irq, gpmc);
  1134. for (hwirq = 0; hwirq < gpmc->nirqs; hwirq++)
  1135. irq_dispose_mapping(irq_find_mapping(gpmc_irq_domain, hwirq));
  1136. irq_domain_remove(gpmc_irq_domain);
  1137. gpmc_irq_domain = NULL;
  1138. return 0;
  1139. }
  1140. static void gpmc_mem_exit(void)
  1141. {
  1142. int cs;
  1143. for (cs = 0; cs < gpmc_cs_num; cs++) {
  1144. if (!gpmc_cs_mem_enabled(cs))
  1145. continue;
  1146. gpmc_cs_delete_mem(cs);
  1147. }
  1148. }
  1149. static void gpmc_mem_init(void)
  1150. {
  1151. int cs;
  1152. gpmc_mem_root.start = GPMC_MEM_START;
  1153. gpmc_mem_root.end = GPMC_MEM_END;
  1154. /* Reserve all regions that has been set up by bootloader */
  1155. for (cs = 0; cs < gpmc_cs_num; cs++) {
  1156. u32 base, size;
  1157. if (!gpmc_cs_mem_enabled(cs))
  1158. continue;
  1159. gpmc_cs_get_memconf(cs, &base, &size);
  1160. if (gpmc_cs_insert_mem(cs, base, size)) {
  1161. pr_warn("%s: disabling cs %d mapped at 0x%x-0x%x\n",
  1162. __func__, cs, base, base + size);
  1163. gpmc_cs_disable_mem(cs);
  1164. }
  1165. }
  1166. }
  1167. static u32 gpmc_round_ps_to_sync_clk(u32 time_ps, u32 sync_clk)
  1168. {
  1169. u32 temp;
  1170. int div;
  1171. div = gpmc_calc_divider(sync_clk);
  1172. temp = gpmc_ps_to_ticks(time_ps);
  1173. temp = (temp + div - 1) / div;
  1174. return gpmc_ticks_to_ps(temp * div);
  1175. }
  1176. /* XXX: can the cycles be avoided ? */
  1177. static int gpmc_calc_sync_read_timings(struct gpmc_timings *gpmc_t,
  1178. struct gpmc_device_timings *dev_t,
  1179. bool mux)
  1180. {
  1181. u32 temp;
  1182. /* adv_rd_off */
  1183. temp = dev_t->t_avdp_r;
  1184. /* XXX: mux check required ? */
  1185. if (mux) {
  1186. /* XXX: t_avdp not to be required for sync, only added for tusb
  1187. * this indirectly necessitates requirement of t_avdp_r and
  1188. * t_avdp_w instead of having a single t_avdp
  1189. */
  1190. temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_avdh);
  1191. temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
  1192. }
  1193. gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp);
  1194. /* oe_on */
  1195. temp = dev_t->t_oeasu; /* XXX: remove this ? */
  1196. if (mux) {
  1197. temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_ach);
  1198. temp = max_t(u32, temp, gpmc_t->adv_rd_off +
  1199. gpmc_ticks_to_ps(dev_t->cyc_aavdh_oe));
  1200. }
  1201. gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp);
  1202. /* access */
  1203. /* XXX: any scope for improvement ?, by combining oe_on
  1204. * and clk_activation, need to check whether
  1205. * access = clk_activation + round to sync clk ?
  1206. */
  1207. temp = max_t(u32, dev_t->t_iaa, dev_t->cyc_iaa * gpmc_t->sync_clk);
  1208. temp += gpmc_t->clk_activation;
  1209. if (dev_t->cyc_oe)
  1210. temp = max_t(u32, temp, gpmc_t->oe_on +
  1211. gpmc_ticks_to_ps(dev_t->cyc_oe));
  1212. gpmc_t->access = gpmc_round_ps_to_ticks(temp);
  1213. gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1);
  1214. gpmc_t->cs_rd_off = gpmc_t->oe_off;
  1215. /* rd_cycle */
  1216. temp = max_t(u32, dev_t->t_cez_r, dev_t->t_oez);
  1217. temp = gpmc_round_ps_to_sync_clk(temp, gpmc_t->sync_clk) +
  1218. gpmc_t->access;
  1219. /* XXX: barter t_ce_rdyz with t_cez_r ? */
  1220. if (dev_t->t_ce_rdyz)
  1221. temp = max_t(u32, temp, gpmc_t->cs_rd_off + dev_t->t_ce_rdyz);
  1222. gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp);
  1223. return 0;
  1224. }
  1225. static int gpmc_calc_sync_write_timings(struct gpmc_timings *gpmc_t,
  1226. struct gpmc_device_timings *dev_t,
  1227. bool mux)
  1228. {
  1229. u32 temp;
  1230. /* adv_wr_off */
  1231. temp = dev_t->t_avdp_w;
  1232. if (mux) {
  1233. temp = max_t(u32, temp,
  1234. gpmc_t->clk_activation + dev_t->t_avdh);
  1235. temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
  1236. }
  1237. gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp);
  1238. /* wr_data_mux_bus */
  1239. temp = max_t(u32, dev_t->t_weasu,
  1240. gpmc_t->clk_activation + dev_t->t_rdyo);
  1241. /* XXX: shouldn't mux be kept as a whole for wr_data_mux_bus ?,
  1242. * and in that case remember to handle we_on properly
  1243. */
  1244. if (mux) {
  1245. temp = max_t(u32, temp,
  1246. gpmc_t->adv_wr_off + dev_t->t_aavdh);
  1247. temp = max_t(u32, temp, gpmc_t->adv_wr_off +
  1248. gpmc_ticks_to_ps(dev_t->cyc_aavdh_we));
  1249. }
  1250. gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp);
  1251. /* we_on */
  1252. if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
  1253. gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu);
  1254. else
  1255. gpmc_t->we_on = gpmc_t->wr_data_mux_bus;
  1256. /* wr_access */
  1257. /* XXX: gpmc_capability check reqd ? , even if not, will not harm */
  1258. gpmc_t->wr_access = gpmc_t->access;
  1259. /* we_off */
  1260. temp = gpmc_t->we_on + dev_t->t_wpl;
  1261. temp = max_t(u32, temp,
  1262. gpmc_t->wr_access + gpmc_ticks_to_ps(1));
  1263. temp = max_t(u32, temp,
  1264. gpmc_t->we_on + gpmc_ticks_to_ps(dev_t->cyc_wpl));
  1265. gpmc_t->we_off = gpmc_round_ps_to_ticks(temp);
  1266. gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off +
  1267. dev_t->t_wph);
  1268. /* wr_cycle */
  1269. temp = gpmc_round_ps_to_sync_clk(dev_t->t_cez_w, gpmc_t->sync_clk);
  1270. temp += gpmc_t->wr_access;
  1271. /* XXX: barter t_ce_rdyz with t_cez_w ? */
  1272. if (dev_t->t_ce_rdyz)
  1273. temp = max_t(u32, temp,
  1274. gpmc_t->cs_wr_off + dev_t->t_ce_rdyz);
  1275. gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp);
  1276. return 0;
  1277. }
  1278. static int gpmc_calc_async_read_timings(struct gpmc_timings *gpmc_t,
  1279. struct gpmc_device_timings *dev_t,
  1280. bool mux)
  1281. {
  1282. u32 temp;
  1283. /* adv_rd_off */
  1284. temp = dev_t->t_avdp_r;
  1285. if (mux)
  1286. temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
  1287. gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp);
  1288. /* oe_on */
  1289. temp = dev_t->t_oeasu;
  1290. if (mux)
  1291. temp = max_t(u32, temp,
  1292. gpmc_t->adv_rd_off + dev_t->t_aavdh);
  1293. gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp);
  1294. /* access */
  1295. temp = max_t(u32, dev_t->t_iaa, /* XXX: remove t_iaa in async ? */
  1296. gpmc_t->oe_on + dev_t->t_oe);
  1297. temp = max_t(u32, temp,
  1298. gpmc_t->cs_on + dev_t->t_ce);
  1299. temp = max_t(u32, temp,
  1300. gpmc_t->adv_on + dev_t->t_aa);
  1301. gpmc_t->access = gpmc_round_ps_to_ticks(temp);
  1302. gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1);
  1303. gpmc_t->cs_rd_off = gpmc_t->oe_off;
  1304. /* rd_cycle */
  1305. temp = max_t(u32, dev_t->t_rd_cycle,
  1306. gpmc_t->cs_rd_off + dev_t->t_cez_r);
  1307. temp = max_t(u32, temp, gpmc_t->oe_off + dev_t->t_oez);
  1308. gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp);
  1309. return 0;
  1310. }
  1311. static int gpmc_calc_async_write_timings(struct gpmc_timings *gpmc_t,
  1312. struct gpmc_device_timings *dev_t,
  1313. bool mux)
  1314. {
  1315. u32 temp;
  1316. /* adv_wr_off */
  1317. temp = dev_t->t_avdp_w;
  1318. if (mux)
  1319. temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
  1320. gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp);
  1321. /* wr_data_mux_bus */
  1322. temp = dev_t->t_weasu;
  1323. if (mux) {
  1324. temp = max_t(u32, temp, gpmc_t->adv_wr_off + dev_t->t_aavdh);
  1325. temp = max_t(u32, temp, gpmc_t->adv_wr_off +
  1326. gpmc_ticks_to_ps(dev_t->cyc_aavdh_we));
  1327. }
  1328. gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp);
  1329. /* we_on */
  1330. if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
  1331. gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu);
  1332. else
  1333. gpmc_t->we_on = gpmc_t->wr_data_mux_bus;
  1334. /* we_off */
  1335. temp = gpmc_t->we_on + dev_t->t_wpl;
  1336. gpmc_t->we_off = gpmc_round_ps_to_ticks(temp);
  1337. gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off +
  1338. dev_t->t_wph);
  1339. /* wr_cycle */
  1340. temp = max_t(u32, dev_t->t_wr_cycle,
  1341. gpmc_t->cs_wr_off + dev_t->t_cez_w);
  1342. gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp);
  1343. return 0;
  1344. }
  1345. static int gpmc_calc_sync_common_timings(struct gpmc_timings *gpmc_t,
  1346. struct gpmc_device_timings *dev_t)
  1347. {
  1348. u32 temp;
  1349. gpmc_t->sync_clk = gpmc_calc_divider(dev_t->clk) *
  1350. gpmc_get_fclk_period();
  1351. gpmc_t->page_burst_access = gpmc_round_ps_to_sync_clk(
  1352. dev_t->t_bacc,
  1353. gpmc_t->sync_clk);
  1354. temp = max_t(u32, dev_t->t_ces, dev_t->t_avds);
  1355. gpmc_t->clk_activation = gpmc_round_ps_to_ticks(temp);
  1356. if (gpmc_calc_divider(gpmc_t->sync_clk) != 1)
  1357. return 0;
  1358. if (dev_t->ce_xdelay)
  1359. gpmc_t->bool_timings.cs_extra_delay = true;
  1360. if (dev_t->avd_xdelay)
  1361. gpmc_t->bool_timings.adv_extra_delay = true;
  1362. if (dev_t->oe_xdelay)
  1363. gpmc_t->bool_timings.oe_extra_delay = true;
  1364. if (dev_t->we_xdelay)
  1365. gpmc_t->bool_timings.we_extra_delay = true;
  1366. return 0;
  1367. }
  1368. static int gpmc_calc_common_timings(struct gpmc_timings *gpmc_t,
  1369. struct gpmc_device_timings *dev_t,
  1370. bool sync)
  1371. {
  1372. u32 temp;
  1373. /* cs_on */
  1374. gpmc_t->cs_on = gpmc_round_ps_to_ticks(dev_t->t_ceasu);
  1375. /* adv_on */
  1376. temp = dev_t->t_avdasu;
  1377. if (dev_t->t_ce_avd)
  1378. temp = max_t(u32, temp,
  1379. gpmc_t->cs_on + dev_t->t_ce_avd);
  1380. gpmc_t->adv_on = gpmc_round_ps_to_ticks(temp);
  1381. if (sync)
  1382. gpmc_calc_sync_common_timings(gpmc_t, dev_t);
  1383. return 0;
  1384. }
  1385. /* TODO: remove this function once all peripherals are confirmed to
  1386. * work with generic timing. Simultaneously gpmc_cs_set_timings()
  1387. * has to be modified to handle timings in ps instead of ns
  1388. */
  1389. static void gpmc_convert_ps_to_ns(struct gpmc_timings *t)
  1390. {
  1391. t->cs_on /= 1000;
  1392. t->cs_rd_off /= 1000;
  1393. t->cs_wr_off /= 1000;
  1394. t->adv_on /= 1000;
  1395. t->adv_rd_off /= 1000;
  1396. t->adv_wr_off /= 1000;
  1397. t->we_on /= 1000;
  1398. t->we_off /= 1000;
  1399. t->oe_on /= 1000;
  1400. t->oe_off /= 1000;
  1401. t->page_burst_access /= 1000;
  1402. t->access /= 1000;
  1403. t->rd_cycle /= 1000;
  1404. t->wr_cycle /= 1000;
  1405. t->bus_turnaround /= 1000;
  1406. t->cycle2cycle_delay /= 1000;
  1407. t->wait_monitoring /= 1000;
  1408. t->clk_activation /= 1000;
  1409. t->wr_access /= 1000;
  1410. t->wr_data_mux_bus /= 1000;
  1411. }
  1412. int gpmc_calc_timings(struct gpmc_timings *gpmc_t,
  1413. struct gpmc_settings *gpmc_s,
  1414. struct gpmc_device_timings *dev_t)
  1415. {
  1416. bool mux = false, sync = false;
  1417. if (gpmc_s) {
  1418. mux = gpmc_s->mux_add_data ? true : false;
  1419. sync = (gpmc_s->sync_read || gpmc_s->sync_write);
  1420. }
  1421. memset(gpmc_t, 0, sizeof(*gpmc_t));
  1422. gpmc_calc_common_timings(gpmc_t, dev_t, sync);
  1423. if (gpmc_s && gpmc_s->sync_read)
  1424. gpmc_calc_sync_read_timings(gpmc_t, dev_t, mux);
  1425. else
  1426. gpmc_calc_async_read_timings(gpmc_t, dev_t, mux);
  1427. if (gpmc_s && gpmc_s->sync_write)
  1428. gpmc_calc_sync_write_timings(gpmc_t, dev_t, mux);
  1429. else
  1430. gpmc_calc_async_write_timings(gpmc_t, dev_t, mux);
  1431. /* TODO: remove, see function definition */
  1432. gpmc_convert_ps_to_ns(gpmc_t);
  1433. return 0;
  1434. }
  1435. /**
  1436. * gpmc_cs_program_settings - programs non-timing related settings
  1437. * @cs: GPMC chip-select to program
  1438. * @p: pointer to GPMC settings structure
  1439. *
  1440. * Programs non-timing related settings for a GPMC chip-select, such as
  1441. * bus-width, burst configuration, etc. Function should be called once
  1442. * for each chip-select that is being used and must be called before
  1443. * calling gpmc_cs_set_timings() as timing parameters in the CONFIG1
  1444. * register will be initialised to zero by this function. Returns 0 on
  1445. * success and appropriate negative error code on failure.
  1446. */
  1447. int gpmc_cs_program_settings(int cs, struct gpmc_settings *p)
  1448. {
  1449. u32 config1;
  1450. if ((!p->device_width) || (p->device_width > GPMC_DEVWIDTH_16BIT)) {
  1451. pr_err("%s: invalid width %d!", __func__, p->device_width);
  1452. return -EINVAL;
  1453. }
  1454. /* Address-data multiplexing not supported for NAND devices */
  1455. if (p->device_nand && p->mux_add_data) {
  1456. pr_err("%s: invalid configuration!\n", __func__);
  1457. return -EINVAL;
  1458. }
  1459. if ((p->mux_add_data > GPMC_MUX_AD) ||
  1460. ((p->mux_add_data == GPMC_MUX_AAD) &&
  1461. !(gpmc_capability & GPMC_HAS_MUX_AAD))) {
  1462. pr_err("%s: invalid multiplex configuration!\n", __func__);
  1463. return -EINVAL;
  1464. }
  1465. /* Page/burst mode supports lengths of 4, 8 and 16 bytes */
  1466. if (p->burst_read || p->burst_write) {
  1467. switch (p->burst_len) {
  1468. case GPMC_BURST_4:
  1469. case GPMC_BURST_8:
  1470. case GPMC_BURST_16:
  1471. break;
  1472. default:
  1473. pr_err("%s: invalid page/burst-length (%d)\n",
  1474. __func__, p->burst_len);
  1475. return -EINVAL;
  1476. }
  1477. }
  1478. if (p->wait_pin > gpmc_nr_waitpins) {
  1479. pr_err("%s: invalid wait-pin (%d)\n", __func__, p->wait_pin);
  1480. return -EINVAL;
  1481. }
  1482. config1 = GPMC_CONFIG1_DEVICESIZE((p->device_width - 1));
  1483. if (p->sync_read)
  1484. config1 |= GPMC_CONFIG1_READTYPE_SYNC;
  1485. if (p->sync_write)
  1486. config1 |= GPMC_CONFIG1_WRITETYPE_SYNC;
  1487. if (p->wait_on_read)
  1488. config1 |= GPMC_CONFIG1_WAIT_READ_MON;
  1489. if (p->wait_on_write)
  1490. config1 |= GPMC_CONFIG1_WAIT_WRITE_MON;
  1491. if (p->wait_on_read || p->wait_on_write)
  1492. config1 |= GPMC_CONFIG1_WAIT_PIN_SEL(p->wait_pin);
  1493. if (p->device_nand)
  1494. config1 |= GPMC_CONFIG1_DEVICETYPE(GPMC_DEVICETYPE_NAND);
  1495. if (p->mux_add_data)
  1496. config1 |= GPMC_CONFIG1_MUXTYPE(p->mux_add_data);
  1497. if (p->burst_read)
  1498. config1 |= GPMC_CONFIG1_READMULTIPLE_SUPP;
  1499. if (p->burst_write)
  1500. config1 |= GPMC_CONFIG1_WRITEMULTIPLE_SUPP;
  1501. if (p->burst_read || p->burst_write) {
  1502. config1 |= GPMC_CONFIG1_PAGE_LEN(p->burst_len >> 3);
  1503. config1 |= p->burst_wrap ? GPMC_CONFIG1_WRAPBURST_SUPP : 0;
  1504. }
  1505. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, config1);
  1506. return 0;
  1507. }
  1508. #ifdef CONFIG_OF
  1509. static const struct of_device_id gpmc_dt_ids[] = {
  1510. { .compatible = "ti,omap2420-gpmc" },
  1511. { .compatible = "ti,omap2430-gpmc" },
  1512. { .compatible = "ti,omap3430-gpmc" }, /* omap3430 & omap3630 */
  1513. { .compatible = "ti,omap4430-gpmc" }, /* omap4430 & omap4460 & omap543x */
  1514. { .compatible = "ti,am3352-gpmc" }, /* am335x devices */
  1515. { }
  1516. };
  1517. /**
  1518. * gpmc_read_settings_dt - read gpmc settings from device-tree
  1519. * @np: pointer to device-tree node for a gpmc child device
  1520. * @p: pointer to gpmc settings structure
  1521. *
  1522. * Reads the GPMC settings for a GPMC child device from device-tree and
  1523. * stores them in the GPMC settings structure passed. The GPMC settings
  1524. * structure is initialised to zero by this function and so any
  1525. * previously stored settings will be cleared.
  1526. */
  1527. void gpmc_read_settings_dt(struct device_node *np, struct gpmc_settings *p)
  1528. {
  1529. memset(p, 0, sizeof(struct gpmc_settings));
  1530. p->sync_read = of_property_read_bool(np, "gpmc,sync-read");
  1531. p->sync_write = of_property_read_bool(np, "gpmc,sync-write");
  1532. of_property_read_u32(np, "gpmc,device-width", &p->device_width);
  1533. of_property_read_u32(np, "gpmc,mux-add-data", &p->mux_add_data);
  1534. if (!of_property_read_u32(np, "gpmc,burst-length", &p->burst_len)) {
  1535. p->burst_wrap = of_property_read_bool(np, "gpmc,burst-wrap");
  1536. p->burst_read = of_property_read_bool(np, "gpmc,burst-read");
  1537. p->burst_write = of_property_read_bool(np, "gpmc,burst-write");
  1538. if (!p->burst_read && !p->burst_write)
  1539. pr_warn("%s: page/burst-length set but not used!\n",
  1540. __func__);
  1541. }
  1542. if (!of_property_read_u32(np, "gpmc,wait-pin", &p->wait_pin)) {
  1543. p->wait_on_read = of_property_read_bool(np,
  1544. "gpmc,wait-on-read");
  1545. p->wait_on_write = of_property_read_bool(np,
  1546. "gpmc,wait-on-write");
  1547. if (!p->wait_on_read && !p->wait_on_write)
  1548. pr_debug("%s: rd/wr wait monitoring not enabled!\n",
  1549. __func__);
  1550. }
  1551. }
  1552. static void __maybe_unused gpmc_read_timings_dt(struct device_node *np,
  1553. struct gpmc_timings *gpmc_t)
  1554. {
  1555. struct gpmc_bool_timings *p;
  1556. if (!np || !gpmc_t)
  1557. return;
  1558. memset(gpmc_t, 0, sizeof(*gpmc_t));
  1559. /* minimum clock period for syncronous mode */
  1560. of_property_read_u32(np, "gpmc,sync-clk-ps", &gpmc_t->sync_clk);
  1561. /* chip select timtings */
  1562. of_property_read_u32(np, "gpmc,cs-on-ns", &gpmc_t->cs_on);
  1563. of_property_read_u32(np, "gpmc,cs-rd-off-ns", &gpmc_t->cs_rd_off);
  1564. of_property_read_u32(np, "gpmc,cs-wr-off-ns", &gpmc_t->cs_wr_off);
  1565. /* ADV signal timings */
  1566. of_property_read_u32(np, "gpmc,adv-on-ns", &gpmc_t->adv_on);
  1567. of_property_read_u32(np, "gpmc,adv-rd-off-ns", &gpmc_t->adv_rd_off);
  1568. of_property_read_u32(np, "gpmc,adv-wr-off-ns", &gpmc_t->adv_wr_off);
  1569. of_property_read_u32(np, "gpmc,adv-aad-mux-on-ns",
  1570. &gpmc_t->adv_aad_mux_on);
  1571. of_property_read_u32(np, "gpmc,adv-aad-mux-rd-off-ns",
  1572. &gpmc_t->adv_aad_mux_rd_off);
  1573. of_property_read_u32(np, "gpmc,adv-aad-mux-wr-off-ns",
  1574. &gpmc_t->adv_aad_mux_wr_off);
  1575. /* WE signal timings */
  1576. of_property_read_u32(np, "gpmc,we-on-ns", &gpmc_t->we_on);
  1577. of_property_read_u32(np, "gpmc,we-off-ns", &gpmc_t->we_off);
  1578. /* OE signal timings */
  1579. of_property_read_u32(np, "gpmc,oe-on-ns", &gpmc_t->oe_on);
  1580. of_property_read_u32(np, "gpmc,oe-off-ns", &gpmc_t->oe_off);
  1581. of_property_read_u32(np, "gpmc,oe-aad-mux-on-ns",
  1582. &gpmc_t->oe_aad_mux_on);
  1583. of_property_read_u32(np, "gpmc,oe-aad-mux-off-ns",
  1584. &gpmc_t->oe_aad_mux_off);
  1585. /* access and cycle timings */
  1586. of_property_read_u32(np, "gpmc,page-burst-access-ns",
  1587. &gpmc_t->page_burst_access);
  1588. of_property_read_u32(np, "gpmc,access-ns", &gpmc_t->access);
  1589. of_property_read_u32(np, "gpmc,rd-cycle-ns", &gpmc_t->rd_cycle);
  1590. of_property_read_u32(np, "gpmc,wr-cycle-ns", &gpmc_t->wr_cycle);
  1591. of_property_read_u32(np, "gpmc,bus-turnaround-ns",
  1592. &gpmc_t->bus_turnaround);
  1593. of_property_read_u32(np, "gpmc,cycle2cycle-delay-ns",
  1594. &gpmc_t->cycle2cycle_delay);
  1595. of_property_read_u32(np, "gpmc,wait-monitoring-ns",
  1596. &gpmc_t->wait_monitoring);
  1597. of_property_read_u32(np, "gpmc,clk-activation-ns",
  1598. &gpmc_t->clk_activation);
  1599. /* only applicable to OMAP3+ */
  1600. of_property_read_u32(np, "gpmc,wr-access-ns", &gpmc_t->wr_access);
  1601. of_property_read_u32(np, "gpmc,wr-data-mux-bus-ns",
  1602. &gpmc_t->wr_data_mux_bus);
  1603. /* bool timing parameters */
  1604. p = &gpmc_t->bool_timings;
  1605. p->cycle2cyclediffcsen =
  1606. of_property_read_bool(np, "gpmc,cycle2cycle-diffcsen");
  1607. p->cycle2cyclesamecsen =
  1608. of_property_read_bool(np, "gpmc,cycle2cycle-samecsen");
  1609. p->we_extra_delay = of_property_read_bool(np, "gpmc,we-extra-delay");
  1610. p->oe_extra_delay = of_property_read_bool(np, "gpmc,oe-extra-delay");
  1611. p->adv_extra_delay = of_property_read_bool(np, "gpmc,adv-extra-delay");
  1612. p->cs_extra_delay = of_property_read_bool(np, "gpmc,cs-extra-delay");
  1613. p->time_para_granularity =
  1614. of_property_read_bool(np, "gpmc,time-para-granularity");
  1615. }
  1616. #if IS_ENABLED(CONFIG_MTD_ONENAND)
  1617. static int gpmc_probe_onenand_child(struct platform_device *pdev,
  1618. struct device_node *child)
  1619. {
  1620. u32 val;
  1621. struct omap_onenand_platform_data *gpmc_onenand_data;
  1622. if (of_property_read_u32(child, "reg", &val) < 0) {
  1623. dev_err(&pdev->dev, "%s has no 'reg' property\n",
  1624. child->full_name);
  1625. return -ENODEV;
  1626. }
  1627. gpmc_onenand_data = devm_kzalloc(&pdev->dev, sizeof(*gpmc_onenand_data),
  1628. GFP_KERNEL);
  1629. if (!gpmc_onenand_data)
  1630. return -ENOMEM;
  1631. gpmc_onenand_data->cs = val;
  1632. gpmc_onenand_data->of_node = child;
  1633. gpmc_onenand_data->dma_channel = -1;
  1634. if (!of_property_read_u32(child, "dma-channel", &val))
  1635. gpmc_onenand_data->dma_channel = val;
  1636. return gpmc_onenand_init(gpmc_onenand_data);
  1637. }
  1638. #else
  1639. static int gpmc_probe_onenand_child(struct platform_device *pdev,
  1640. struct device_node *child)
  1641. {
  1642. return 0;
  1643. }
  1644. #endif
  1645. /**
  1646. * gpmc_probe_generic_child - configures the gpmc for a child device
  1647. * @pdev: pointer to gpmc platform device
  1648. * @child: pointer to device-tree node for child device
  1649. *
  1650. * Allocates and configures a GPMC chip-select for a child device.
  1651. * Returns 0 on success and appropriate negative error code on failure.
  1652. */
  1653. static int gpmc_probe_generic_child(struct platform_device *pdev,
  1654. struct device_node *child)
  1655. {
  1656. struct gpmc_settings gpmc_s;
  1657. struct gpmc_timings gpmc_t;
  1658. struct resource res;
  1659. unsigned long base;
  1660. const char *name;
  1661. int ret, cs;
  1662. u32 val;
  1663. struct gpio_desc *waitpin_desc = NULL;
  1664. struct gpmc_device *gpmc = platform_get_drvdata(pdev);
  1665. if (of_property_read_u32(child, "reg", &cs) < 0) {
  1666. dev_err(&pdev->dev, "%s has no 'reg' property\n",
  1667. child->full_name);
  1668. return -ENODEV;
  1669. }
  1670. if (of_address_to_resource(child, 0, &res) < 0) {
  1671. dev_err(&pdev->dev, "%s has malformed 'reg' property\n",
  1672. child->full_name);
  1673. return -ENODEV;
  1674. }
  1675. /*
  1676. * Check if we have multiple instances of the same device
  1677. * on a single chip select. If so, use the already initialized
  1678. * timings.
  1679. */
  1680. name = gpmc_cs_get_name(cs);
  1681. if (name && child->name && of_node_cmp(child->name, name) == 0)
  1682. goto no_timings;
  1683. ret = gpmc_cs_request(cs, resource_size(&res), &base);
  1684. if (ret < 0) {
  1685. dev_err(&pdev->dev, "cannot request GPMC CS %d\n", cs);
  1686. return ret;
  1687. }
  1688. gpmc_cs_set_name(cs, child->name);
  1689. gpmc_read_settings_dt(child, &gpmc_s);
  1690. gpmc_read_timings_dt(child, &gpmc_t);
  1691. /*
  1692. * For some GPMC devices we still need to rely on the bootloader
  1693. * timings because the devices can be connected via FPGA.
  1694. * REVISIT: Add timing support from slls644g.pdf.
  1695. */
  1696. if (!gpmc_t.cs_rd_off) {
  1697. WARN(1, "enable GPMC debug to configure .dts timings for CS%i\n",
  1698. cs);
  1699. gpmc_cs_show_timings(cs,
  1700. "please add GPMC bootloader timings to .dts");
  1701. goto no_timings;
  1702. }
  1703. /* CS must be disabled while making changes to gpmc configuration */
  1704. gpmc_cs_disable_mem(cs);
  1705. /*
  1706. * FIXME: gpmc_cs_request() will map the CS to an arbitary
  1707. * location in the gpmc address space. When booting with
  1708. * device-tree we want the NOR flash to be mapped to the
  1709. * location specified in the device-tree blob. So remap the
  1710. * CS to this location. Once DT migration is complete should
  1711. * just make gpmc_cs_request() map a specific address.
  1712. */
  1713. ret = gpmc_cs_remap(cs, res.start);
  1714. if (ret < 0) {
  1715. dev_err(&pdev->dev, "cannot remap GPMC CS %d to %pa\n",
  1716. cs, &res.start);
  1717. if (res.start < GPMC_MEM_START) {
  1718. dev_info(&pdev->dev,
  1719. "GPMC CS %d start cannot be lesser than 0x%x\n",
  1720. cs, GPMC_MEM_START);
  1721. } else if (res.end > GPMC_MEM_END) {
  1722. dev_info(&pdev->dev,
  1723. "GPMC CS %d end cannot be greater than 0x%x\n",
  1724. cs, GPMC_MEM_END);
  1725. }
  1726. goto err;
  1727. }
  1728. if (of_node_cmp(child->name, "nand") == 0) {
  1729. /* Warn about older DT blobs with no compatible property */
  1730. if (!of_property_read_bool(child, "compatible")) {
  1731. dev_warn(&pdev->dev,
  1732. "Incompatible NAND node: missing compatible");
  1733. ret = -EINVAL;
  1734. goto err;
  1735. }
  1736. }
  1737. if (of_device_is_compatible(child, "ti,omap2-nand")) {
  1738. /* NAND specific setup */
  1739. val = 8;
  1740. of_property_read_u32(child, "nand-bus-width", &val);
  1741. switch (val) {
  1742. case 8:
  1743. gpmc_s.device_width = GPMC_DEVWIDTH_8BIT;
  1744. break;
  1745. case 16:
  1746. gpmc_s.device_width = GPMC_DEVWIDTH_16BIT;
  1747. break;
  1748. default:
  1749. dev_err(&pdev->dev, "%s: invalid 'nand-bus-width'\n",
  1750. child->name);
  1751. ret = -EINVAL;
  1752. goto err;
  1753. }
  1754. /* disable write protect */
  1755. gpmc_configure(GPMC_CONFIG_WP, 0);
  1756. gpmc_s.device_nand = true;
  1757. } else {
  1758. ret = of_property_read_u32(child, "bank-width",
  1759. &gpmc_s.device_width);
  1760. if (ret < 0)
  1761. goto err;
  1762. }
  1763. /* Reserve wait pin if it is required and valid */
  1764. if (gpmc_s.wait_on_read || gpmc_s.wait_on_write) {
  1765. unsigned int wait_pin = gpmc_s.wait_pin;
  1766. waitpin_desc = gpiochip_request_own_desc(&gpmc->gpio_chip,
  1767. wait_pin, "WAITPIN");
  1768. if (IS_ERR(waitpin_desc)) {
  1769. dev_err(&pdev->dev, "invalid wait-pin: %d\n", wait_pin);
  1770. ret = PTR_ERR(waitpin_desc);
  1771. goto err;
  1772. }
  1773. }
  1774. gpmc_cs_show_timings(cs, "before gpmc_cs_program_settings");
  1775. ret = gpmc_cs_program_settings(cs, &gpmc_s);
  1776. if (ret < 0)
  1777. goto err_cs;
  1778. ret = gpmc_cs_set_timings(cs, &gpmc_t, &gpmc_s);
  1779. if (ret) {
  1780. dev_err(&pdev->dev, "failed to set gpmc timings for: %s\n",
  1781. child->name);
  1782. goto err_cs;
  1783. }
  1784. /* Clear limited address i.e. enable A26-A11 */
  1785. val = gpmc_read_reg(GPMC_CONFIG);
  1786. val &= ~GPMC_CONFIG_LIMITEDADDRESS;
  1787. gpmc_write_reg(GPMC_CONFIG, val);
  1788. /* Enable CS region */
  1789. gpmc_cs_enable_mem(cs);
  1790. no_timings:
  1791. /* create platform device, NULL on error or when disabled */
  1792. if (!of_platform_device_create(child, NULL, &pdev->dev))
  1793. goto err_child_fail;
  1794. /* is child a common bus? */
  1795. if (of_match_node(of_default_bus_match_table, child))
  1796. /* create children and other common bus children */
  1797. if (of_platform_default_populate(child, NULL, &pdev->dev))
  1798. goto err_child_fail;
  1799. return 0;
  1800. err_child_fail:
  1801. dev_err(&pdev->dev, "failed to create gpmc child %s\n", child->name);
  1802. ret = -ENODEV;
  1803. err_cs:
  1804. gpiochip_free_own_desc(waitpin_desc);
  1805. err:
  1806. gpmc_cs_free(cs);
  1807. return ret;
  1808. }
  1809. static int gpmc_probe_dt(struct platform_device *pdev)
  1810. {
  1811. int ret;
  1812. const struct of_device_id *of_id =
  1813. of_match_device(gpmc_dt_ids, &pdev->dev);
  1814. if (!of_id)
  1815. return 0;
  1816. ret = of_property_read_u32(pdev->dev.of_node, "gpmc,num-cs",
  1817. &gpmc_cs_num);
  1818. if (ret < 0) {
  1819. pr_err("%s: number of chip-selects not defined\n", __func__);
  1820. return ret;
  1821. } else if (gpmc_cs_num < 1) {
  1822. pr_err("%s: all chip-selects are disabled\n", __func__);
  1823. return -EINVAL;
  1824. } else if (gpmc_cs_num > GPMC_CS_NUM) {
  1825. pr_err("%s: number of supported chip-selects cannot be > %d\n",
  1826. __func__, GPMC_CS_NUM);
  1827. return -EINVAL;
  1828. }
  1829. ret = of_property_read_u32(pdev->dev.of_node, "gpmc,num-waitpins",
  1830. &gpmc_nr_waitpins);
  1831. if (ret < 0) {
  1832. pr_err("%s: number of wait pins not found!\n", __func__);
  1833. return ret;
  1834. }
  1835. return 0;
  1836. }
  1837. static void gpmc_probe_dt_children(struct platform_device *pdev)
  1838. {
  1839. int ret;
  1840. struct device_node *child;
  1841. for_each_available_child_of_node(pdev->dev.of_node, child) {
  1842. if (!child->name)
  1843. continue;
  1844. if (of_node_cmp(child->name, "onenand") == 0)
  1845. ret = gpmc_probe_onenand_child(pdev, child);
  1846. else
  1847. ret = gpmc_probe_generic_child(pdev, child);
  1848. if (ret) {
  1849. dev_err(&pdev->dev, "failed to probe DT child '%s': %d\n",
  1850. child->name, ret);
  1851. }
  1852. }
  1853. }
  1854. #else
  1855. static int gpmc_probe_dt(struct platform_device *pdev)
  1856. {
  1857. return 0;
  1858. }
  1859. static void gpmc_probe_dt_children(struct platform_device *pdev)
  1860. {
  1861. }
  1862. #endif /* CONFIG_OF */
  1863. static int gpmc_gpio_get_direction(struct gpio_chip *chip, unsigned int offset)
  1864. {
  1865. return 1; /* we're input only */
  1866. }
  1867. static int gpmc_gpio_direction_input(struct gpio_chip *chip,
  1868. unsigned int offset)
  1869. {
  1870. return 0; /* we're input only */
  1871. }
  1872. static int gpmc_gpio_direction_output(struct gpio_chip *chip,
  1873. unsigned int offset, int value)
  1874. {
  1875. return -EINVAL; /* we're input only */
  1876. }
  1877. static void gpmc_gpio_set(struct gpio_chip *chip, unsigned int offset,
  1878. int value)
  1879. {
  1880. }
  1881. static int gpmc_gpio_get(struct gpio_chip *chip, unsigned int offset)
  1882. {
  1883. u32 reg;
  1884. offset += 8;
  1885. reg = gpmc_read_reg(GPMC_STATUS) & BIT(offset);
  1886. return !!reg;
  1887. }
  1888. static int gpmc_gpio_init(struct gpmc_device *gpmc)
  1889. {
  1890. int ret;
  1891. gpmc->gpio_chip.parent = gpmc->dev;
  1892. gpmc->gpio_chip.owner = THIS_MODULE;
  1893. gpmc->gpio_chip.label = DEVICE_NAME;
  1894. gpmc->gpio_chip.ngpio = gpmc_nr_waitpins;
  1895. gpmc->gpio_chip.get_direction = gpmc_gpio_get_direction;
  1896. gpmc->gpio_chip.direction_input = gpmc_gpio_direction_input;
  1897. gpmc->gpio_chip.direction_output = gpmc_gpio_direction_output;
  1898. gpmc->gpio_chip.set = gpmc_gpio_set;
  1899. gpmc->gpio_chip.get = gpmc_gpio_get;
  1900. gpmc->gpio_chip.base = -1;
  1901. ret = devm_gpiochip_add_data(gpmc->dev, &gpmc->gpio_chip, NULL);
  1902. if (ret < 0) {
  1903. dev_err(gpmc->dev, "could not register gpio chip: %d\n", ret);
  1904. return ret;
  1905. }
  1906. return 0;
  1907. }
  1908. static int gpmc_probe(struct platform_device *pdev)
  1909. {
  1910. int rc;
  1911. u32 l;
  1912. struct resource *res;
  1913. struct gpmc_device *gpmc;
  1914. gpmc = devm_kzalloc(&pdev->dev, sizeof(*gpmc), GFP_KERNEL);
  1915. if (!gpmc)
  1916. return -ENOMEM;
  1917. gpmc->dev = &pdev->dev;
  1918. platform_set_drvdata(pdev, gpmc);
  1919. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1920. if (res == NULL)
  1921. return -ENOENT;
  1922. phys_base = res->start;
  1923. mem_size = resource_size(res);
  1924. gpmc_base = devm_ioremap_resource(&pdev->dev, res);
  1925. if (IS_ERR(gpmc_base))
  1926. return PTR_ERR(gpmc_base);
  1927. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1928. if (!res) {
  1929. dev_err(&pdev->dev, "Failed to get resource: irq\n");
  1930. return -ENOENT;
  1931. }
  1932. gpmc->irq = res->start;
  1933. gpmc_l3_clk = devm_clk_get(&pdev->dev, "fck");
  1934. if (IS_ERR(gpmc_l3_clk)) {
  1935. dev_err(&pdev->dev, "Failed to get GPMC fck\n");
  1936. return PTR_ERR(gpmc_l3_clk);
  1937. }
  1938. if (!clk_get_rate(gpmc_l3_clk)) {
  1939. dev_err(&pdev->dev, "Invalid GPMC fck clock rate\n");
  1940. return -EINVAL;
  1941. }
  1942. if (pdev->dev.of_node) {
  1943. rc = gpmc_probe_dt(pdev);
  1944. if (rc)
  1945. return rc;
  1946. } else {
  1947. gpmc_cs_num = GPMC_CS_NUM;
  1948. gpmc_nr_waitpins = GPMC_NR_WAITPINS;
  1949. }
  1950. pm_runtime_enable(&pdev->dev);
  1951. pm_runtime_get_sync(&pdev->dev);
  1952. l = gpmc_read_reg(GPMC_REVISION);
  1953. /*
  1954. * FIXME: Once device-tree migration is complete the below flags
  1955. * should be populated based upon the device-tree compatible
  1956. * string. For now just use the IP revision. OMAP3+ devices have
  1957. * the wr_access and wr_data_mux_bus register fields. OMAP4+
  1958. * devices support the addr-addr-data multiplex protocol.
  1959. *
  1960. * GPMC IP revisions:
  1961. * - OMAP24xx = 2.0
  1962. * - OMAP3xxx = 5.0
  1963. * - OMAP44xx/54xx/AM335x = 6.0
  1964. */
  1965. if (GPMC_REVISION_MAJOR(l) > 0x4)
  1966. gpmc_capability = GPMC_HAS_WR_ACCESS | GPMC_HAS_WR_DATA_MUX_BUS;
  1967. if (GPMC_REVISION_MAJOR(l) > 0x5)
  1968. gpmc_capability |= GPMC_HAS_MUX_AAD;
  1969. dev_info(gpmc->dev, "GPMC revision %d.%d\n", GPMC_REVISION_MAJOR(l),
  1970. GPMC_REVISION_MINOR(l));
  1971. gpmc_mem_init();
  1972. rc = gpmc_gpio_init(gpmc);
  1973. if (rc)
  1974. goto gpio_init_failed;
  1975. gpmc->nirqs = GPMC_NR_NAND_IRQS + gpmc_nr_waitpins;
  1976. rc = gpmc_setup_irq(gpmc);
  1977. if (rc) {
  1978. dev_err(gpmc->dev, "gpmc_setup_irq failed\n");
  1979. goto gpio_init_failed;
  1980. }
  1981. gpmc_probe_dt_children(pdev);
  1982. return 0;
  1983. gpio_init_failed:
  1984. gpmc_mem_exit();
  1985. pm_runtime_put_sync(&pdev->dev);
  1986. pm_runtime_disable(&pdev->dev);
  1987. return rc;
  1988. }
  1989. static int gpmc_remove(struct platform_device *pdev)
  1990. {
  1991. struct gpmc_device *gpmc = platform_get_drvdata(pdev);
  1992. gpmc_free_irq(gpmc);
  1993. gpmc_mem_exit();
  1994. pm_runtime_put_sync(&pdev->dev);
  1995. pm_runtime_disable(&pdev->dev);
  1996. return 0;
  1997. }
  1998. #ifdef CONFIG_PM_SLEEP
  1999. static int gpmc_suspend(struct device *dev)
  2000. {
  2001. omap3_gpmc_save_context();
  2002. pm_runtime_put_sync(dev);
  2003. return 0;
  2004. }
  2005. static int gpmc_resume(struct device *dev)
  2006. {
  2007. pm_runtime_get_sync(dev);
  2008. omap3_gpmc_restore_context();
  2009. return 0;
  2010. }
  2011. #endif
  2012. static SIMPLE_DEV_PM_OPS(gpmc_pm_ops, gpmc_suspend, gpmc_resume);
  2013. static struct platform_driver gpmc_driver = {
  2014. .probe = gpmc_probe,
  2015. .remove = gpmc_remove,
  2016. .driver = {
  2017. .name = DEVICE_NAME,
  2018. .of_match_table = of_match_ptr(gpmc_dt_ids),
  2019. .pm = &gpmc_pm_ops,
  2020. },
  2021. };
  2022. static __init int gpmc_init(void)
  2023. {
  2024. return platform_driver_register(&gpmc_driver);
  2025. }
  2026. postcore_initcall(gpmc_init);
  2027. static struct omap3_gpmc_regs gpmc_context;
  2028. void omap3_gpmc_save_context(void)
  2029. {
  2030. int i;
  2031. if (!gpmc_base)
  2032. return;
  2033. gpmc_context.sysconfig = gpmc_read_reg(GPMC_SYSCONFIG);
  2034. gpmc_context.irqenable = gpmc_read_reg(GPMC_IRQENABLE);
  2035. gpmc_context.timeout_ctrl = gpmc_read_reg(GPMC_TIMEOUT_CONTROL);
  2036. gpmc_context.config = gpmc_read_reg(GPMC_CONFIG);
  2037. gpmc_context.prefetch_config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1);
  2038. gpmc_context.prefetch_config2 = gpmc_read_reg(GPMC_PREFETCH_CONFIG2);
  2039. gpmc_context.prefetch_control = gpmc_read_reg(GPMC_PREFETCH_CONTROL);
  2040. for (i = 0; i < gpmc_cs_num; i++) {
  2041. gpmc_context.cs_context[i].is_valid = gpmc_cs_mem_enabled(i);
  2042. if (gpmc_context.cs_context[i].is_valid) {
  2043. gpmc_context.cs_context[i].config1 =
  2044. gpmc_cs_read_reg(i, GPMC_CS_CONFIG1);
  2045. gpmc_context.cs_context[i].config2 =
  2046. gpmc_cs_read_reg(i, GPMC_CS_CONFIG2);
  2047. gpmc_context.cs_context[i].config3 =
  2048. gpmc_cs_read_reg(i, GPMC_CS_CONFIG3);
  2049. gpmc_context.cs_context[i].config4 =
  2050. gpmc_cs_read_reg(i, GPMC_CS_CONFIG4);
  2051. gpmc_context.cs_context[i].config5 =
  2052. gpmc_cs_read_reg(i, GPMC_CS_CONFIG5);
  2053. gpmc_context.cs_context[i].config6 =
  2054. gpmc_cs_read_reg(i, GPMC_CS_CONFIG6);
  2055. gpmc_context.cs_context[i].config7 =
  2056. gpmc_cs_read_reg(i, GPMC_CS_CONFIG7);
  2057. }
  2058. }
  2059. }
  2060. void omap3_gpmc_restore_context(void)
  2061. {
  2062. int i;
  2063. if (!gpmc_base)
  2064. return;
  2065. gpmc_write_reg(GPMC_SYSCONFIG, gpmc_context.sysconfig);
  2066. gpmc_write_reg(GPMC_IRQENABLE, gpmc_context.irqenable);
  2067. gpmc_write_reg(GPMC_TIMEOUT_CONTROL, gpmc_context.timeout_ctrl);
  2068. gpmc_write_reg(GPMC_CONFIG, gpmc_context.config);
  2069. gpmc_write_reg(GPMC_PREFETCH_CONFIG1, gpmc_context.prefetch_config1);
  2070. gpmc_write_reg(GPMC_PREFETCH_CONFIG2, gpmc_context.prefetch_config2);
  2071. gpmc_write_reg(GPMC_PREFETCH_CONTROL, gpmc_context.prefetch_control);
  2072. for (i = 0; i < gpmc_cs_num; i++) {
  2073. if (gpmc_context.cs_context[i].is_valid) {
  2074. gpmc_cs_write_reg(i, GPMC_CS_CONFIG1,
  2075. gpmc_context.cs_context[i].config1);
  2076. gpmc_cs_write_reg(i, GPMC_CS_CONFIG2,
  2077. gpmc_context.cs_context[i].config2);
  2078. gpmc_cs_write_reg(i, GPMC_CS_CONFIG3,
  2079. gpmc_context.cs_context[i].config3);
  2080. gpmc_cs_write_reg(i, GPMC_CS_CONFIG4,
  2081. gpmc_context.cs_context[i].config4);
  2082. gpmc_cs_write_reg(i, GPMC_CS_CONFIG5,
  2083. gpmc_context.cs_context[i].config5);
  2084. gpmc_cs_write_reg(i, GPMC_CS_CONFIG6,
  2085. gpmc_context.cs_context[i].config6);
  2086. gpmc_cs_write_reg(i, GPMC_CS_CONFIG7,
  2087. gpmc_context.cs_context[i].config7);
  2088. }
  2089. }
  2090. }