gpmc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914
  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 <asm/mach-types.h>
  27. #include <plat/gpmc.h>
  28. #include <plat/sdrc.h>
  29. /* GPMC register offsets */
  30. #define GPMC_REVISION 0x00
  31. #define GPMC_SYSCONFIG 0x10
  32. #define GPMC_SYSSTATUS 0x14
  33. #define GPMC_IRQSTATUS 0x18
  34. #define GPMC_IRQENABLE 0x1c
  35. #define GPMC_TIMEOUT_CONTROL 0x40
  36. #define GPMC_ERR_ADDRESS 0x44
  37. #define GPMC_ERR_TYPE 0x48
  38. #define GPMC_CONFIG 0x50
  39. #define GPMC_STATUS 0x54
  40. #define GPMC_PREFETCH_CONFIG1 0x1e0
  41. #define GPMC_PREFETCH_CONFIG2 0x1e4
  42. #define GPMC_PREFETCH_CONTROL 0x1ec
  43. #define GPMC_PREFETCH_STATUS 0x1f0
  44. #define GPMC_ECC_CONFIG 0x1f4
  45. #define GPMC_ECC_CONTROL 0x1f8
  46. #define GPMC_ECC_SIZE_CONFIG 0x1fc
  47. #define GPMC_ECC1_RESULT 0x200
  48. #define GPMC_CS0_OFFSET 0x60
  49. #define GPMC_CS_SIZE 0x30
  50. #define GPMC_MEM_START 0x00000000
  51. #define GPMC_MEM_END 0x3FFFFFFF
  52. #define BOOT_ROM_SPACE 0x100000 /* 1MB */
  53. #define GPMC_CHUNK_SHIFT 24 /* 16 MB */
  54. #define GPMC_SECTION_SHIFT 28 /* 128 MB */
  55. #define CS_NUM_SHIFT 24
  56. #define ENABLE_PREFETCH (0x1 << 7)
  57. #define DMA_MPU_MODE 2
  58. /* Structure to save gpmc cs context */
  59. struct gpmc_cs_config {
  60. u32 config1;
  61. u32 config2;
  62. u32 config3;
  63. u32 config4;
  64. u32 config5;
  65. u32 config6;
  66. u32 config7;
  67. int is_valid;
  68. };
  69. /*
  70. * Structure to save/restore gpmc context
  71. * to support core off on OMAP3
  72. */
  73. struct omap3_gpmc_regs {
  74. u32 sysconfig;
  75. u32 irqenable;
  76. u32 timeout_ctrl;
  77. u32 config;
  78. u32 prefetch_config1;
  79. u32 prefetch_config2;
  80. u32 prefetch_control;
  81. struct gpmc_cs_config cs_context[GPMC_CS_NUM];
  82. };
  83. static struct resource gpmc_mem_root;
  84. static struct resource gpmc_cs_mem[GPMC_CS_NUM];
  85. static DEFINE_SPINLOCK(gpmc_mem_lock);
  86. static unsigned int gpmc_cs_map; /* flag for cs which are initialized */
  87. static int gpmc_ecc_used = -EINVAL; /* cs using ecc engine */
  88. static void __iomem *gpmc_base;
  89. static struct clk *gpmc_l3_clk;
  90. static irqreturn_t gpmc_handle_irq(int irq, void *dev);
  91. static void gpmc_write_reg(int idx, u32 val)
  92. {
  93. __raw_writel(val, gpmc_base + idx);
  94. }
  95. static u32 gpmc_read_reg(int idx)
  96. {
  97. return __raw_readl(gpmc_base + idx);
  98. }
  99. static void gpmc_cs_write_byte(int cs, int idx, u8 val)
  100. {
  101. void __iomem *reg_addr;
  102. reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
  103. __raw_writeb(val, reg_addr);
  104. }
  105. static u8 gpmc_cs_read_byte(int cs, int idx)
  106. {
  107. void __iomem *reg_addr;
  108. reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
  109. return __raw_readb(reg_addr);
  110. }
  111. void gpmc_cs_write_reg(int cs, int idx, u32 val)
  112. {
  113. void __iomem *reg_addr;
  114. reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
  115. __raw_writel(val, reg_addr);
  116. }
  117. u32 gpmc_cs_read_reg(int cs, int idx)
  118. {
  119. void __iomem *reg_addr;
  120. reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
  121. return __raw_readl(reg_addr);
  122. }
  123. /* TODO: Add support for gpmc_fck to clock framework and use it */
  124. unsigned long gpmc_get_fclk_period(void)
  125. {
  126. unsigned long rate = clk_get_rate(gpmc_l3_clk);
  127. if (rate == 0) {
  128. printk(KERN_WARNING "gpmc_l3_clk not enabled\n");
  129. return 0;
  130. }
  131. rate /= 1000;
  132. rate = 1000000000 / rate; /* In picoseconds */
  133. return rate;
  134. }
  135. unsigned int gpmc_ns_to_ticks(unsigned int time_ns)
  136. {
  137. unsigned long tick_ps;
  138. /* Calculate in picosecs to yield more exact results */
  139. tick_ps = gpmc_get_fclk_period();
  140. return (time_ns * 1000 + tick_ps - 1) / tick_ps;
  141. }
  142. unsigned int gpmc_ps_to_ticks(unsigned int time_ps)
  143. {
  144. unsigned long tick_ps;
  145. /* Calculate in picosecs to yield more exact results */
  146. tick_ps = gpmc_get_fclk_period();
  147. return (time_ps + tick_ps - 1) / tick_ps;
  148. }
  149. unsigned int gpmc_ticks_to_ns(unsigned int ticks)
  150. {
  151. return ticks * gpmc_get_fclk_period() / 1000;
  152. }
  153. unsigned int gpmc_round_ns_to_ticks(unsigned int time_ns)
  154. {
  155. unsigned long ticks = gpmc_ns_to_ticks(time_ns);
  156. return ticks * gpmc_get_fclk_period() / 1000;
  157. }
  158. #ifdef DEBUG
  159. static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
  160. int time, const char *name)
  161. #else
  162. static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
  163. int time)
  164. #endif
  165. {
  166. u32 l;
  167. int ticks, mask, nr_bits;
  168. if (time == 0)
  169. ticks = 0;
  170. else
  171. ticks = gpmc_ns_to_ticks(time);
  172. nr_bits = end_bit - st_bit + 1;
  173. if (ticks >= 1 << nr_bits) {
  174. #ifdef DEBUG
  175. printk(KERN_INFO "GPMC CS%d: %-10s* %3d ns, %3d ticks >= %d\n",
  176. cs, name, time, ticks, 1 << nr_bits);
  177. #endif
  178. return -1;
  179. }
  180. mask = (1 << nr_bits) - 1;
  181. l = gpmc_cs_read_reg(cs, reg);
  182. #ifdef DEBUG
  183. printk(KERN_INFO
  184. "GPMC CS%d: %-10s: %3d ticks, %3lu ns (was %3i ticks) %3d ns\n",
  185. cs, name, ticks, gpmc_get_fclk_period() * ticks / 1000,
  186. (l >> st_bit) & mask, time);
  187. #endif
  188. l &= ~(mask << st_bit);
  189. l |= ticks << st_bit;
  190. gpmc_cs_write_reg(cs, reg, l);
  191. return 0;
  192. }
  193. #ifdef DEBUG
  194. #define GPMC_SET_ONE(reg, st, end, field) \
  195. if (set_gpmc_timing_reg(cs, (reg), (st), (end), \
  196. t->field, #field) < 0) \
  197. return -1
  198. #else
  199. #define GPMC_SET_ONE(reg, st, end, field) \
  200. if (set_gpmc_timing_reg(cs, (reg), (st), (end), t->field) < 0) \
  201. return -1
  202. #endif
  203. int gpmc_cs_calc_divider(int cs, unsigned int sync_clk)
  204. {
  205. int div;
  206. u32 l;
  207. l = sync_clk + (gpmc_get_fclk_period() - 1);
  208. div = l / gpmc_get_fclk_period();
  209. if (div > 4)
  210. return -1;
  211. if (div <= 0)
  212. div = 1;
  213. return div;
  214. }
  215. int gpmc_cs_set_timings(int cs, const struct gpmc_timings *t)
  216. {
  217. int div;
  218. u32 l;
  219. div = gpmc_cs_calc_divider(cs, t->sync_clk);
  220. if (div < 0)
  221. return -1;
  222. GPMC_SET_ONE(GPMC_CS_CONFIG2, 0, 3, cs_on);
  223. GPMC_SET_ONE(GPMC_CS_CONFIG2, 8, 12, cs_rd_off);
  224. GPMC_SET_ONE(GPMC_CS_CONFIG2, 16, 20, cs_wr_off);
  225. GPMC_SET_ONE(GPMC_CS_CONFIG3, 0, 3, adv_on);
  226. GPMC_SET_ONE(GPMC_CS_CONFIG3, 8, 12, adv_rd_off);
  227. GPMC_SET_ONE(GPMC_CS_CONFIG3, 16, 20, adv_wr_off);
  228. GPMC_SET_ONE(GPMC_CS_CONFIG4, 0, 3, oe_on);
  229. GPMC_SET_ONE(GPMC_CS_CONFIG4, 8, 12, oe_off);
  230. GPMC_SET_ONE(GPMC_CS_CONFIG4, 16, 19, we_on);
  231. GPMC_SET_ONE(GPMC_CS_CONFIG4, 24, 28, we_off);
  232. GPMC_SET_ONE(GPMC_CS_CONFIG5, 0, 4, rd_cycle);
  233. GPMC_SET_ONE(GPMC_CS_CONFIG5, 8, 12, wr_cycle);
  234. GPMC_SET_ONE(GPMC_CS_CONFIG5, 16, 20, access);
  235. GPMC_SET_ONE(GPMC_CS_CONFIG5, 24, 27, page_burst_access);
  236. if (cpu_is_omap34xx()) {
  237. GPMC_SET_ONE(GPMC_CS_CONFIG6, 16, 19, wr_data_mux_bus);
  238. GPMC_SET_ONE(GPMC_CS_CONFIG6, 24, 28, wr_access);
  239. }
  240. /* caller is expected to have initialized CONFIG1 to cover
  241. * at least sync vs async
  242. */
  243. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
  244. if (l & (GPMC_CONFIG1_READTYPE_SYNC | GPMC_CONFIG1_WRITETYPE_SYNC)) {
  245. #ifdef DEBUG
  246. printk(KERN_INFO "GPMC CS%d CLK period is %lu ns (div %d)\n",
  247. cs, (div * gpmc_get_fclk_period()) / 1000, div);
  248. #endif
  249. l &= ~0x03;
  250. l |= (div - 1);
  251. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, l);
  252. }
  253. return 0;
  254. }
  255. static void gpmc_cs_enable_mem(int cs, u32 base, u32 size)
  256. {
  257. u32 l;
  258. u32 mask;
  259. mask = (1 << GPMC_SECTION_SHIFT) - size;
  260. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  261. l &= ~0x3f;
  262. l = (base >> GPMC_CHUNK_SHIFT) & 0x3f;
  263. l &= ~(0x0f << 8);
  264. l |= ((mask >> GPMC_CHUNK_SHIFT) & 0x0f) << 8;
  265. l |= GPMC_CONFIG7_CSVALID;
  266. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
  267. }
  268. static void gpmc_cs_disable_mem(int cs)
  269. {
  270. u32 l;
  271. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  272. l &= ~GPMC_CONFIG7_CSVALID;
  273. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
  274. }
  275. static void gpmc_cs_get_memconf(int cs, u32 *base, u32 *size)
  276. {
  277. u32 l;
  278. u32 mask;
  279. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  280. *base = (l & 0x3f) << GPMC_CHUNK_SHIFT;
  281. mask = (l >> 8) & 0x0f;
  282. *size = (1 << GPMC_SECTION_SHIFT) - (mask << GPMC_CHUNK_SHIFT);
  283. }
  284. static int gpmc_cs_mem_enabled(int cs)
  285. {
  286. u32 l;
  287. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  288. return l & GPMC_CONFIG7_CSVALID;
  289. }
  290. int gpmc_cs_set_reserved(int cs, int reserved)
  291. {
  292. if (cs > GPMC_CS_NUM)
  293. return -ENODEV;
  294. gpmc_cs_map &= ~(1 << cs);
  295. gpmc_cs_map |= (reserved ? 1 : 0) << cs;
  296. return 0;
  297. }
  298. int gpmc_cs_reserved(int cs)
  299. {
  300. if (cs > GPMC_CS_NUM)
  301. return -ENODEV;
  302. return gpmc_cs_map & (1 << cs);
  303. }
  304. static unsigned long gpmc_mem_align(unsigned long size)
  305. {
  306. int order;
  307. size = (size - 1) >> (GPMC_CHUNK_SHIFT - 1);
  308. order = GPMC_CHUNK_SHIFT - 1;
  309. do {
  310. size >>= 1;
  311. order++;
  312. } while (size);
  313. size = 1 << order;
  314. return size;
  315. }
  316. static int gpmc_cs_insert_mem(int cs, unsigned long base, unsigned long size)
  317. {
  318. struct resource *res = &gpmc_cs_mem[cs];
  319. int r;
  320. size = gpmc_mem_align(size);
  321. spin_lock(&gpmc_mem_lock);
  322. res->start = base;
  323. res->end = base + size - 1;
  324. r = request_resource(&gpmc_mem_root, res);
  325. spin_unlock(&gpmc_mem_lock);
  326. return r;
  327. }
  328. int gpmc_cs_request(int cs, unsigned long size, unsigned long *base)
  329. {
  330. struct resource *res = &gpmc_cs_mem[cs];
  331. int r = -1;
  332. if (cs > GPMC_CS_NUM)
  333. return -ENODEV;
  334. size = gpmc_mem_align(size);
  335. if (size > (1 << GPMC_SECTION_SHIFT))
  336. return -ENOMEM;
  337. spin_lock(&gpmc_mem_lock);
  338. if (gpmc_cs_reserved(cs)) {
  339. r = -EBUSY;
  340. goto out;
  341. }
  342. if (gpmc_cs_mem_enabled(cs))
  343. r = adjust_resource(res, res->start & ~(size - 1), size);
  344. if (r < 0)
  345. r = allocate_resource(&gpmc_mem_root, res, size, 0, ~0,
  346. size, NULL, NULL);
  347. if (r < 0)
  348. goto out;
  349. gpmc_cs_enable_mem(cs, res->start, resource_size(res));
  350. *base = res->start;
  351. gpmc_cs_set_reserved(cs, 1);
  352. out:
  353. spin_unlock(&gpmc_mem_lock);
  354. return r;
  355. }
  356. EXPORT_SYMBOL(gpmc_cs_request);
  357. void gpmc_cs_free(int cs)
  358. {
  359. spin_lock(&gpmc_mem_lock);
  360. if (cs >= GPMC_CS_NUM || cs < 0 || !gpmc_cs_reserved(cs)) {
  361. printk(KERN_ERR "Trying to free non-reserved GPMC CS%d\n", cs);
  362. BUG();
  363. spin_unlock(&gpmc_mem_lock);
  364. return;
  365. }
  366. gpmc_cs_disable_mem(cs);
  367. release_resource(&gpmc_cs_mem[cs]);
  368. gpmc_cs_set_reserved(cs, 0);
  369. spin_unlock(&gpmc_mem_lock);
  370. }
  371. EXPORT_SYMBOL(gpmc_cs_free);
  372. /**
  373. * gpmc_read_status - read access request to get the different gpmc status
  374. * @cmd: command type
  375. * @return status
  376. */
  377. int gpmc_read_status(int cmd)
  378. {
  379. int status = -EINVAL;
  380. u32 regval = 0;
  381. switch (cmd) {
  382. case GPMC_GET_IRQ_STATUS:
  383. status = gpmc_read_reg(GPMC_IRQSTATUS);
  384. break;
  385. case GPMC_PREFETCH_FIFO_CNT:
  386. regval = gpmc_read_reg(GPMC_PREFETCH_STATUS);
  387. status = GPMC_PREFETCH_STATUS_FIFO_CNT(regval);
  388. break;
  389. case GPMC_PREFETCH_COUNT:
  390. regval = gpmc_read_reg(GPMC_PREFETCH_STATUS);
  391. status = GPMC_PREFETCH_STATUS_COUNT(regval);
  392. break;
  393. case GPMC_STATUS_BUFFER:
  394. regval = gpmc_read_reg(GPMC_STATUS);
  395. /* 1 : buffer is available to write */
  396. status = regval & GPMC_STATUS_BUFF_EMPTY;
  397. break;
  398. default:
  399. printk(KERN_ERR "gpmc_read_status: Not supported\n");
  400. }
  401. return status;
  402. }
  403. EXPORT_SYMBOL(gpmc_read_status);
  404. /**
  405. * gpmc_cs_configure - write request to configure gpmc
  406. * @cs: chip select number
  407. * @cmd: command type
  408. * @wval: value to write
  409. * @return status of the operation
  410. */
  411. int gpmc_cs_configure(int cs, int cmd, int wval)
  412. {
  413. int err = 0;
  414. u32 regval = 0;
  415. switch (cmd) {
  416. case GPMC_ENABLE_IRQ:
  417. gpmc_write_reg(GPMC_IRQENABLE, wval);
  418. break;
  419. case GPMC_SET_IRQ_STATUS:
  420. gpmc_write_reg(GPMC_IRQSTATUS, wval);
  421. break;
  422. case GPMC_CONFIG_WP:
  423. regval = gpmc_read_reg(GPMC_CONFIG);
  424. if (wval)
  425. regval &= ~GPMC_CONFIG_WRITEPROTECT; /* WP is ON */
  426. else
  427. regval |= GPMC_CONFIG_WRITEPROTECT; /* WP is OFF */
  428. gpmc_write_reg(GPMC_CONFIG, regval);
  429. break;
  430. case GPMC_CONFIG_RDY_BSY:
  431. regval = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
  432. if (wval)
  433. regval |= WR_RD_PIN_MONITORING;
  434. else
  435. regval &= ~WR_RD_PIN_MONITORING;
  436. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval);
  437. break;
  438. case GPMC_CONFIG_DEV_SIZE:
  439. regval = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
  440. regval |= GPMC_CONFIG1_DEVICESIZE(wval);
  441. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval);
  442. break;
  443. case GPMC_CONFIG_DEV_TYPE:
  444. regval = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
  445. regval |= GPMC_CONFIG1_DEVICETYPE(wval);
  446. if (wval == GPMC_DEVICETYPE_NOR)
  447. regval |= GPMC_CONFIG1_MUXADDDATA;
  448. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval);
  449. break;
  450. default:
  451. printk(KERN_ERR "gpmc_configure_cs: Not supported\n");
  452. err = -EINVAL;
  453. }
  454. return err;
  455. }
  456. EXPORT_SYMBOL(gpmc_cs_configure);
  457. /**
  458. * gpmc_nand_read - nand specific read access request
  459. * @cs: chip select number
  460. * @cmd: command type
  461. */
  462. int gpmc_nand_read(int cs, int cmd)
  463. {
  464. int rval = -EINVAL;
  465. switch (cmd) {
  466. case GPMC_NAND_DATA:
  467. rval = gpmc_cs_read_byte(cs, GPMC_CS_NAND_DATA);
  468. break;
  469. default:
  470. printk(KERN_ERR "gpmc_read_nand_ctrl: Not supported\n");
  471. }
  472. return rval;
  473. }
  474. EXPORT_SYMBOL(gpmc_nand_read);
  475. /**
  476. * gpmc_nand_write - nand specific write request
  477. * @cs: chip select number
  478. * @cmd: command type
  479. * @wval: value to write
  480. */
  481. int gpmc_nand_write(int cs, int cmd, int wval)
  482. {
  483. int err = 0;
  484. switch (cmd) {
  485. case GPMC_NAND_COMMAND:
  486. gpmc_cs_write_byte(cs, GPMC_CS_NAND_COMMAND, wval);
  487. break;
  488. case GPMC_NAND_ADDRESS:
  489. gpmc_cs_write_byte(cs, GPMC_CS_NAND_ADDRESS, wval);
  490. break;
  491. case GPMC_NAND_DATA:
  492. gpmc_cs_write_byte(cs, GPMC_CS_NAND_DATA, wval);
  493. default:
  494. printk(KERN_ERR "gpmc_write_nand_ctrl: Not supported\n");
  495. err = -EINVAL;
  496. }
  497. return err;
  498. }
  499. EXPORT_SYMBOL(gpmc_nand_write);
  500. /**
  501. * gpmc_prefetch_enable - configures and starts prefetch transfer
  502. * @cs: cs (chip select) number
  503. * @fifo_th: fifo threshold to be used for read/ write
  504. * @dma_mode: dma mode enable (1) or disable (0)
  505. * @u32_count: number of bytes to be transferred
  506. * @is_write: prefetch read(0) or write post(1) mode
  507. */
  508. int gpmc_prefetch_enable(int cs, int fifo_th, int dma_mode,
  509. unsigned int u32_count, int is_write)
  510. {
  511. if (fifo_th > PREFETCH_FIFOTHRESHOLD_MAX) {
  512. pr_err("gpmc: fifo threshold is not supported\n");
  513. return -1;
  514. } else if (!(gpmc_read_reg(GPMC_PREFETCH_CONTROL))) {
  515. /* Set the amount of bytes to be prefetched */
  516. gpmc_write_reg(GPMC_PREFETCH_CONFIG2, u32_count);
  517. /* Set dma/mpu mode, the prefetch read / post write and
  518. * enable the engine. Set which cs is has requested for.
  519. */
  520. gpmc_write_reg(GPMC_PREFETCH_CONFIG1, ((cs << CS_NUM_SHIFT) |
  521. PREFETCH_FIFOTHRESHOLD(fifo_th) |
  522. ENABLE_PREFETCH |
  523. (dma_mode << DMA_MPU_MODE) |
  524. (0x1 & is_write)));
  525. /* Start the prefetch engine */
  526. gpmc_write_reg(GPMC_PREFETCH_CONTROL, 0x1);
  527. } else {
  528. return -EBUSY;
  529. }
  530. return 0;
  531. }
  532. EXPORT_SYMBOL(gpmc_prefetch_enable);
  533. /**
  534. * gpmc_prefetch_reset - disables and stops the prefetch engine
  535. */
  536. int gpmc_prefetch_reset(int cs)
  537. {
  538. u32 config1;
  539. /* check if the same module/cs is trying to reset */
  540. config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1);
  541. if (((config1 >> CS_NUM_SHIFT) & 0x7) != cs)
  542. return -EINVAL;
  543. /* Stop the PFPW engine */
  544. gpmc_write_reg(GPMC_PREFETCH_CONTROL, 0x0);
  545. /* Reset/disable the PFPW engine */
  546. gpmc_write_reg(GPMC_PREFETCH_CONFIG1, 0x0);
  547. return 0;
  548. }
  549. EXPORT_SYMBOL(gpmc_prefetch_reset);
  550. static void __init gpmc_mem_init(void)
  551. {
  552. int cs;
  553. unsigned long boot_rom_space = 0;
  554. /* never allocate the first page, to facilitate bug detection;
  555. * even if we didn't boot from ROM.
  556. */
  557. boot_rom_space = BOOT_ROM_SPACE;
  558. /* In apollon the CS0 is mapped as 0x0000 0000 */
  559. if (machine_is_omap_apollon())
  560. boot_rom_space = 0;
  561. gpmc_mem_root.start = GPMC_MEM_START + boot_rom_space;
  562. gpmc_mem_root.end = GPMC_MEM_END;
  563. /* Reserve all regions that has been set up by bootloader */
  564. for (cs = 0; cs < GPMC_CS_NUM; cs++) {
  565. u32 base, size;
  566. if (!gpmc_cs_mem_enabled(cs))
  567. continue;
  568. gpmc_cs_get_memconf(cs, &base, &size);
  569. if (gpmc_cs_insert_mem(cs, base, size) < 0)
  570. BUG();
  571. }
  572. }
  573. static int __init gpmc_init(void)
  574. {
  575. u32 l, irq;
  576. int cs, ret = -EINVAL;
  577. int gpmc_irq;
  578. char *ck = NULL;
  579. if (cpu_is_omap24xx()) {
  580. ck = "core_l3_ck";
  581. if (cpu_is_omap2420())
  582. l = OMAP2420_GPMC_BASE;
  583. else
  584. l = OMAP34XX_GPMC_BASE;
  585. gpmc_irq = INT_34XX_GPMC_IRQ;
  586. } else if (cpu_is_omap34xx()) {
  587. ck = "gpmc_fck";
  588. l = OMAP34XX_GPMC_BASE;
  589. gpmc_irq = INT_34XX_GPMC_IRQ;
  590. } else if (cpu_is_omap44xx()) {
  591. ck = "gpmc_ck";
  592. l = OMAP44XX_GPMC_BASE;
  593. gpmc_irq = OMAP44XX_IRQ_GPMC;
  594. }
  595. if (WARN_ON(!ck))
  596. return ret;
  597. gpmc_l3_clk = clk_get(NULL, ck);
  598. if (IS_ERR(gpmc_l3_clk)) {
  599. printk(KERN_ERR "Could not get GPMC clock %s\n", ck);
  600. BUG();
  601. }
  602. gpmc_base = ioremap(l, SZ_4K);
  603. if (!gpmc_base) {
  604. clk_put(gpmc_l3_clk);
  605. printk(KERN_ERR "Could not get GPMC register memory\n");
  606. BUG();
  607. }
  608. clk_enable(gpmc_l3_clk);
  609. l = gpmc_read_reg(GPMC_REVISION);
  610. printk(KERN_INFO "GPMC revision %d.%d\n", (l >> 4) & 0x0f, l & 0x0f);
  611. /* Set smart idle mode and automatic L3 clock gating */
  612. l = gpmc_read_reg(GPMC_SYSCONFIG);
  613. l &= 0x03 << 3;
  614. l |= (0x02 << 3) | (1 << 0);
  615. gpmc_write_reg(GPMC_SYSCONFIG, l);
  616. gpmc_mem_init();
  617. /* initalize the irq_chained */
  618. irq = OMAP_GPMC_IRQ_BASE;
  619. for (cs = 0; cs < GPMC_CS_NUM; cs++) {
  620. irq_set_chip_and_handler(irq, &dummy_irq_chip,
  621. handle_simple_irq);
  622. set_irq_flags(irq, IRQF_VALID);
  623. irq++;
  624. }
  625. ret = request_irq(gpmc_irq,
  626. gpmc_handle_irq, IRQF_SHARED, "gpmc", gpmc_base);
  627. if (ret)
  628. pr_err("gpmc: irq-%d could not claim: err %d\n",
  629. gpmc_irq, ret);
  630. return ret;
  631. }
  632. postcore_initcall(gpmc_init);
  633. static irqreturn_t gpmc_handle_irq(int irq, void *dev)
  634. {
  635. u8 cs;
  636. /* check cs to invoke the irq */
  637. cs = ((gpmc_read_reg(GPMC_PREFETCH_CONFIG1)) >> CS_NUM_SHIFT) & 0x7;
  638. if (OMAP_GPMC_IRQ_BASE+cs <= OMAP_GPMC_IRQ_END)
  639. generic_handle_irq(OMAP_GPMC_IRQ_BASE+cs);
  640. return IRQ_HANDLED;
  641. }
  642. #ifdef CONFIG_ARCH_OMAP3
  643. static struct omap3_gpmc_regs gpmc_context;
  644. void omap3_gpmc_save_context(void)
  645. {
  646. int i;
  647. gpmc_context.sysconfig = gpmc_read_reg(GPMC_SYSCONFIG);
  648. gpmc_context.irqenable = gpmc_read_reg(GPMC_IRQENABLE);
  649. gpmc_context.timeout_ctrl = gpmc_read_reg(GPMC_TIMEOUT_CONTROL);
  650. gpmc_context.config = gpmc_read_reg(GPMC_CONFIG);
  651. gpmc_context.prefetch_config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1);
  652. gpmc_context.prefetch_config2 = gpmc_read_reg(GPMC_PREFETCH_CONFIG2);
  653. gpmc_context.prefetch_control = gpmc_read_reg(GPMC_PREFETCH_CONTROL);
  654. for (i = 0; i < GPMC_CS_NUM; i++) {
  655. gpmc_context.cs_context[i].is_valid = gpmc_cs_mem_enabled(i);
  656. if (gpmc_context.cs_context[i].is_valid) {
  657. gpmc_context.cs_context[i].config1 =
  658. gpmc_cs_read_reg(i, GPMC_CS_CONFIG1);
  659. gpmc_context.cs_context[i].config2 =
  660. gpmc_cs_read_reg(i, GPMC_CS_CONFIG2);
  661. gpmc_context.cs_context[i].config3 =
  662. gpmc_cs_read_reg(i, GPMC_CS_CONFIG3);
  663. gpmc_context.cs_context[i].config4 =
  664. gpmc_cs_read_reg(i, GPMC_CS_CONFIG4);
  665. gpmc_context.cs_context[i].config5 =
  666. gpmc_cs_read_reg(i, GPMC_CS_CONFIG5);
  667. gpmc_context.cs_context[i].config6 =
  668. gpmc_cs_read_reg(i, GPMC_CS_CONFIG6);
  669. gpmc_context.cs_context[i].config7 =
  670. gpmc_cs_read_reg(i, GPMC_CS_CONFIG7);
  671. }
  672. }
  673. }
  674. void omap3_gpmc_restore_context(void)
  675. {
  676. int i;
  677. gpmc_write_reg(GPMC_SYSCONFIG, gpmc_context.sysconfig);
  678. gpmc_write_reg(GPMC_IRQENABLE, gpmc_context.irqenable);
  679. gpmc_write_reg(GPMC_TIMEOUT_CONTROL, gpmc_context.timeout_ctrl);
  680. gpmc_write_reg(GPMC_CONFIG, gpmc_context.config);
  681. gpmc_write_reg(GPMC_PREFETCH_CONFIG1, gpmc_context.prefetch_config1);
  682. gpmc_write_reg(GPMC_PREFETCH_CONFIG2, gpmc_context.prefetch_config2);
  683. gpmc_write_reg(GPMC_PREFETCH_CONTROL, gpmc_context.prefetch_control);
  684. for (i = 0; i < GPMC_CS_NUM; i++) {
  685. if (gpmc_context.cs_context[i].is_valid) {
  686. gpmc_cs_write_reg(i, GPMC_CS_CONFIG1,
  687. gpmc_context.cs_context[i].config1);
  688. gpmc_cs_write_reg(i, GPMC_CS_CONFIG2,
  689. gpmc_context.cs_context[i].config2);
  690. gpmc_cs_write_reg(i, GPMC_CS_CONFIG3,
  691. gpmc_context.cs_context[i].config3);
  692. gpmc_cs_write_reg(i, GPMC_CS_CONFIG4,
  693. gpmc_context.cs_context[i].config4);
  694. gpmc_cs_write_reg(i, GPMC_CS_CONFIG5,
  695. gpmc_context.cs_context[i].config5);
  696. gpmc_cs_write_reg(i, GPMC_CS_CONFIG6,
  697. gpmc_context.cs_context[i].config6);
  698. gpmc_cs_write_reg(i, GPMC_CS_CONFIG7,
  699. gpmc_context.cs_context[i].config7);
  700. }
  701. }
  702. }
  703. #endif /* CONFIG_ARCH_OMAP3 */
  704. /**
  705. * gpmc_enable_hwecc - enable hardware ecc functionality
  706. * @cs: chip select number
  707. * @mode: read/write mode
  708. * @dev_width: device bus width(1 for x16, 0 for x8)
  709. * @ecc_size: bytes for which ECC will be generated
  710. */
  711. int gpmc_enable_hwecc(int cs, int mode, int dev_width, int ecc_size)
  712. {
  713. unsigned int val;
  714. /* check if ecc module is in used */
  715. if (gpmc_ecc_used != -EINVAL)
  716. return -EINVAL;
  717. gpmc_ecc_used = cs;
  718. /* clear ecc and enable bits */
  719. val = ((0x00000001<<8) | 0x00000001);
  720. gpmc_write_reg(GPMC_ECC_CONTROL, val);
  721. /* program ecc and result sizes */
  722. val = ((((ecc_size >> 1) - 1) << 22) | (0x0000000F));
  723. gpmc_write_reg(GPMC_ECC_SIZE_CONFIG, val);
  724. switch (mode) {
  725. case GPMC_ECC_READ:
  726. gpmc_write_reg(GPMC_ECC_CONTROL, 0x101);
  727. break;
  728. case GPMC_ECC_READSYN:
  729. gpmc_write_reg(GPMC_ECC_CONTROL, 0x100);
  730. break;
  731. case GPMC_ECC_WRITE:
  732. gpmc_write_reg(GPMC_ECC_CONTROL, 0x101);
  733. break;
  734. default:
  735. printk(KERN_INFO "Error: Unrecognized Mode[%d]!\n", mode);
  736. break;
  737. }
  738. /* (ECC 16 or 8 bit col) | ( CS ) | ECC Enable */
  739. val = (dev_width << 7) | (cs << 1) | (0x1);
  740. gpmc_write_reg(GPMC_ECC_CONFIG, val);
  741. return 0;
  742. }
  743. /**
  744. * gpmc_calculate_ecc - generate non-inverted ecc bytes
  745. * @cs: chip select number
  746. * @dat: data pointer over which ecc is computed
  747. * @ecc_code: ecc code buffer
  748. *
  749. * Using non-inverted ECC is considered ugly since writing a blank
  750. * page (padding) will clear the ECC bytes. This is not a problem as long
  751. * no one is trying to write data on the seemingly unused page. Reading
  752. * an erased page will produce an ECC mismatch between generated and read
  753. * ECC bytes that has to be dealt with separately.
  754. */
  755. int gpmc_calculate_ecc(int cs, const u_char *dat, u_char *ecc_code)
  756. {
  757. unsigned int val = 0x0;
  758. if (gpmc_ecc_used != cs)
  759. return -EINVAL;
  760. /* read ecc result */
  761. val = gpmc_read_reg(GPMC_ECC1_RESULT);
  762. *ecc_code++ = val; /* P128e, ..., P1e */
  763. *ecc_code++ = val >> 16; /* P128o, ..., P1o */
  764. /* P2048o, P1024o, P512o, P256o, P2048e, P1024e, P512e, P256e */
  765. *ecc_code++ = ((val >> 8) & 0x0f) | ((val >> 20) & 0xf0);
  766. gpmc_ecc_used = -EINVAL;
  767. return 0;
  768. }