gpmc.c 20 KB

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