gpmc.c 21 KB

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