omap-gpmc.c 73 KB

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