gpmc.c 22 KB

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