gpmc.c 32 KB

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