gpmc.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539
  1. /*
  2. * GPMC support functions
  3. *
  4. * Copyright (C) 2005-2006 Nokia Corporation
  5. *
  6. * Author: Juha Yrjola
  7. *
  8. * Copyright (C) 2009 Texas Instruments
  9. * Added OMAP4 support - Santosh Shilimkar <santosh.shilimkar@ti.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #undef DEBUG
  16. #include <linux/irq.h>
  17. #include <linux/kernel.h>
  18. #include <linux/init.h>
  19. #include <linux/err.h>
  20. #include <linux/clk.h>
  21. #include <linux/ioport.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/io.h>
  24. #include <linux/module.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/of.h>
  28. #include <linux/of_mtd.h>
  29. #include <linux/of_device.h>
  30. #include <linux/mtd/nand.h>
  31. #include <linux/platform_data/mtd-nand-omap2.h>
  32. #include <asm/mach-types.h>
  33. #include "soc.h"
  34. #include "common.h"
  35. #include "omap_device.h"
  36. #include "gpmc.h"
  37. #include "gpmc-nand.h"
  38. #include "gpmc-onenand.h"
  39. #define DEVICE_NAME "omap-gpmc"
  40. /* GPMC register offsets */
  41. #define GPMC_REVISION 0x00
  42. #define GPMC_SYSCONFIG 0x10
  43. #define GPMC_SYSSTATUS 0x14
  44. #define GPMC_IRQSTATUS 0x18
  45. #define GPMC_IRQENABLE 0x1c
  46. #define GPMC_TIMEOUT_CONTROL 0x40
  47. #define GPMC_ERR_ADDRESS 0x44
  48. #define GPMC_ERR_TYPE 0x48
  49. #define GPMC_CONFIG 0x50
  50. #define GPMC_STATUS 0x54
  51. #define GPMC_PREFETCH_CONFIG1 0x1e0
  52. #define GPMC_PREFETCH_CONFIG2 0x1e4
  53. #define GPMC_PREFETCH_CONTROL 0x1ec
  54. #define GPMC_PREFETCH_STATUS 0x1f0
  55. #define GPMC_ECC_CONFIG 0x1f4
  56. #define GPMC_ECC_CONTROL 0x1f8
  57. #define GPMC_ECC_SIZE_CONFIG 0x1fc
  58. #define GPMC_ECC1_RESULT 0x200
  59. #define GPMC_ECC_BCH_RESULT_0 0x240 /* not available on OMAP2 */
  60. #define GPMC_ECC_BCH_RESULT_1 0x244 /* not available on OMAP2 */
  61. #define GPMC_ECC_BCH_RESULT_2 0x248 /* not available on OMAP2 */
  62. #define GPMC_ECC_BCH_RESULT_3 0x24c /* not available on OMAP2 */
  63. /* GPMC ECC control settings */
  64. #define GPMC_ECC_CTRL_ECCCLEAR 0x100
  65. #define GPMC_ECC_CTRL_ECCDISABLE 0x000
  66. #define GPMC_ECC_CTRL_ECCREG1 0x001
  67. #define GPMC_ECC_CTRL_ECCREG2 0x002
  68. #define GPMC_ECC_CTRL_ECCREG3 0x003
  69. #define GPMC_ECC_CTRL_ECCREG4 0x004
  70. #define GPMC_ECC_CTRL_ECCREG5 0x005
  71. #define GPMC_ECC_CTRL_ECCREG6 0x006
  72. #define GPMC_ECC_CTRL_ECCREG7 0x007
  73. #define GPMC_ECC_CTRL_ECCREG8 0x008
  74. #define GPMC_ECC_CTRL_ECCREG9 0x009
  75. #define GPMC_CONFIG2_CSEXTRADELAY BIT(7)
  76. #define GPMC_CONFIG3_ADVEXTRADELAY BIT(7)
  77. #define GPMC_CONFIG4_OEEXTRADELAY BIT(7)
  78. #define GPMC_CONFIG4_WEEXTRADELAY BIT(23)
  79. #define GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN BIT(6)
  80. #define GPMC_CONFIG6_CYCLE2CYCLESAMECSEN BIT(7)
  81. #define GPMC_CS0_OFFSET 0x60
  82. #define GPMC_CS_SIZE 0x30
  83. #define GPMC_BCH_SIZE 0x10
  84. #define GPMC_MEM_START 0x00000000
  85. #define GPMC_MEM_END 0x3FFFFFFF
  86. #define BOOT_ROM_SPACE 0x100000 /* 1MB */
  87. #define GPMC_CHUNK_SHIFT 24 /* 16 MB */
  88. #define GPMC_SECTION_SHIFT 28 /* 128 MB */
  89. #define CS_NUM_SHIFT 24
  90. #define ENABLE_PREFETCH (0x1 << 7)
  91. #define DMA_MPU_MODE 2
  92. #define GPMC_REVISION_MAJOR(l) ((l >> 4) & 0xf)
  93. #define GPMC_REVISION_MINOR(l) (l & 0xf)
  94. #define GPMC_HAS_WR_ACCESS 0x1
  95. #define GPMC_HAS_WR_DATA_MUX_BUS 0x2
  96. /* XXX: Only NAND irq has been considered,currently these are the only ones used
  97. */
  98. #define GPMC_NR_IRQ 2
  99. struct gpmc_client_irq {
  100. unsigned irq;
  101. u32 bitmask;
  102. };
  103. /* Structure to save gpmc cs context */
  104. struct gpmc_cs_config {
  105. u32 config1;
  106. u32 config2;
  107. u32 config3;
  108. u32 config4;
  109. u32 config5;
  110. u32 config6;
  111. u32 config7;
  112. int is_valid;
  113. };
  114. /*
  115. * Structure to save/restore gpmc context
  116. * to support core off on OMAP3
  117. */
  118. struct omap3_gpmc_regs {
  119. u32 sysconfig;
  120. u32 irqenable;
  121. u32 timeout_ctrl;
  122. u32 config;
  123. u32 prefetch_config1;
  124. u32 prefetch_config2;
  125. u32 prefetch_control;
  126. struct gpmc_cs_config cs_context[GPMC_CS_NUM];
  127. };
  128. static struct gpmc_client_irq gpmc_client_irq[GPMC_NR_IRQ];
  129. static struct irq_chip gpmc_irq_chip;
  130. static unsigned gpmc_irq_start;
  131. static struct resource gpmc_mem_root;
  132. static struct resource gpmc_cs_mem[GPMC_CS_NUM];
  133. static DEFINE_SPINLOCK(gpmc_mem_lock);
  134. /* Define chip-selects as reserved by default until probe completes */
  135. static unsigned int gpmc_cs_map = ((1 << GPMC_CS_NUM) - 1);
  136. static struct device *gpmc_dev;
  137. static int gpmc_irq;
  138. static resource_size_t phys_base, mem_size;
  139. static unsigned gpmc_capability;
  140. static void __iomem *gpmc_base;
  141. static struct clk *gpmc_l3_clk;
  142. static irqreturn_t gpmc_handle_irq(int irq, void *dev);
  143. static void gpmc_write_reg(int idx, u32 val)
  144. {
  145. __raw_writel(val, gpmc_base + idx);
  146. }
  147. static u32 gpmc_read_reg(int idx)
  148. {
  149. return __raw_readl(gpmc_base + idx);
  150. }
  151. void gpmc_cs_write_reg(int cs, int idx, u32 val)
  152. {
  153. void __iomem *reg_addr;
  154. reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
  155. __raw_writel(val, reg_addr);
  156. }
  157. u32 gpmc_cs_read_reg(int cs, int idx)
  158. {
  159. void __iomem *reg_addr;
  160. reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
  161. return __raw_readl(reg_addr);
  162. }
  163. /* TODO: Add support for gpmc_fck to clock framework and use it */
  164. unsigned long gpmc_get_fclk_period(void)
  165. {
  166. unsigned long rate = clk_get_rate(gpmc_l3_clk);
  167. if (rate == 0) {
  168. printk(KERN_WARNING "gpmc_l3_clk not enabled\n");
  169. return 0;
  170. }
  171. rate /= 1000;
  172. rate = 1000000000 / rate; /* In picoseconds */
  173. return rate;
  174. }
  175. unsigned int gpmc_ns_to_ticks(unsigned int time_ns)
  176. {
  177. unsigned long tick_ps;
  178. /* Calculate in picosecs to yield more exact results */
  179. tick_ps = gpmc_get_fclk_period();
  180. return (time_ns * 1000 + tick_ps - 1) / tick_ps;
  181. }
  182. unsigned int gpmc_ps_to_ticks(unsigned int time_ps)
  183. {
  184. unsigned long tick_ps;
  185. /* Calculate in picosecs to yield more exact results */
  186. tick_ps = gpmc_get_fclk_period();
  187. return (time_ps + tick_ps - 1) / tick_ps;
  188. }
  189. unsigned int gpmc_ticks_to_ns(unsigned int ticks)
  190. {
  191. return ticks * gpmc_get_fclk_period() / 1000;
  192. }
  193. unsigned int gpmc_round_ns_to_ticks(unsigned int time_ns)
  194. {
  195. unsigned long ticks = gpmc_ns_to_ticks(time_ns);
  196. return ticks * gpmc_get_fclk_period() / 1000;
  197. }
  198. static unsigned int gpmc_ticks_to_ps(unsigned int ticks)
  199. {
  200. return ticks * gpmc_get_fclk_period();
  201. }
  202. static unsigned int gpmc_round_ps_to_ticks(unsigned int time_ps)
  203. {
  204. unsigned long ticks = gpmc_ps_to_ticks(time_ps);
  205. return ticks * gpmc_get_fclk_period();
  206. }
  207. static inline void gpmc_cs_modify_reg(int cs, int reg, u32 mask, bool value)
  208. {
  209. u32 l;
  210. l = gpmc_cs_read_reg(cs, reg);
  211. if (value)
  212. l |= mask;
  213. else
  214. l &= ~mask;
  215. gpmc_cs_write_reg(cs, reg, l);
  216. }
  217. static void gpmc_cs_bool_timings(int cs, const struct gpmc_bool_timings *p)
  218. {
  219. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG1,
  220. GPMC_CONFIG1_TIME_PARA_GRAN,
  221. p->time_para_granularity);
  222. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG2,
  223. GPMC_CONFIG2_CSEXTRADELAY, p->cs_extra_delay);
  224. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG3,
  225. GPMC_CONFIG3_ADVEXTRADELAY, p->adv_extra_delay);
  226. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4,
  227. GPMC_CONFIG4_OEEXTRADELAY, p->oe_extra_delay);
  228. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG4,
  229. GPMC_CONFIG4_OEEXTRADELAY, p->we_extra_delay);
  230. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6,
  231. GPMC_CONFIG6_CYCLE2CYCLESAMECSEN,
  232. p->cycle2cyclesamecsen);
  233. gpmc_cs_modify_reg(cs, GPMC_CS_CONFIG6,
  234. GPMC_CONFIG6_CYCLE2CYCLEDIFFCSEN,
  235. p->cycle2cyclediffcsen);
  236. }
  237. #ifdef DEBUG
  238. static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
  239. int time, const char *name)
  240. #else
  241. static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
  242. int time)
  243. #endif
  244. {
  245. u32 l;
  246. int ticks, mask, nr_bits;
  247. if (time == 0)
  248. ticks = 0;
  249. else
  250. ticks = gpmc_ns_to_ticks(time);
  251. nr_bits = end_bit - st_bit + 1;
  252. if (ticks >= 1 << nr_bits) {
  253. #ifdef DEBUG
  254. printk(KERN_INFO "GPMC CS%d: %-10s* %3d ns, %3d ticks >= %d\n",
  255. cs, name, time, ticks, 1 << nr_bits);
  256. #endif
  257. return -1;
  258. }
  259. mask = (1 << nr_bits) - 1;
  260. l = gpmc_cs_read_reg(cs, reg);
  261. #ifdef DEBUG
  262. printk(KERN_INFO
  263. "GPMC CS%d: %-10s: %3d ticks, %3lu ns (was %3i ticks) %3d ns\n",
  264. cs, name, ticks, gpmc_get_fclk_period() * ticks / 1000,
  265. (l >> st_bit) & mask, time);
  266. #endif
  267. l &= ~(mask << st_bit);
  268. l |= ticks << st_bit;
  269. gpmc_cs_write_reg(cs, reg, l);
  270. return 0;
  271. }
  272. #ifdef DEBUG
  273. #define GPMC_SET_ONE(reg, st, end, field) \
  274. if (set_gpmc_timing_reg(cs, (reg), (st), (end), \
  275. t->field, #field) < 0) \
  276. return -1
  277. #else
  278. #define GPMC_SET_ONE(reg, st, end, field) \
  279. if (set_gpmc_timing_reg(cs, (reg), (st), (end), t->field) < 0) \
  280. return -1
  281. #endif
  282. int gpmc_calc_divider(unsigned int sync_clk)
  283. {
  284. int div;
  285. u32 l;
  286. l = sync_clk + (gpmc_get_fclk_period() - 1);
  287. div = l / gpmc_get_fclk_period();
  288. if (div > 4)
  289. return -1;
  290. if (div <= 0)
  291. div = 1;
  292. return div;
  293. }
  294. int gpmc_cs_set_timings(int cs, const struct gpmc_timings *t)
  295. {
  296. int div;
  297. u32 l;
  298. div = gpmc_calc_divider(t->sync_clk);
  299. if (div < 0)
  300. return div;
  301. GPMC_SET_ONE(GPMC_CS_CONFIG2, 0, 3, cs_on);
  302. GPMC_SET_ONE(GPMC_CS_CONFIG2, 8, 12, cs_rd_off);
  303. GPMC_SET_ONE(GPMC_CS_CONFIG2, 16, 20, cs_wr_off);
  304. GPMC_SET_ONE(GPMC_CS_CONFIG3, 0, 3, adv_on);
  305. GPMC_SET_ONE(GPMC_CS_CONFIG3, 8, 12, adv_rd_off);
  306. GPMC_SET_ONE(GPMC_CS_CONFIG3, 16, 20, adv_wr_off);
  307. GPMC_SET_ONE(GPMC_CS_CONFIG4, 0, 3, oe_on);
  308. GPMC_SET_ONE(GPMC_CS_CONFIG4, 8, 12, oe_off);
  309. GPMC_SET_ONE(GPMC_CS_CONFIG4, 16, 19, we_on);
  310. GPMC_SET_ONE(GPMC_CS_CONFIG4, 24, 28, we_off);
  311. GPMC_SET_ONE(GPMC_CS_CONFIG5, 0, 4, rd_cycle);
  312. GPMC_SET_ONE(GPMC_CS_CONFIG5, 8, 12, wr_cycle);
  313. GPMC_SET_ONE(GPMC_CS_CONFIG5, 16, 20, access);
  314. GPMC_SET_ONE(GPMC_CS_CONFIG5, 24, 27, page_burst_access);
  315. GPMC_SET_ONE(GPMC_CS_CONFIG6, 0, 3, bus_turnaround);
  316. GPMC_SET_ONE(GPMC_CS_CONFIG6, 8, 11, cycle2cycle_delay);
  317. GPMC_SET_ONE(GPMC_CS_CONFIG1, 18, 19, wait_monitoring);
  318. GPMC_SET_ONE(GPMC_CS_CONFIG1, 25, 26, clk_activation);
  319. if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
  320. GPMC_SET_ONE(GPMC_CS_CONFIG6, 16, 19, wr_data_mux_bus);
  321. if (gpmc_capability & GPMC_HAS_WR_ACCESS)
  322. GPMC_SET_ONE(GPMC_CS_CONFIG6, 24, 28, wr_access);
  323. /* caller is expected to have initialized CONFIG1 to cover
  324. * at least sync vs async
  325. */
  326. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
  327. if (l & (GPMC_CONFIG1_READTYPE_SYNC | GPMC_CONFIG1_WRITETYPE_SYNC)) {
  328. #ifdef DEBUG
  329. printk(KERN_INFO "GPMC CS%d CLK period is %lu ns (div %d)\n",
  330. cs, (div * gpmc_get_fclk_period()) / 1000, div);
  331. #endif
  332. l &= ~0x03;
  333. l |= (div - 1);
  334. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, l);
  335. }
  336. gpmc_cs_bool_timings(cs, &t->bool_timings);
  337. return 0;
  338. }
  339. static void gpmc_cs_enable_mem(int cs, u32 base, u32 size)
  340. {
  341. u32 l;
  342. u32 mask;
  343. mask = (1 << GPMC_SECTION_SHIFT) - size;
  344. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  345. l &= ~0x3f;
  346. l = (base >> GPMC_CHUNK_SHIFT) & 0x3f;
  347. l &= ~(0x0f << 8);
  348. l |= ((mask >> GPMC_CHUNK_SHIFT) & 0x0f) << 8;
  349. l |= GPMC_CONFIG7_CSVALID;
  350. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
  351. }
  352. static void gpmc_cs_disable_mem(int cs)
  353. {
  354. u32 l;
  355. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  356. l &= ~GPMC_CONFIG7_CSVALID;
  357. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
  358. }
  359. static void gpmc_cs_get_memconf(int cs, u32 *base, u32 *size)
  360. {
  361. u32 l;
  362. u32 mask;
  363. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  364. *base = (l & 0x3f) << GPMC_CHUNK_SHIFT;
  365. mask = (l >> 8) & 0x0f;
  366. *size = (1 << GPMC_SECTION_SHIFT) - (mask << GPMC_CHUNK_SHIFT);
  367. }
  368. static int gpmc_cs_mem_enabled(int cs)
  369. {
  370. u32 l;
  371. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  372. return l & GPMC_CONFIG7_CSVALID;
  373. }
  374. int gpmc_cs_set_reserved(int cs, int reserved)
  375. {
  376. if (cs > GPMC_CS_NUM)
  377. return -ENODEV;
  378. gpmc_cs_map &= ~(1 << cs);
  379. gpmc_cs_map |= (reserved ? 1 : 0) << cs;
  380. return 0;
  381. }
  382. int gpmc_cs_reserved(int cs)
  383. {
  384. if (cs > GPMC_CS_NUM)
  385. return -ENODEV;
  386. return gpmc_cs_map & (1 << cs);
  387. }
  388. static unsigned long gpmc_mem_align(unsigned long size)
  389. {
  390. int order;
  391. size = (size - 1) >> (GPMC_CHUNK_SHIFT - 1);
  392. order = GPMC_CHUNK_SHIFT - 1;
  393. do {
  394. size >>= 1;
  395. order++;
  396. } while (size);
  397. size = 1 << order;
  398. return size;
  399. }
  400. static int gpmc_cs_insert_mem(int cs, unsigned long base, unsigned long size)
  401. {
  402. struct resource *res = &gpmc_cs_mem[cs];
  403. int r;
  404. size = gpmc_mem_align(size);
  405. spin_lock(&gpmc_mem_lock);
  406. res->start = base;
  407. res->end = base + size - 1;
  408. r = request_resource(&gpmc_mem_root, res);
  409. spin_unlock(&gpmc_mem_lock);
  410. return r;
  411. }
  412. static int gpmc_cs_delete_mem(int cs)
  413. {
  414. struct resource *res = &gpmc_cs_mem[cs];
  415. int r;
  416. spin_lock(&gpmc_mem_lock);
  417. r = release_resource(&gpmc_cs_mem[cs]);
  418. res->start = 0;
  419. res->end = 0;
  420. spin_unlock(&gpmc_mem_lock);
  421. return r;
  422. }
  423. int gpmc_cs_request(int cs, unsigned long size, unsigned long *base)
  424. {
  425. struct resource *res = &gpmc_cs_mem[cs];
  426. int r = -1;
  427. if (cs > GPMC_CS_NUM)
  428. return -ENODEV;
  429. size = gpmc_mem_align(size);
  430. if (size > (1 << GPMC_SECTION_SHIFT))
  431. return -ENOMEM;
  432. spin_lock(&gpmc_mem_lock);
  433. if (gpmc_cs_reserved(cs)) {
  434. r = -EBUSY;
  435. goto out;
  436. }
  437. if (gpmc_cs_mem_enabled(cs))
  438. r = adjust_resource(res, res->start & ~(size - 1), size);
  439. if (r < 0)
  440. r = allocate_resource(&gpmc_mem_root, res, size, 0, ~0,
  441. size, NULL, NULL);
  442. if (r < 0)
  443. goto out;
  444. gpmc_cs_enable_mem(cs, res->start, resource_size(res));
  445. *base = res->start;
  446. gpmc_cs_set_reserved(cs, 1);
  447. out:
  448. spin_unlock(&gpmc_mem_lock);
  449. return r;
  450. }
  451. EXPORT_SYMBOL(gpmc_cs_request);
  452. void gpmc_cs_free(int cs)
  453. {
  454. spin_lock(&gpmc_mem_lock);
  455. if (cs >= GPMC_CS_NUM || cs < 0 || !gpmc_cs_reserved(cs)) {
  456. printk(KERN_ERR "Trying to free non-reserved GPMC CS%d\n", cs);
  457. BUG();
  458. spin_unlock(&gpmc_mem_lock);
  459. return;
  460. }
  461. gpmc_cs_disable_mem(cs);
  462. release_resource(&gpmc_cs_mem[cs]);
  463. gpmc_cs_set_reserved(cs, 0);
  464. spin_unlock(&gpmc_mem_lock);
  465. }
  466. EXPORT_SYMBOL(gpmc_cs_free);
  467. /**
  468. * gpmc_cs_configure - write request to configure gpmc
  469. * @cs: chip select number
  470. * @cmd: command type
  471. * @wval: value to write
  472. * @return status of the operation
  473. */
  474. int gpmc_cs_configure(int cs, int cmd, int wval)
  475. {
  476. int err = 0;
  477. u32 regval = 0;
  478. switch (cmd) {
  479. case GPMC_ENABLE_IRQ:
  480. gpmc_write_reg(GPMC_IRQENABLE, wval);
  481. break;
  482. case GPMC_SET_IRQ_STATUS:
  483. gpmc_write_reg(GPMC_IRQSTATUS, wval);
  484. break;
  485. case GPMC_CONFIG_WP:
  486. regval = gpmc_read_reg(GPMC_CONFIG);
  487. if (wval)
  488. regval &= ~GPMC_CONFIG_WRITEPROTECT; /* WP is ON */
  489. else
  490. regval |= GPMC_CONFIG_WRITEPROTECT; /* WP is OFF */
  491. gpmc_write_reg(GPMC_CONFIG, regval);
  492. break;
  493. case GPMC_CONFIG_RDY_BSY:
  494. regval = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
  495. if (wval)
  496. regval |= WR_RD_PIN_MONITORING;
  497. else
  498. regval &= ~WR_RD_PIN_MONITORING;
  499. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval);
  500. break;
  501. case GPMC_CONFIG_DEV_SIZE:
  502. regval = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
  503. /* clear 2 target bits */
  504. regval &= ~GPMC_CONFIG1_DEVICESIZE(3);
  505. /* set the proper value */
  506. regval |= GPMC_CONFIG1_DEVICESIZE(wval);
  507. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval);
  508. break;
  509. case GPMC_CONFIG_DEV_TYPE:
  510. regval = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
  511. regval |= GPMC_CONFIG1_DEVICETYPE(wval);
  512. if (wval == GPMC_DEVICETYPE_NOR)
  513. regval |= GPMC_CONFIG1_MUXADDDATA;
  514. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval);
  515. break;
  516. default:
  517. printk(KERN_ERR "gpmc_configure_cs: Not supported\n");
  518. err = -EINVAL;
  519. }
  520. return err;
  521. }
  522. EXPORT_SYMBOL(gpmc_cs_configure);
  523. void gpmc_update_nand_reg(struct gpmc_nand_regs *reg, int cs)
  524. {
  525. int i;
  526. reg->gpmc_status = gpmc_base + GPMC_STATUS;
  527. reg->gpmc_nand_command = gpmc_base + GPMC_CS0_OFFSET +
  528. GPMC_CS_NAND_COMMAND + GPMC_CS_SIZE * cs;
  529. reg->gpmc_nand_address = gpmc_base + GPMC_CS0_OFFSET +
  530. GPMC_CS_NAND_ADDRESS + GPMC_CS_SIZE * cs;
  531. reg->gpmc_nand_data = gpmc_base + GPMC_CS0_OFFSET +
  532. GPMC_CS_NAND_DATA + GPMC_CS_SIZE * cs;
  533. reg->gpmc_prefetch_config1 = gpmc_base + GPMC_PREFETCH_CONFIG1;
  534. reg->gpmc_prefetch_config2 = gpmc_base + GPMC_PREFETCH_CONFIG2;
  535. reg->gpmc_prefetch_control = gpmc_base + GPMC_PREFETCH_CONTROL;
  536. reg->gpmc_prefetch_status = gpmc_base + GPMC_PREFETCH_STATUS;
  537. reg->gpmc_ecc_config = gpmc_base + GPMC_ECC_CONFIG;
  538. reg->gpmc_ecc_control = gpmc_base + GPMC_ECC_CONTROL;
  539. reg->gpmc_ecc_size_config = gpmc_base + GPMC_ECC_SIZE_CONFIG;
  540. reg->gpmc_ecc1_result = gpmc_base + GPMC_ECC1_RESULT;
  541. for (i = 0; i < GPMC_BCH_NUM_REMAINDER; i++) {
  542. reg->gpmc_bch_result0[i] = gpmc_base + GPMC_ECC_BCH_RESULT_0 +
  543. GPMC_BCH_SIZE * i;
  544. reg->gpmc_bch_result1[i] = gpmc_base + GPMC_ECC_BCH_RESULT_1 +
  545. GPMC_BCH_SIZE * i;
  546. reg->gpmc_bch_result2[i] = gpmc_base + GPMC_ECC_BCH_RESULT_2 +
  547. GPMC_BCH_SIZE * i;
  548. reg->gpmc_bch_result3[i] = gpmc_base + GPMC_ECC_BCH_RESULT_3 +
  549. GPMC_BCH_SIZE * i;
  550. }
  551. }
  552. int gpmc_get_client_irq(unsigned irq_config)
  553. {
  554. int i;
  555. if (hweight32(irq_config) > 1)
  556. return 0;
  557. for (i = 0; i < GPMC_NR_IRQ; i++)
  558. if (gpmc_client_irq[i].bitmask & irq_config)
  559. return gpmc_client_irq[i].irq;
  560. return 0;
  561. }
  562. static int gpmc_irq_endis(unsigned irq, bool endis)
  563. {
  564. int i;
  565. u32 regval;
  566. for (i = 0; i < GPMC_NR_IRQ; i++)
  567. if (irq == gpmc_client_irq[i].irq) {
  568. regval = gpmc_read_reg(GPMC_IRQENABLE);
  569. if (endis)
  570. regval |= gpmc_client_irq[i].bitmask;
  571. else
  572. regval &= ~gpmc_client_irq[i].bitmask;
  573. gpmc_write_reg(GPMC_IRQENABLE, regval);
  574. break;
  575. }
  576. return 0;
  577. }
  578. static void gpmc_irq_disable(struct irq_data *p)
  579. {
  580. gpmc_irq_endis(p->irq, false);
  581. }
  582. static void gpmc_irq_enable(struct irq_data *p)
  583. {
  584. gpmc_irq_endis(p->irq, true);
  585. }
  586. static void gpmc_irq_noop(struct irq_data *data) { }
  587. static unsigned int gpmc_irq_noop_ret(struct irq_data *data) { return 0; }
  588. static int gpmc_setup_irq(void)
  589. {
  590. int i;
  591. u32 regval;
  592. if (!gpmc_irq)
  593. return -EINVAL;
  594. gpmc_irq_start = irq_alloc_descs(-1, 0, GPMC_NR_IRQ, 0);
  595. if (IS_ERR_VALUE(gpmc_irq_start)) {
  596. pr_err("irq_alloc_descs failed\n");
  597. return gpmc_irq_start;
  598. }
  599. gpmc_irq_chip.name = "gpmc";
  600. gpmc_irq_chip.irq_startup = gpmc_irq_noop_ret;
  601. gpmc_irq_chip.irq_enable = gpmc_irq_enable;
  602. gpmc_irq_chip.irq_disable = gpmc_irq_disable;
  603. gpmc_irq_chip.irq_shutdown = gpmc_irq_noop;
  604. gpmc_irq_chip.irq_ack = gpmc_irq_noop;
  605. gpmc_irq_chip.irq_mask = gpmc_irq_noop;
  606. gpmc_irq_chip.irq_unmask = gpmc_irq_noop;
  607. gpmc_client_irq[0].bitmask = GPMC_IRQ_FIFOEVENTENABLE;
  608. gpmc_client_irq[1].bitmask = GPMC_IRQ_COUNT_EVENT;
  609. for (i = 0; i < GPMC_NR_IRQ; i++) {
  610. gpmc_client_irq[i].irq = gpmc_irq_start + i;
  611. irq_set_chip_and_handler(gpmc_client_irq[i].irq,
  612. &gpmc_irq_chip, handle_simple_irq);
  613. set_irq_flags(gpmc_client_irq[i].irq,
  614. IRQF_VALID | IRQF_NOAUTOEN);
  615. }
  616. /* Disable interrupts */
  617. gpmc_write_reg(GPMC_IRQENABLE, 0);
  618. /* clear interrupts */
  619. regval = gpmc_read_reg(GPMC_IRQSTATUS);
  620. gpmc_write_reg(GPMC_IRQSTATUS, regval);
  621. return request_irq(gpmc_irq, gpmc_handle_irq, 0, "gpmc", NULL);
  622. }
  623. static int gpmc_free_irq(void)
  624. {
  625. int i;
  626. if (gpmc_irq)
  627. free_irq(gpmc_irq, NULL);
  628. for (i = 0; i < GPMC_NR_IRQ; i++) {
  629. irq_set_handler(gpmc_client_irq[i].irq, NULL);
  630. irq_set_chip(gpmc_client_irq[i].irq, &no_irq_chip);
  631. irq_modify_status(gpmc_client_irq[i].irq, 0, 0);
  632. }
  633. irq_free_descs(gpmc_irq_start, GPMC_NR_IRQ);
  634. return 0;
  635. }
  636. static void gpmc_mem_exit(void)
  637. {
  638. int cs;
  639. for (cs = 0; cs < GPMC_CS_NUM; cs++) {
  640. if (!gpmc_cs_mem_enabled(cs))
  641. continue;
  642. gpmc_cs_delete_mem(cs);
  643. }
  644. }
  645. static int gpmc_mem_init(void)
  646. {
  647. int cs, rc;
  648. unsigned long boot_rom_space = 0;
  649. /* never allocate the first page, to facilitate bug detection;
  650. * even if we didn't boot from ROM.
  651. */
  652. boot_rom_space = BOOT_ROM_SPACE;
  653. gpmc_mem_root.start = GPMC_MEM_START + boot_rom_space;
  654. gpmc_mem_root.end = GPMC_MEM_END;
  655. /* Reserve all regions that has been set up by bootloader */
  656. for (cs = 0; cs < GPMC_CS_NUM; cs++) {
  657. u32 base, size;
  658. if (!gpmc_cs_mem_enabled(cs))
  659. continue;
  660. gpmc_cs_get_memconf(cs, &base, &size);
  661. rc = gpmc_cs_insert_mem(cs, base, size);
  662. if (IS_ERR_VALUE(rc)) {
  663. while (--cs >= 0)
  664. if (gpmc_cs_mem_enabled(cs))
  665. gpmc_cs_delete_mem(cs);
  666. return rc;
  667. }
  668. }
  669. return 0;
  670. }
  671. static u32 gpmc_round_ps_to_sync_clk(u32 time_ps, u32 sync_clk)
  672. {
  673. u32 temp;
  674. int div;
  675. div = gpmc_calc_divider(sync_clk);
  676. temp = gpmc_ps_to_ticks(time_ps);
  677. temp = (temp + div - 1) / div;
  678. return gpmc_ticks_to_ps(temp * div);
  679. }
  680. /* XXX: can the cycles be avoided ? */
  681. static int gpmc_calc_sync_read_timings(struct gpmc_timings *gpmc_t,
  682. struct gpmc_device_timings *dev_t)
  683. {
  684. bool mux = dev_t->mux;
  685. u32 temp;
  686. /* adv_rd_off */
  687. temp = dev_t->t_avdp_r;
  688. /* XXX: mux check required ? */
  689. if (mux) {
  690. /* XXX: t_avdp not to be required for sync, only added for tusb
  691. * this indirectly necessitates requirement of t_avdp_r and
  692. * t_avdp_w instead of having a single t_avdp
  693. */
  694. temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_avdh);
  695. temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
  696. }
  697. gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp);
  698. /* oe_on */
  699. temp = dev_t->t_oeasu; /* XXX: remove this ? */
  700. if (mux) {
  701. temp = max_t(u32, temp, gpmc_t->clk_activation + dev_t->t_ach);
  702. temp = max_t(u32, temp, gpmc_t->adv_rd_off +
  703. gpmc_ticks_to_ps(dev_t->cyc_aavdh_oe));
  704. }
  705. gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp);
  706. /* access */
  707. /* XXX: any scope for improvement ?, by combining oe_on
  708. * and clk_activation, need to check whether
  709. * access = clk_activation + round to sync clk ?
  710. */
  711. temp = max_t(u32, dev_t->t_iaa, dev_t->cyc_iaa * gpmc_t->sync_clk);
  712. temp += gpmc_t->clk_activation;
  713. if (dev_t->cyc_oe)
  714. temp = max_t(u32, temp, gpmc_t->oe_on +
  715. gpmc_ticks_to_ps(dev_t->cyc_oe));
  716. gpmc_t->access = gpmc_round_ps_to_ticks(temp);
  717. gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1);
  718. gpmc_t->cs_rd_off = gpmc_t->oe_off;
  719. /* rd_cycle */
  720. temp = max_t(u32, dev_t->t_cez_r, dev_t->t_oez);
  721. temp = gpmc_round_ps_to_sync_clk(temp, gpmc_t->sync_clk) +
  722. gpmc_t->access;
  723. /* XXX: barter t_ce_rdyz with t_cez_r ? */
  724. if (dev_t->t_ce_rdyz)
  725. temp = max_t(u32, temp, gpmc_t->cs_rd_off + dev_t->t_ce_rdyz);
  726. gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp);
  727. return 0;
  728. }
  729. static int gpmc_calc_sync_write_timings(struct gpmc_timings *gpmc_t,
  730. struct gpmc_device_timings *dev_t)
  731. {
  732. bool mux = dev_t->mux;
  733. u32 temp;
  734. /* adv_wr_off */
  735. temp = dev_t->t_avdp_w;
  736. if (mux) {
  737. temp = max_t(u32, temp,
  738. gpmc_t->clk_activation + dev_t->t_avdh);
  739. temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
  740. }
  741. gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp);
  742. /* wr_data_mux_bus */
  743. temp = max_t(u32, dev_t->t_weasu,
  744. gpmc_t->clk_activation + dev_t->t_rdyo);
  745. /* XXX: shouldn't mux be kept as a whole for wr_data_mux_bus ?,
  746. * and in that case remember to handle we_on properly
  747. */
  748. if (mux) {
  749. temp = max_t(u32, temp,
  750. gpmc_t->adv_wr_off + dev_t->t_aavdh);
  751. temp = max_t(u32, temp, gpmc_t->adv_wr_off +
  752. gpmc_ticks_to_ps(dev_t->cyc_aavdh_we));
  753. }
  754. gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp);
  755. /* we_on */
  756. if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
  757. gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu);
  758. else
  759. gpmc_t->we_on = gpmc_t->wr_data_mux_bus;
  760. /* wr_access */
  761. /* XXX: gpmc_capability check reqd ? , even if not, will not harm */
  762. gpmc_t->wr_access = gpmc_t->access;
  763. /* we_off */
  764. temp = gpmc_t->we_on + dev_t->t_wpl;
  765. temp = max_t(u32, temp,
  766. gpmc_t->wr_access + gpmc_ticks_to_ps(1));
  767. temp = max_t(u32, temp,
  768. gpmc_t->we_on + gpmc_ticks_to_ps(dev_t->cyc_wpl));
  769. gpmc_t->we_off = gpmc_round_ps_to_ticks(temp);
  770. gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off +
  771. dev_t->t_wph);
  772. /* wr_cycle */
  773. temp = gpmc_round_ps_to_sync_clk(dev_t->t_cez_w, gpmc_t->sync_clk);
  774. temp += gpmc_t->wr_access;
  775. /* XXX: barter t_ce_rdyz with t_cez_w ? */
  776. if (dev_t->t_ce_rdyz)
  777. temp = max_t(u32, temp,
  778. gpmc_t->cs_wr_off + dev_t->t_ce_rdyz);
  779. gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp);
  780. return 0;
  781. }
  782. static int gpmc_calc_async_read_timings(struct gpmc_timings *gpmc_t,
  783. struct gpmc_device_timings *dev_t)
  784. {
  785. bool mux = dev_t->mux;
  786. u32 temp;
  787. /* adv_rd_off */
  788. temp = dev_t->t_avdp_r;
  789. if (mux)
  790. temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
  791. gpmc_t->adv_rd_off = gpmc_round_ps_to_ticks(temp);
  792. /* oe_on */
  793. temp = dev_t->t_oeasu;
  794. if (mux)
  795. temp = max_t(u32, temp,
  796. gpmc_t->adv_rd_off + dev_t->t_aavdh);
  797. gpmc_t->oe_on = gpmc_round_ps_to_ticks(temp);
  798. /* access */
  799. temp = max_t(u32, dev_t->t_iaa, /* XXX: remove t_iaa in async ? */
  800. gpmc_t->oe_on + dev_t->t_oe);
  801. temp = max_t(u32, temp,
  802. gpmc_t->cs_on + dev_t->t_ce);
  803. temp = max_t(u32, temp,
  804. gpmc_t->adv_on + dev_t->t_aa);
  805. gpmc_t->access = gpmc_round_ps_to_ticks(temp);
  806. gpmc_t->oe_off = gpmc_t->access + gpmc_ticks_to_ps(1);
  807. gpmc_t->cs_rd_off = gpmc_t->oe_off;
  808. /* rd_cycle */
  809. temp = max_t(u32, dev_t->t_rd_cycle,
  810. gpmc_t->cs_rd_off + dev_t->t_cez_r);
  811. temp = max_t(u32, temp, gpmc_t->oe_off + dev_t->t_oez);
  812. gpmc_t->rd_cycle = gpmc_round_ps_to_ticks(temp);
  813. return 0;
  814. }
  815. static int gpmc_calc_async_write_timings(struct gpmc_timings *gpmc_t,
  816. struct gpmc_device_timings *dev_t)
  817. {
  818. bool mux = dev_t->mux;
  819. u32 temp;
  820. /* adv_wr_off */
  821. temp = dev_t->t_avdp_w;
  822. if (mux)
  823. temp = max_t(u32, gpmc_t->adv_on + gpmc_ticks_to_ps(1), temp);
  824. gpmc_t->adv_wr_off = gpmc_round_ps_to_ticks(temp);
  825. /* wr_data_mux_bus */
  826. temp = dev_t->t_weasu;
  827. if (mux) {
  828. temp = max_t(u32, temp, gpmc_t->adv_wr_off + dev_t->t_aavdh);
  829. temp = max_t(u32, temp, gpmc_t->adv_wr_off +
  830. gpmc_ticks_to_ps(dev_t->cyc_aavdh_we));
  831. }
  832. gpmc_t->wr_data_mux_bus = gpmc_round_ps_to_ticks(temp);
  833. /* we_on */
  834. if (gpmc_capability & GPMC_HAS_WR_DATA_MUX_BUS)
  835. gpmc_t->we_on = gpmc_round_ps_to_ticks(dev_t->t_weasu);
  836. else
  837. gpmc_t->we_on = gpmc_t->wr_data_mux_bus;
  838. /* we_off */
  839. temp = gpmc_t->we_on + dev_t->t_wpl;
  840. gpmc_t->we_off = gpmc_round_ps_to_ticks(temp);
  841. gpmc_t->cs_wr_off = gpmc_round_ps_to_ticks(gpmc_t->we_off +
  842. dev_t->t_wph);
  843. /* wr_cycle */
  844. temp = max_t(u32, dev_t->t_wr_cycle,
  845. gpmc_t->cs_wr_off + dev_t->t_cez_w);
  846. gpmc_t->wr_cycle = gpmc_round_ps_to_ticks(temp);
  847. return 0;
  848. }
  849. static int gpmc_calc_sync_common_timings(struct gpmc_timings *gpmc_t,
  850. struct gpmc_device_timings *dev_t)
  851. {
  852. u32 temp;
  853. gpmc_t->sync_clk = gpmc_calc_divider(dev_t->clk) *
  854. gpmc_get_fclk_period();
  855. gpmc_t->page_burst_access = gpmc_round_ps_to_sync_clk(
  856. dev_t->t_bacc,
  857. gpmc_t->sync_clk);
  858. temp = max_t(u32, dev_t->t_ces, dev_t->t_avds);
  859. gpmc_t->clk_activation = gpmc_round_ps_to_ticks(temp);
  860. if (gpmc_calc_divider(gpmc_t->sync_clk) != 1)
  861. return 0;
  862. if (dev_t->ce_xdelay)
  863. gpmc_t->bool_timings.cs_extra_delay = true;
  864. if (dev_t->avd_xdelay)
  865. gpmc_t->bool_timings.adv_extra_delay = true;
  866. if (dev_t->oe_xdelay)
  867. gpmc_t->bool_timings.oe_extra_delay = true;
  868. if (dev_t->we_xdelay)
  869. gpmc_t->bool_timings.we_extra_delay = true;
  870. return 0;
  871. }
  872. static int gpmc_calc_common_timings(struct gpmc_timings *gpmc_t,
  873. struct gpmc_device_timings *dev_t)
  874. {
  875. u32 temp;
  876. /* cs_on */
  877. gpmc_t->cs_on = gpmc_round_ps_to_ticks(dev_t->t_ceasu);
  878. /* adv_on */
  879. temp = dev_t->t_avdasu;
  880. if (dev_t->t_ce_avd)
  881. temp = max_t(u32, temp,
  882. gpmc_t->cs_on + dev_t->t_ce_avd);
  883. gpmc_t->adv_on = gpmc_round_ps_to_ticks(temp);
  884. if (dev_t->sync_write || dev_t->sync_read)
  885. gpmc_calc_sync_common_timings(gpmc_t, dev_t);
  886. return 0;
  887. }
  888. /* TODO: remove this function once all peripherals are confirmed to
  889. * work with generic timing. Simultaneously gpmc_cs_set_timings()
  890. * has to be modified to handle timings in ps instead of ns
  891. */
  892. static void gpmc_convert_ps_to_ns(struct gpmc_timings *t)
  893. {
  894. t->cs_on /= 1000;
  895. t->cs_rd_off /= 1000;
  896. t->cs_wr_off /= 1000;
  897. t->adv_on /= 1000;
  898. t->adv_rd_off /= 1000;
  899. t->adv_wr_off /= 1000;
  900. t->we_on /= 1000;
  901. t->we_off /= 1000;
  902. t->oe_on /= 1000;
  903. t->oe_off /= 1000;
  904. t->page_burst_access /= 1000;
  905. t->access /= 1000;
  906. t->rd_cycle /= 1000;
  907. t->wr_cycle /= 1000;
  908. t->bus_turnaround /= 1000;
  909. t->cycle2cycle_delay /= 1000;
  910. t->wait_monitoring /= 1000;
  911. t->clk_activation /= 1000;
  912. t->wr_access /= 1000;
  913. t->wr_data_mux_bus /= 1000;
  914. }
  915. int gpmc_calc_timings(struct gpmc_timings *gpmc_t,
  916. struct gpmc_device_timings *dev_t)
  917. {
  918. memset(gpmc_t, 0, sizeof(*gpmc_t));
  919. gpmc_calc_common_timings(gpmc_t, dev_t);
  920. if (dev_t->sync_read)
  921. gpmc_calc_sync_read_timings(gpmc_t, dev_t);
  922. else
  923. gpmc_calc_async_read_timings(gpmc_t, dev_t);
  924. if (dev_t->sync_write)
  925. gpmc_calc_sync_write_timings(gpmc_t, dev_t);
  926. else
  927. gpmc_calc_async_write_timings(gpmc_t, dev_t);
  928. /* TODO: remove, see function definition */
  929. gpmc_convert_ps_to_ns(gpmc_t);
  930. return 0;
  931. }
  932. #ifdef CONFIG_OF
  933. static struct of_device_id gpmc_dt_ids[] = {
  934. { .compatible = "ti,omap2420-gpmc" },
  935. { .compatible = "ti,omap2430-gpmc" },
  936. { .compatible = "ti,omap3430-gpmc" }, /* omap3430 & omap3630 */
  937. { .compatible = "ti,omap4430-gpmc" }, /* omap4430 & omap4460 & omap543x */
  938. { .compatible = "ti,am3352-gpmc" }, /* am335x devices */
  939. { }
  940. };
  941. MODULE_DEVICE_TABLE(of, gpmc_dt_ids);
  942. static void __maybe_unused gpmc_read_timings_dt(struct device_node *np,
  943. struct gpmc_timings *gpmc_t)
  944. {
  945. u32 val;
  946. memset(gpmc_t, 0, sizeof(*gpmc_t));
  947. /* minimum clock period for syncronous mode */
  948. if (!of_property_read_u32(np, "gpmc,sync-clk", &val))
  949. gpmc_t->sync_clk = val;
  950. /* chip select timtings */
  951. if (!of_property_read_u32(np, "gpmc,cs-on", &val))
  952. gpmc_t->cs_on = val;
  953. if (!of_property_read_u32(np, "gpmc,cs-rd-off", &val))
  954. gpmc_t->cs_rd_off = val;
  955. if (!of_property_read_u32(np, "gpmc,cs-wr-off", &val))
  956. gpmc_t->cs_wr_off = val;
  957. /* ADV signal timings */
  958. if (!of_property_read_u32(np, "gpmc,adv-on", &val))
  959. gpmc_t->adv_on = val;
  960. if (!of_property_read_u32(np, "gpmc,adv-rd-off", &val))
  961. gpmc_t->adv_rd_off = val;
  962. if (!of_property_read_u32(np, "gpmc,adv-wr-off", &val))
  963. gpmc_t->adv_wr_off = val;
  964. /* WE signal timings */
  965. if (!of_property_read_u32(np, "gpmc,we-on", &val))
  966. gpmc_t->we_on = val;
  967. if (!of_property_read_u32(np, "gpmc,we-off", &val))
  968. gpmc_t->we_off = val;
  969. /* OE signal timings */
  970. if (!of_property_read_u32(np, "gpmc,oe-on", &val))
  971. gpmc_t->oe_on = val;
  972. if (!of_property_read_u32(np, "gpmc,oe-off", &val))
  973. gpmc_t->oe_off = val;
  974. /* access and cycle timings */
  975. if (!of_property_read_u32(np, "gpmc,page-burst-access", &val))
  976. gpmc_t->page_burst_access = val;
  977. if (!of_property_read_u32(np, "gpmc,access", &val))
  978. gpmc_t->access = val;
  979. if (!of_property_read_u32(np, "gpmc,rd-cycle", &val))
  980. gpmc_t->rd_cycle = val;
  981. if (!of_property_read_u32(np, "gpmc,wr-cycle", &val))
  982. gpmc_t->wr_cycle = val;
  983. /* only for OMAP3430 */
  984. if (!of_property_read_u32(np, "gpmc,wr-access", &val))
  985. gpmc_t->wr_access = val;
  986. if (!of_property_read_u32(np, "gpmc,wr-data-mux-bus", &val))
  987. gpmc_t->wr_data_mux_bus = val;
  988. }
  989. #ifdef CONFIG_MTD_NAND
  990. static const char * const nand_ecc_opts[] = {
  991. [OMAP_ECC_HAMMING_CODE_DEFAULT] = "sw",
  992. [OMAP_ECC_HAMMING_CODE_HW] = "hw",
  993. [OMAP_ECC_HAMMING_CODE_HW_ROMCODE] = "hw-romcode",
  994. [OMAP_ECC_BCH4_CODE_HW] = "bch4",
  995. [OMAP_ECC_BCH8_CODE_HW] = "bch8",
  996. };
  997. static int gpmc_probe_nand_child(struct platform_device *pdev,
  998. struct device_node *child)
  999. {
  1000. u32 val;
  1001. const char *s;
  1002. struct gpmc_timings gpmc_t;
  1003. struct omap_nand_platform_data *gpmc_nand_data;
  1004. if (of_property_read_u32(child, "reg", &val) < 0) {
  1005. dev_err(&pdev->dev, "%s has no 'reg' property\n",
  1006. child->full_name);
  1007. return -ENODEV;
  1008. }
  1009. gpmc_nand_data = devm_kzalloc(&pdev->dev, sizeof(*gpmc_nand_data),
  1010. GFP_KERNEL);
  1011. if (!gpmc_nand_data)
  1012. return -ENOMEM;
  1013. gpmc_nand_data->cs = val;
  1014. gpmc_nand_data->of_node = child;
  1015. if (!of_property_read_string(child, "ti,nand-ecc-opt", &s))
  1016. for (val = 0; val < ARRAY_SIZE(nand_ecc_opts); val++)
  1017. if (!strcasecmp(s, nand_ecc_opts[val])) {
  1018. gpmc_nand_data->ecc_opt = val;
  1019. break;
  1020. }
  1021. val = of_get_nand_bus_width(child);
  1022. if (val == 16)
  1023. gpmc_nand_data->devsize = NAND_BUSWIDTH_16;
  1024. gpmc_read_timings_dt(child, &gpmc_t);
  1025. gpmc_nand_init(gpmc_nand_data, &gpmc_t);
  1026. return 0;
  1027. }
  1028. #else
  1029. static int gpmc_probe_nand_child(struct platform_device *pdev,
  1030. struct device_node *child)
  1031. {
  1032. return 0;
  1033. }
  1034. #endif
  1035. #ifdef CONFIG_MTD_ONENAND
  1036. static int gpmc_probe_onenand_child(struct platform_device *pdev,
  1037. struct device_node *child)
  1038. {
  1039. u32 val;
  1040. struct omap_onenand_platform_data *gpmc_onenand_data;
  1041. if (of_property_read_u32(child, "reg", &val) < 0) {
  1042. dev_err(&pdev->dev, "%s has no 'reg' property\n",
  1043. child->full_name);
  1044. return -ENODEV;
  1045. }
  1046. gpmc_onenand_data = devm_kzalloc(&pdev->dev, sizeof(*gpmc_onenand_data),
  1047. GFP_KERNEL);
  1048. if (!gpmc_onenand_data)
  1049. return -ENOMEM;
  1050. gpmc_onenand_data->cs = val;
  1051. gpmc_onenand_data->of_node = child;
  1052. gpmc_onenand_data->dma_channel = -1;
  1053. if (!of_property_read_u32(child, "dma-channel", &val))
  1054. gpmc_onenand_data->dma_channel = val;
  1055. gpmc_onenand_init(gpmc_onenand_data);
  1056. return 0;
  1057. }
  1058. #else
  1059. static int gpmc_probe_onenand_child(struct platform_device *pdev,
  1060. struct device_node *child)
  1061. {
  1062. return 0;
  1063. }
  1064. #endif
  1065. static int gpmc_probe_dt(struct platform_device *pdev)
  1066. {
  1067. int ret;
  1068. struct device_node *child;
  1069. const struct of_device_id *of_id =
  1070. of_match_device(gpmc_dt_ids, &pdev->dev);
  1071. if (!of_id)
  1072. return 0;
  1073. for_each_node_by_name(child, "nand") {
  1074. ret = gpmc_probe_nand_child(pdev, child);
  1075. if (ret < 0) {
  1076. of_node_put(child);
  1077. return ret;
  1078. }
  1079. }
  1080. for_each_node_by_name(child, "onenand") {
  1081. ret = gpmc_probe_onenand_child(pdev, child);
  1082. if (ret < 0) {
  1083. of_node_put(child);
  1084. return ret;
  1085. }
  1086. }
  1087. return 0;
  1088. }
  1089. #else
  1090. static int gpmc_probe_dt(struct platform_device *pdev)
  1091. {
  1092. return 0;
  1093. }
  1094. #endif
  1095. static int gpmc_probe(struct platform_device *pdev)
  1096. {
  1097. int rc;
  1098. u32 l;
  1099. struct resource *res;
  1100. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1101. if (res == NULL)
  1102. return -ENOENT;
  1103. phys_base = res->start;
  1104. mem_size = resource_size(res);
  1105. gpmc_base = devm_ioremap_resource(&pdev->dev, res);
  1106. if (IS_ERR(gpmc_base))
  1107. return PTR_ERR(gpmc_base);
  1108. res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
  1109. if (res == NULL)
  1110. dev_warn(&pdev->dev, "Failed to get resource: irq\n");
  1111. else
  1112. gpmc_irq = res->start;
  1113. gpmc_l3_clk = clk_get(&pdev->dev, "fck");
  1114. if (IS_ERR(gpmc_l3_clk)) {
  1115. dev_err(&pdev->dev, "error: clk_get\n");
  1116. gpmc_irq = 0;
  1117. return PTR_ERR(gpmc_l3_clk);
  1118. }
  1119. clk_prepare_enable(gpmc_l3_clk);
  1120. gpmc_dev = &pdev->dev;
  1121. l = gpmc_read_reg(GPMC_REVISION);
  1122. if (GPMC_REVISION_MAJOR(l) > 0x4)
  1123. gpmc_capability = GPMC_HAS_WR_ACCESS | GPMC_HAS_WR_DATA_MUX_BUS;
  1124. dev_info(gpmc_dev, "GPMC revision %d.%d\n", GPMC_REVISION_MAJOR(l),
  1125. GPMC_REVISION_MINOR(l));
  1126. rc = gpmc_mem_init();
  1127. if (IS_ERR_VALUE(rc)) {
  1128. clk_disable_unprepare(gpmc_l3_clk);
  1129. clk_put(gpmc_l3_clk);
  1130. dev_err(gpmc_dev, "failed to reserve memory\n");
  1131. return rc;
  1132. }
  1133. if (IS_ERR_VALUE(gpmc_setup_irq()))
  1134. dev_warn(gpmc_dev, "gpmc_setup_irq failed\n");
  1135. /* Now the GPMC is initialised, unreserve the chip-selects */
  1136. gpmc_cs_map = 0;
  1137. rc = gpmc_probe_dt(pdev);
  1138. if (rc < 0) {
  1139. clk_disable_unprepare(gpmc_l3_clk);
  1140. clk_put(gpmc_l3_clk);
  1141. dev_err(gpmc_dev, "failed to probe DT parameters\n");
  1142. return rc;
  1143. }
  1144. return 0;
  1145. }
  1146. static int gpmc_remove(struct platform_device *pdev)
  1147. {
  1148. gpmc_free_irq();
  1149. gpmc_mem_exit();
  1150. gpmc_dev = NULL;
  1151. return 0;
  1152. }
  1153. static struct platform_driver gpmc_driver = {
  1154. .probe = gpmc_probe,
  1155. .remove = gpmc_remove,
  1156. .driver = {
  1157. .name = DEVICE_NAME,
  1158. .owner = THIS_MODULE,
  1159. .of_match_table = of_match_ptr(gpmc_dt_ids),
  1160. },
  1161. };
  1162. static __init int gpmc_init(void)
  1163. {
  1164. return platform_driver_register(&gpmc_driver);
  1165. }
  1166. static __exit void gpmc_exit(void)
  1167. {
  1168. platform_driver_unregister(&gpmc_driver);
  1169. }
  1170. omap_postcore_initcall(gpmc_init);
  1171. module_exit(gpmc_exit);
  1172. static int __init omap_gpmc_init(void)
  1173. {
  1174. struct omap_hwmod *oh;
  1175. struct platform_device *pdev;
  1176. char *oh_name = "gpmc";
  1177. /*
  1178. * if the board boots up with a populated DT, do not
  1179. * manually add the device from this initcall
  1180. */
  1181. if (of_have_populated_dt())
  1182. return -ENODEV;
  1183. oh = omap_hwmod_lookup(oh_name);
  1184. if (!oh) {
  1185. pr_err("Could not look up %s\n", oh_name);
  1186. return -ENODEV;
  1187. }
  1188. pdev = omap_device_build(DEVICE_NAME, -1, oh, NULL, 0);
  1189. WARN(IS_ERR(pdev), "could not build omap_device for %s\n", oh_name);
  1190. return IS_ERR(pdev) ? PTR_ERR(pdev) : 0;
  1191. }
  1192. omap_postcore_initcall(omap_gpmc_init);
  1193. static irqreturn_t gpmc_handle_irq(int irq, void *dev)
  1194. {
  1195. int i;
  1196. u32 regval;
  1197. regval = gpmc_read_reg(GPMC_IRQSTATUS);
  1198. if (!regval)
  1199. return IRQ_NONE;
  1200. for (i = 0; i < GPMC_NR_IRQ; i++)
  1201. if (regval & gpmc_client_irq[i].bitmask)
  1202. generic_handle_irq(gpmc_client_irq[i].irq);
  1203. gpmc_write_reg(GPMC_IRQSTATUS, regval);
  1204. return IRQ_HANDLED;
  1205. }
  1206. #ifdef CONFIG_ARCH_OMAP3
  1207. static struct omap3_gpmc_regs gpmc_context;
  1208. void omap3_gpmc_save_context(void)
  1209. {
  1210. int i;
  1211. gpmc_context.sysconfig = gpmc_read_reg(GPMC_SYSCONFIG);
  1212. gpmc_context.irqenable = gpmc_read_reg(GPMC_IRQENABLE);
  1213. gpmc_context.timeout_ctrl = gpmc_read_reg(GPMC_TIMEOUT_CONTROL);
  1214. gpmc_context.config = gpmc_read_reg(GPMC_CONFIG);
  1215. gpmc_context.prefetch_config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1);
  1216. gpmc_context.prefetch_config2 = gpmc_read_reg(GPMC_PREFETCH_CONFIG2);
  1217. gpmc_context.prefetch_control = gpmc_read_reg(GPMC_PREFETCH_CONTROL);
  1218. for (i = 0; i < GPMC_CS_NUM; i++) {
  1219. gpmc_context.cs_context[i].is_valid = gpmc_cs_mem_enabled(i);
  1220. if (gpmc_context.cs_context[i].is_valid) {
  1221. gpmc_context.cs_context[i].config1 =
  1222. gpmc_cs_read_reg(i, GPMC_CS_CONFIG1);
  1223. gpmc_context.cs_context[i].config2 =
  1224. gpmc_cs_read_reg(i, GPMC_CS_CONFIG2);
  1225. gpmc_context.cs_context[i].config3 =
  1226. gpmc_cs_read_reg(i, GPMC_CS_CONFIG3);
  1227. gpmc_context.cs_context[i].config4 =
  1228. gpmc_cs_read_reg(i, GPMC_CS_CONFIG4);
  1229. gpmc_context.cs_context[i].config5 =
  1230. gpmc_cs_read_reg(i, GPMC_CS_CONFIG5);
  1231. gpmc_context.cs_context[i].config6 =
  1232. gpmc_cs_read_reg(i, GPMC_CS_CONFIG6);
  1233. gpmc_context.cs_context[i].config7 =
  1234. gpmc_cs_read_reg(i, GPMC_CS_CONFIG7);
  1235. }
  1236. }
  1237. }
  1238. void omap3_gpmc_restore_context(void)
  1239. {
  1240. int i;
  1241. gpmc_write_reg(GPMC_SYSCONFIG, gpmc_context.sysconfig);
  1242. gpmc_write_reg(GPMC_IRQENABLE, gpmc_context.irqenable);
  1243. gpmc_write_reg(GPMC_TIMEOUT_CONTROL, gpmc_context.timeout_ctrl);
  1244. gpmc_write_reg(GPMC_CONFIG, gpmc_context.config);
  1245. gpmc_write_reg(GPMC_PREFETCH_CONFIG1, gpmc_context.prefetch_config1);
  1246. gpmc_write_reg(GPMC_PREFETCH_CONFIG2, gpmc_context.prefetch_config2);
  1247. gpmc_write_reg(GPMC_PREFETCH_CONTROL, gpmc_context.prefetch_control);
  1248. for (i = 0; i < GPMC_CS_NUM; i++) {
  1249. if (gpmc_context.cs_context[i].is_valid) {
  1250. gpmc_cs_write_reg(i, GPMC_CS_CONFIG1,
  1251. gpmc_context.cs_context[i].config1);
  1252. gpmc_cs_write_reg(i, GPMC_CS_CONFIG2,
  1253. gpmc_context.cs_context[i].config2);
  1254. gpmc_cs_write_reg(i, GPMC_CS_CONFIG3,
  1255. gpmc_context.cs_context[i].config3);
  1256. gpmc_cs_write_reg(i, GPMC_CS_CONFIG4,
  1257. gpmc_context.cs_context[i].config4);
  1258. gpmc_cs_write_reg(i, GPMC_CS_CONFIG5,
  1259. gpmc_context.cs_context[i].config5);
  1260. gpmc_cs_write_reg(i, GPMC_CS_CONFIG6,
  1261. gpmc_context.cs_context[i].config6);
  1262. gpmc_cs_write_reg(i, GPMC_CS_CONFIG7,
  1263. gpmc_context.cs_context[i].config7);
  1264. }
  1265. }
  1266. }
  1267. #endif /* CONFIG_ARCH_OMAP3 */