gpmc.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249
  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/cpu.h>
  29. #include <plat/gpmc.h>
  30. #include <plat/sdrc.h>
  31. #include "soc.h"
  32. #include "common.h"
  33. /* GPMC register offsets */
  34. #define GPMC_REVISION 0x00
  35. #define GPMC_SYSCONFIG 0x10
  36. #define GPMC_SYSSTATUS 0x14
  37. #define GPMC_IRQSTATUS 0x18
  38. #define GPMC_IRQENABLE 0x1c
  39. #define GPMC_TIMEOUT_CONTROL 0x40
  40. #define GPMC_ERR_ADDRESS 0x44
  41. #define GPMC_ERR_TYPE 0x48
  42. #define GPMC_CONFIG 0x50
  43. #define GPMC_STATUS 0x54
  44. #define GPMC_PREFETCH_CONFIG1 0x1e0
  45. #define GPMC_PREFETCH_CONFIG2 0x1e4
  46. #define GPMC_PREFETCH_CONTROL 0x1ec
  47. #define GPMC_PREFETCH_STATUS 0x1f0
  48. #define GPMC_ECC_CONFIG 0x1f4
  49. #define GPMC_ECC_CONTROL 0x1f8
  50. #define GPMC_ECC_SIZE_CONFIG 0x1fc
  51. #define GPMC_ECC1_RESULT 0x200
  52. #define GPMC_ECC_BCH_RESULT_0 0x240 /* not available on OMAP2 */
  53. /* GPMC ECC control settings */
  54. #define GPMC_ECC_CTRL_ECCCLEAR 0x100
  55. #define GPMC_ECC_CTRL_ECCDISABLE 0x000
  56. #define GPMC_ECC_CTRL_ECCREG1 0x001
  57. #define GPMC_ECC_CTRL_ECCREG2 0x002
  58. #define GPMC_ECC_CTRL_ECCREG3 0x003
  59. #define GPMC_ECC_CTRL_ECCREG4 0x004
  60. #define GPMC_ECC_CTRL_ECCREG5 0x005
  61. #define GPMC_ECC_CTRL_ECCREG6 0x006
  62. #define GPMC_ECC_CTRL_ECCREG7 0x007
  63. #define GPMC_ECC_CTRL_ECCREG8 0x008
  64. #define GPMC_ECC_CTRL_ECCREG9 0x009
  65. #define GPMC_CS0_OFFSET 0x60
  66. #define GPMC_CS_SIZE 0x30
  67. #define GPMC_MEM_START 0x00000000
  68. #define GPMC_MEM_END 0x3FFFFFFF
  69. #define BOOT_ROM_SPACE 0x100000 /* 1MB */
  70. #define GPMC_CHUNK_SHIFT 24 /* 16 MB */
  71. #define GPMC_SECTION_SHIFT 28 /* 128 MB */
  72. #define CS_NUM_SHIFT 24
  73. #define ENABLE_PREFETCH (0x1 << 7)
  74. #define DMA_MPU_MODE 2
  75. /* XXX: Only NAND irq has been considered,currently these are the only ones used
  76. */
  77. #define GPMC_NR_IRQ 2
  78. struct gpmc_client_irq {
  79. unsigned irq;
  80. u32 bitmask;
  81. };
  82. /* Structure to save gpmc cs context */
  83. struct gpmc_cs_config {
  84. u32 config1;
  85. u32 config2;
  86. u32 config3;
  87. u32 config4;
  88. u32 config5;
  89. u32 config6;
  90. u32 config7;
  91. int is_valid;
  92. };
  93. /*
  94. * Structure to save/restore gpmc context
  95. * to support core off on OMAP3
  96. */
  97. struct omap3_gpmc_regs {
  98. u32 sysconfig;
  99. u32 irqenable;
  100. u32 timeout_ctrl;
  101. u32 config;
  102. u32 prefetch_config1;
  103. u32 prefetch_config2;
  104. u32 prefetch_control;
  105. struct gpmc_cs_config cs_context[GPMC_CS_NUM];
  106. };
  107. static struct gpmc_client_irq gpmc_client_irq[GPMC_NR_IRQ];
  108. static struct irq_chip gpmc_irq_chip;
  109. static unsigned gpmc_irq_start;
  110. static struct resource gpmc_mem_root;
  111. static struct resource gpmc_cs_mem[GPMC_CS_NUM];
  112. static DEFINE_SPINLOCK(gpmc_mem_lock);
  113. static unsigned int gpmc_cs_map; /* flag for cs which are initialized */
  114. static int gpmc_ecc_used = -EINVAL; /* cs using ecc engine */
  115. static void __iomem *gpmc_base;
  116. static struct clk *gpmc_l3_clk;
  117. static irqreturn_t gpmc_handle_irq(int irq, void *dev);
  118. static void gpmc_write_reg(int idx, u32 val)
  119. {
  120. __raw_writel(val, gpmc_base + idx);
  121. }
  122. static u32 gpmc_read_reg(int idx)
  123. {
  124. return __raw_readl(gpmc_base + idx);
  125. }
  126. static void gpmc_cs_write_byte(int cs, int idx, u8 val)
  127. {
  128. void __iomem *reg_addr;
  129. reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
  130. __raw_writeb(val, reg_addr);
  131. }
  132. static u8 gpmc_cs_read_byte(int cs, int idx)
  133. {
  134. void __iomem *reg_addr;
  135. reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
  136. return __raw_readb(reg_addr);
  137. }
  138. void gpmc_cs_write_reg(int cs, int idx, u32 val)
  139. {
  140. void __iomem *reg_addr;
  141. reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
  142. __raw_writel(val, reg_addr);
  143. }
  144. u32 gpmc_cs_read_reg(int cs, int idx)
  145. {
  146. void __iomem *reg_addr;
  147. reg_addr = gpmc_base + GPMC_CS0_OFFSET + (cs * GPMC_CS_SIZE) + idx;
  148. return __raw_readl(reg_addr);
  149. }
  150. /* TODO: Add support for gpmc_fck to clock framework and use it */
  151. unsigned long gpmc_get_fclk_period(void)
  152. {
  153. unsigned long rate = clk_get_rate(gpmc_l3_clk);
  154. if (rate == 0) {
  155. printk(KERN_WARNING "gpmc_l3_clk not enabled\n");
  156. return 0;
  157. }
  158. rate /= 1000;
  159. rate = 1000000000 / rate; /* In picoseconds */
  160. return rate;
  161. }
  162. unsigned int gpmc_ns_to_ticks(unsigned int time_ns)
  163. {
  164. unsigned long tick_ps;
  165. /* Calculate in picosecs to yield more exact results */
  166. tick_ps = gpmc_get_fclk_period();
  167. return (time_ns * 1000 + tick_ps - 1) / tick_ps;
  168. }
  169. unsigned int gpmc_ps_to_ticks(unsigned int time_ps)
  170. {
  171. unsigned long tick_ps;
  172. /* Calculate in picosecs to yield more exact results */
  173. tick_ps = gpmc_get_fclk_period();
  174. return (time_ps + tick_ps - 1) / tick_ps;
  175. }
  176. unsigned int gpmc_ticks_to_ns(unsigned int ticks)
  177. {
  178. return ticks * gpmc_get_fclk_period() / 1000;
  179. }
  180. unsigned int gpmc_round_ns_to_ticks(unsigned int time_ns)
  181. {
  182. unsigned long ticks = gpmc_ns_to_ticks(time_ns);
  183. return ticks * gpmc_get_fclk_period() / 1000;
  184. }
  185. #ifdef DEBUG
  186. static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
  187. int time, const char *name)
  188. #else
  189. static int set_gpmc_timing_reg(int cs, int reg, int st_bit, int end_bit,
  190. int time)
  191. #endif
  192. {
  193. u32 l;
  194. int ticks, mask, nr_bits;
  195. if (time == 0)
  196. ticks = 0;
  197. else
  198. ticks = gpmc_ns_to_ticks(time);
  199. nr_bits = end_bit - st_bit + 1;
  200. if (ticks >= 1 << nr_bits) {
  201. #ifdef DEBUG
  202. printk(KERN_INFO "GPMC CS%d: %-10s* %3d ns, %3d ticks >= %d\n",
  203. cs, name, time, ticks, 1 << nr_bits);
  204. #endif
  205. return -1;
  206. }
  207. mask = (1 << nr_bits) - 1;
  208. l = gpmc_cs_read_reg(cs, reg);
  209. #ifdef DEBUG
  210. printk(KERN_INFO
  211. "GPMC CS%d: %-10s: %3d ticks, %3lu ns (was %3i ticks) %3d ns\n",
  212. cs, name, ticks, gpmc_get_fclk_period() * ticks / 1000,
  213. (l >> st_bit) & mask, time);
  214. #endif
  215. l &= ~(mask << st_bit);
  216. l |= ticks << st_bit;
  217. gpmc_cs_write_reg(cs, reg, l);
  218. return 0;
  219. }
  220. #ifdef DEBUG
  221. #define GPMC_SET_ONE(reg, st, end, field) \
  222. if (set_gpmc_timing_reg(cs, (reg), (st), (end), \
  223. t->field, #field) < 0) \
  224. return -1
  225. #else
  226. #define GPMC_SET_ONE(reg, st, end, field) \
  227. if (set_gpmc_timing_reg(cs, (reg), (st), (end), t->field) < 0) \
  228. return -1
  229. #endif
  230. int gpmc_cs_calc_divider(int cs, unsigned int sync_clk)
  231. {
  232. int div;
  233. u32 l;
  234. l = sync_clk + (gpmc_get_fclk_period() - 1);
  235. div = l / gpmc_get_fclk_period();
  236. if (div > 4)
  237. return -1;
  238. if (div <= 0)
  239. div = 1;
  240. return div;
  241. }
  242. int gpmc_cs_set_timings(int cs, const struct gpmc_timings *t)
  243. {
  244. int div;
  245. u32 l;
  246. div = gpmc_cs_calc_divider(cs, t->sync_clk);
  247. if (div < 0)
  248. return -1;
  249. GPMC_SET_ONE(GPMC_CS_CONFIG2, 0, 3, cs_on);
  250. GPMC_SET_ONE(GPMC_CS_CONFIG2, 8, 12, cs_rd_off);
  251. GPMC_SET_ONE(GPMC_CS_CONFIG2, 16, 20, cs_wr_off);
  252. GPMC_SET_ONE(GPMC_CS_CONFIG3, 0, 3, adv_on);
  253. GPMC_SET_ONE(GPMC_CS_CONFIG3, 8, 12, adv_rd_off);
  254. GPMC_SET_ONE(GPMC_CS_CONFIG3, 16, 20, adv_wr_off);
  255. GPMC_SET_ONE(GPMC_CS_CONFIG4, 0, 3, oe_on);
  256. GPMC_SET_ONE(GPMC_CS_CONFIG4, 8, 12, oe_off);
  257. GPMC_SET_ONE(GPMC_CS_CONFIG4, 16, 19, we_on);
  258. GPMC_SET_ONE(GPMC_CS_CONFIG4, 24, 28, we_off);
  259. GPMC_SET_ONE(GPMC_CS_CONFIG5, 0, 4, rd_cycle);
  260. GPMC_SET_ONE(GPMC_CS_CONFIG5, 8, 12, wr_cycle);
  261. GPMC_SET_ONE(GPMC_CS_CONFIG5, 16, 20, access);
  262. GPMC_SET_ONE(GPMC_CS_CONFIG5, 24, 27, page_burst_access);
  263. if (cpu_is_omap34xx()) {
  264. GPMC_SET_ONE(GPMC_CS_CONFIG6, 16, 19, wr_data_mux_bus);
  265. GPMC_SET_ONE(GPMC_CS_CONFIG6, 24, 28, wr_access);
  266. }
  267. /* caller is expected to have initialized CONFIG1 to cover
  268. * at least sync vs async
  269. */
  270. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
  271. if (l & (GPMC_CONFIG1_READTYPE_SYNC | GPMC_CONFIG1_WRITETYPE_SYNC)) {
  272. #ifdef DEBUG
  273. printk(KERN_INFO "GPMC CS%d CLK period is %lu ns (div %d)\n",
  274. cs, (div * gpmc_get_fclk_period()) / 1000, div);
  275. #endif
  276. l &= ~0x03;
  277. l |= (div - 1);
  278. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, l);
  279. }
  280. return 0;
  281. }
  282. static void gpmc_cs_enable_mem(int cs, u32 base, u32 size)
  283. {
  284. u32 l;
  285. u32 mask;
  286. mask = (1 << GPMC_SECTION_SHIFT) - size;
  287. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  288. l &= ~0x3f;
  289. l = (base >> GPMC_CHUNK_SHIFT) & 0x3f;
  290. l &= ~(0x0f << 8);
  291. l |= ((mask >> GPMC_CHUNK_SHIFT) & 0x0f) << 8;
  292. l |= GPMC_CONFIG7_CSVALID;
  293. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
  294. }
  295. static void gpmc_cs_disable_mem(int cs)
  296. {
  297. u32 l;
  298. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  299. l &= ~GPMC_CONFIG7_CSVALID;
  300. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG7, l);
  301. }
  302. static void gpmc_cs_get_memconf(int cs, u32 *base, u32 *size)
  303. {
  304. u32 l;
  305. u32 mask;
  306. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  307. *base = (l & 0x3f) << GPMC_CHUNK_SHIFT;
  308. mask = (l >> 8) & 0x0f;
  309. *size = (1 << GPMC_SECTION_SHIFT) - (mask << GPMC_CHUNK_SHIFT);
  310. }
  311. static int gpmc_cs_mem_enabled(int cs)
  312. {
  313. u32 l;
  314. l = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG7);
  315. return l & GPMC_CONFIG7_CSVALID;
  316. }
  317. int gpmc_cs_set_reserved(int cs, int reserved)
  318. {
  319. if (cs > GPMC_CS_NUM)
  320. return -ENODEV;
  321. gpmc_cs_map &= ~(1 << cs);
  322. gpmc_cs_map |= (reserved ? 1 : 0) << cs;
  323. return 0;
  324. }
  325. int gpmc_cs_reserved(int cs)
  326. {
  327. if (cs > GPMC_CS_NUM)
  328. return -ENODEV;
  329. return gpmc_cs_map & (1 << cs);
  330. }
  331. static unsigned long gpmc_mem_align(unsigned long size)
  332. {
  333. int order;
  334. size = (size - 1) >> (GPMC_CHUNK_SHIFT - 1);
  335. order = GPMC_CHUNK_SHIFT - 1;
  336. do {
  337. size >>= 1;
  338. order++;
  339. } while (size);
  340. size = 1 << order;
  341. return size;
  342. }
  343. static int gpmc_cs_insert_mem(int cs, unsigned long base, unsigned long size)
  344. {
  345. struct resource *res = &gpmc_cs_mem[cs];
  346. int r;
  347. size = gpmc_mem_align(size);
  348. spin_lock(&gpmc_mem_lock);
  349. res->start = base;
  350. res->end = base + size - 1;
  351. r = request_resource(&gpmc_mem_root, res);
  352. spin_unlock(&gpmc_mem_lock);
  353. return r;
  354. }
  355. int gpmc_cs_request(int cs, unsigned long size, unsigned long *base)
  356. {
  357. struct resource *res = &gpmc_cs_mem[cs];
  358. int r = -1;
  359. if (cs > GPMC_CS_NUM)
  360. return -ENODEV;
  361. size = gpmc_mem_align(size);
  362. if (size > (1 << GPMC_SECTION_SHIFT))
  363. return -ENOMEM;
  364. spin_lock(&gpmc_mem_lock);
  365. if (gpmc_cs_reserved(cs)) {
  366. r = -EBUSY;
  367. goto out;
  368. }
  369. if (gpmc_cs_mem_enabled(cs))
  370. r = adjust_resource(res, res->start & ~(size - 1), size);
  371. if (r < 0)
  372. r = allocate_resource(&gpmc_mem_root, res, size, 0, ~0,
  373. size, NULL, NULL);
  374. if (r < 0)
  375. goto out;
  376. gpmc_cs_enable_mem(cs, res->start, resource_size(res));
  377. *base = res->start;
  378. gpmc_cs_set_reserved(cs, 1);
  379. out:
  380. spin_unlock(&gpmc_mem_lock);
  381. return r;
  382. }
  383. EXPORT_SYMBOL(gpmc_cs_request);
  384. void gpmc_cs_free(int cs)
  385. {
  386. spin_lock(&gpmc_mem_lock);
  387. if (cs >= GPMC_CS_NUM || cs < 0 || !gpmc_cs_reserved(cs)) {
  388. printk(KERN_ERR "Trying to free non-reserved GPMC CS%d\n", cs);
  389. BUG();
  390. spin_unlock(&gpmc_mem_lock);
  391. return;
  392. }
  393. gpmc_cs_disable_mem(cs);
  394. release_resource(&gpmc_cs_mem[cs]);
  395. gpmc_cs_set_reserved(cs, 0);
  396. spin_unlock(&gpmc_mem_lock);
  397. }
  398. EXPORT_SYMBOL(gpmc_cs_free);
  399. /**
  400. * gpmc_read_status - read access request to get the different gpmc status
  401. * @cmd: command type
  402. * @return status
  403. */
  404. int gpmc_read_status(int cmd)
  405. {
  406. int status = -EINVAL;
  407. u32 regval = 0;
  408. switch (cmd) {
  409. case GPMC_GET_IRQ_STATUS:
  410. status = gpmc_read_reg(GPMC_IRQSTATUS);
  411. break;
  412. case GPMC_PREFETCH_FIFO_CNT:
  413. regval = gpmc_read_reg(GPMC_PREFETCH_STATUS);
  414. status = GPMC_PREFETCH_STATUS_FIFO_CNT(regval);
  415. break;
  416. case GPMC_PREFETCH_COUNT:
  417. regval = gpmc_read_reg(GPMC_PREFETCH_STATUS);
  418. status = GPMC_PREFETCH_STATUS_COUNT(regval);
  419. break;
  420. case GPMC_STATUS_BUFFER:
  421. regval = gpmc_read_reg(GPMC_STATUS);
  422. /* 1 : buffer is available to write */
  423. status = regval & GPMC_STATUS_BUFF_EMPTY;
  424. break;
  425. default:
  426. printk(KERN_ERR "gpmc_read_status: Not supported\n");
  427. }
  428. return status;
  429. }
  430. EXPORT_SYMBOL(gpmc_read_status);
  431. /**
  432. * gpmc_cs_configure - write request to configure gpmc
  433. * @cs: chip select number
  434. * @cmd: command type
  435. * @wval: value to write
  436. * @return status of the operation
  437. */
  438. int gpmc_cs_configure(int cs, int cmd, int wval)
  439. {
  440. int err = 0;
  441. u32 regval = 0;
  442. switch (cmd) {
  443. case GPMC_ENABLE_IRQ:
  444. gpmc_write_reg(GPMC_IRQENABLE, wval);
  445. break;
  446. case GPMC_SET_IRQ_STATUS:
  447. gpmc_write_reg(GPMC_IRQSTATUS, wval);
  448. break;
  449. case GPMC_CONFIG_WP:
  450. regval = gpmc_read_reg(GPMC_CONFIG);
  451. if (wval)
  452. regval &= ~GPMC_CONFIG_WRITEPROTECT; /* WP is ON */
  453. else
  454. regval |= GPMC_CONFIG_WRITEPROTECT; /* WP is OFF */
  455. gpmc_write_reg(GPMC_CONFIG, regval);
  456. break;
  457. case GPMC_CONFIG_RDY_BSY:
  458. regval = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
  459. if (wval)
  460. regval |= WR_RD_PIN_MONITORING;
  461. else
  462. regval &= ~WR_RD_PIN_MONITORING;
  463. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval);
  464. break;
  465. case GPMC_CONFIG_DEV_SIZE:
  466. regval = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
  467. /* clear 2 target bits */
  468. regval &= ~GPMC_CONFIG1_DEVICESIZE(3);
  469. /* set the proper value */
  470. regval |= GPMC_CONFIG1_DEVICESIZE(wval);
  471. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval);
  472. break;
  473. case GPMC_CONFIG_DEV_TYPE:
  474. regval = gpmc_cs_read_reg(cs, GPMC_CS_CONFIG1);
  475. regval |= GPMC_CONFIG1_DEVICETYPE(wval);
  476. if (wval == GPMC_DEVICETYPE_NOR)
  477. regval |= GPMC_CONFIG1_MUXADDDATA;
  478. gpmc_cs_write_reg(cs, GPMC_CS_CONFIG1, regval);
  479. break;
  480. default:
  481. printk(KERN_ERR "gpmc_configure_cs: Not supported\n");
  482. err = -EINVAL;
  483. }
  484. return err;
  485. }
  486. EXPORT_SYMBOL(gpmc_cs_configure);
  487. /**
  488. * gpmc_nand_read - nand specific read access request
  489. * @cs: chip select number
  490. * @cmd: command type
  491. */
  492. int gpmc_nand_read(int cs, int cmd)
  493. {
  494. int rval = -EINVAL;
  495. switch (cmd) {
  496. case GPMC_NAND_DATA:
  497. rval = gpmc_cs_read_byte(cs, GPMC_CS_NAND_DATA);
  498. break;
  499. default:
  500. printk(KERN_ERR "gpmc_read_nand_ctrl: Not supported\n");
  501. }
  502. return rval;
  503. }
  504. EXPORT_SYMBOL(gpmc_nand_read);
  505. /**
  506. * gpmc_nand_write - nand specific write request
  507. * @cs: chip select number
  508. * @cmd: command type
  509. * @wval: value to write
  510. */
  511. int gpmc_nand_write(int cs, int cmd, int wval)
  512. {
  513. int err = 0;
  514. switch (cmd) {
  515. case GPMC_NAND_COMMAND:
  516. gpmc_cs_write_byte(cs, GPMC_CS_NAND_COMMAND, wval);
  517. break;
  518. case GPMC_NAND_ADDRESS:
  519. gpmc_cs_write_byte(cs, GPMC_CS_NAND_ADDRESS, wval);
  520. break;
  521. case GPMC_NAND_DATA:
  522. gpmc_cs_write_byte(cs, GPMC_CS_NAND_DATA, wval);
  523. default:
  524. printk(KERN_ERR "gpmc_write_nand_ctrl: Not supported\n");
  525. err = -EINVAL;
  526. }
  527. return err;
  528. }
  529. EXPORT_SYMBOL(gpmc_nand_write);
  530. /**
  531. * gpmc_prefetch_enable - configures and starts prefetch transfer
  532. * @cs: cs (chip select) number
  533. * @fifo_th: fifo threshold to be used for read/ write
  534. * @dma_mode: dma mode enable (1) or disable (0)
  535. * @u32_count: number of bytes to be transferred
  536. * @is_write: prefetch read(0) or write post(1) mode
  537. */
  538. int gpmc_prefetch_enable(int cs, int fifo_th, int dma_mode,
  539. unsigned int u32_count, int is_write)
  540. {
  541. if (fifo_th > PREFETCH_FIFOTHRESHOLD_MAX) {
  542. pr_err("gpmc: fifo threshold is not supported\n");
  543. return -1;
  544. } else if (!(gpmc_read_reg(GPMC_PREFETCH_CONTROL))) {
  545. /* Set the amount of bytes to be prefetched */
  546. gpmc_write_reg(GPMC_PREFETCH_CONFIG2, u32_count);
  547. /* Set dma/mpu mode, the prefetch read / post write and
  548. * enable the engine. Set which cs is has requested for.
  549. */
  550. gpmc_write_reg(GPMC_PREFETCH_CONFIG1, ((cs << CS_NUM_SHIFT) |
  551. PREFETCH_FIFOTHRESHOLD(fifo_th) |
  552. ENABLE_PREFETCH |
  553. (dma_mode << DMA_MPU_MODE) |
  554. (0x1 & is_write)));
  555. /* Start the prefetch engine */
  556. gpmc_write_reg(GPMC_PREFETCH_CONTROL, 0x1);
  557. } else {
  558. return -EBUSY;
  559. }
  560. return 0;
  561. }
  562. EXPORT_SYMBOL(gpmc_prefetch_enable);
  563. /**
  564. * gpmc_prefetch_reset - disables and stops the prefetch engine
  565. */
  566. int gpmc_prefetch_reset(int cs)
  567. {
  568. u32 config1;
  569. /* check if the same module/cs is trying to reset */
  570. config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1);
  571. if (((config1 >> CS_NUM_SHIFT) & 0x7) != cs)
  572. return -EINVAL;
  573. /* Stop the PFPW engine */
  574. gpmc_write_reg(GPMC_PREFETCH_CONTROL, 0x0);
  575. /* Reset/disable the PFPW engine */
  576. gpmc_write_reg(GPMC_PREFETCH_CONFIG1, 0x0);
  577. return 0;
  578. }
  579. EXPORT_SYMBOL(gpmc_prefetch_reset);
  580. void gpmc_update_nand_reg(struct gpmc_nand_regs *reg, int cs)
  581. {
  582. reg->gpmc_status = gpmc_base + GPMC_STATUS;
  583. reg->gpmc_nand_command = gpmc_base + GPMC_CS0_OFFSET +
  584. GPMC_CS_NAND_COMMAND + GPMC_CS_SIZE * cs;
  585. reg->gpmc_nand_address = gpmc_base + GPMC_CS0_OFFSET +
  586. GPMC_CS_NAND_ADDRESS + GPMC_CS_SIZE * cs;
  587. reg->gpmc_nand_data = gpmc_base + GPMC_CS0_OFFSET +
  588. GPMC_CS_NAND_DATA + GPMC_CS_SIZE * cs;
  589. reg->gpmc_prefetch_config1 = gpmc_base + GPMC_PREFETCH_CONFIG1;
  590. reg->gpmc_prefetch_config2 = gpmc_base + GPMC_PREFETCH_CONFIG2;
  591. reg->gpmc_prefetch_control = gpmc_base + GPMC_PREFETCH_CONTROL;
  592. reg->gpmc_prefetch_status = gpmc_base + GPMC_PREFETCH_STATUS;
  593. reg->gpmc_ecc_config = gpmc_base + GPMC_ECC_CONFIG;
  594. reg->gpmc_ecc_control = gpmc_base + GPMC_ECC_CONTROL;
  595. reg->gpmc_ecc_size_config = gpmc_base + GPMC_ECC_SIZE_CONFIG;
  596. reg->gpmc_ecc1_result = gpmc_base + GPMC_ECC1_RESULT;
  597. reg->gpmc_bch_result0 = gpmc_base + GPMC_ECC_BCH_RESULT_0;
  598. }
  599. int gpmc_get_client_irq(unsigned irq_config)
  600. {
  601. int i;
  602. if (hweight32(irq_config) > 1)
  603. return 0;
  604. for (i = 0; i < GPMC_NR_IRQ; i++)
  605. if (gpmc_client_irq[i].bitmask & irq_config)
  606. return gpmc_client_irq[i].irq;
  607. return 0;
  608. }
  609. static int gpmc_irq_endis(unsigned irq, bool endis)
  610. {
  611. int i;
  612. u32 regval;
  613. for (i = 0; i < GPMC_NR_IRQ; i++)
  614. if (irq == gpmc_client_irq[i].irq) {
  615. regval = gpmc_read_reg(GPMC_IRQENABLE);
  616. if (endis)
  617. regval |= gpmc_client_irq[i].bitmask;
  618. else
  619. regval &= ~gpmc_client_irq[i].bitmask;
  620. gpmc_write_reg(GPMC_IRQENABLE, regval);
  621. break;
  622. }
  623. return 0;
  624. }
  625. static void gpmc_irq_disable(struct irq_data *p)
  626. {
  627. gpmc_irq_endis(p->irq, false);
  628. }
  629. static void gpmc_irq_enable(struct irq_data *p)
  630. {
  631. gpmc_irq_endis(p->irq, true);
  632. }
  633. static void gpmc_irq_noop(struct irq_data *data) { }
  634. static unsigned int gpmc_irq_noop_ret(struct irq_data *data) { return 0; }
  635. static int gpmc_setup_irq(int gpmc_irq)
  636. {
  637. int i;
  638. u32 regval;
  639. if (!gpmc_irq)
  640. return -EINVAL;
  641. gpmc_irq_start = irq_alloc_descs(-1, 0, GPMC_NR_IRQ, 0);
  642. if (IS_ERR_VALUE(gpmc_irq_start)) {
  643. pr_err("irq_alloc_descs failed\n");
  644. return gpmc_irq_start;
  645. }
  646. gpmc_irq_chip.name = "gpmc";
  647. gpmc_irq_chip.irq_startup = gpmc_irq_noop_ret;
  648. gpmc_irq_chip.irq_enable = gpmc_irq_enable;
  649. gpmc_irq_chip.irq_disable = gpmc_irq_disable;
  650. gpmc_irq_chip.irq_shutdown = gpmc_irq_noop;
  651. gpmc_irq_chip.irq_ack = gpmc_irq_noop;
  652. gpmc_irq_chip.irq_mask = gpmc_irq_noop;
  653. gpmc_irq_chip.irq_unmask = gpmc_irq_noop;
  654. gpmc_client_irq[0].bitmask = GPMC_IRQ_FIFOEVENTENABLE;
  655. gpmc_client_irq[1].bitmask = GPMC_IRQ_COUNT_EVENT;
  656. for (i = 0; i < GPMC_NR_IRQ; i++) {
  657. gpmc_client_irq[i].irq = gpmc_irq_start + i;
  658. irq_set_chip_and_handler(gpmc_client_irq[i].irq,
  659. &gpmc_irq_chip, handle_simple_irq);
  660. set_irq_flags(gpmc_client_irq[i].irq,
  661. IRQF_VALID | IRQF_NOAUTOEN);
  662. }
  663. /* Disable interrupts */
  664. gpmc_write_reg(GPMC_IRQENABLE, 0);
  665. /* clear interrupts */
  666. regval = gpmc_read_reg(GPMC_IRQSTATUS);
  667. gpmc_write_reg(GPMC_IRQSTATUS, regval);
  668. return request_irq(gpmc_irq, gpmc_handle_irq, 0, "gpmc", NULL);
  669. }
  670. static void __init gpmc_mem_init(void)
  671. {
  672. int cs;
  673. unsigned long boot_rom_space = 0;
  674. /* never allocate the first page, to facilitate bug detection;
  675. * even if we didn't boot from ROM.
  676. */
  677. boot_rom_space = BOOT_ROM_SPACE;
  678. /* In apollon the CS0 is mapped as 0x0000 0000 */
  679. if (machine_is_omap_apollon())
  680. boot_rom_space = 0;
  681. gpmc_mem_root.start = GPMC_MEM_START + boot_rom_space;
  682. gpmc_mem_root.end = GPMC_MEM_END;
  683. /* Reserve all regions that has been set up by bootloader */
  684. for (cs = 0; cs < GPMC_CS_NUM; cs++) {
  685. u32 base, size;
  686. if (!gpmc_cs_mem_enabled(cs))
  687. continue;
  688. gpmc_cs_get_memconf(cs, &base, &size);
  689. if (gpmc_cs_insert_mem(cs, base, size) < 0)
  690. BUG();
  691. }
  692. }
  693. static int __init gpmc_init(void)
  694. {
  695. u32 l;
  696. int ret = -EINVAL;
  697. int gpmc_irq;
  698. char *ck = NULL;
  699. if (cpu_is_omap24xx()) {
  700. ck = "core_l3_ck";
  701. if (cpu_is_omap2420())
  702. l = OMAP2420_GPMC_BASE;
  703. else
  704. l = OMAP34XX_GPMC_BASE;
  705. gpmc_irq = 20 + OMAP_INTC_START;
  706. } else if (cpu_is_omap34xx()) {
  707. ck = "gpmc_fck";
  708. l = OMAP34XX_GPMC_BASE;
  709. gpmc_irq = 20 + OMAP_INTC_START;
  710. } else if (cpu_is_omap44xx() || soc_is_omap54xx()) {
  711. /* Base address and irq number are same for OMAP4/5 */
  712. ck = "gpmc_ck";
  713. l = OMAP44XX_GPMC_BASE;
  714. gpmc_irq = 20 + OMAP44XX_IRQ_GIC_START;
  715. }
  716. if (WARN_ON(!ck))
  717. return ret;
  718. gpmc_l3_clk = clk_get(NULL, ck);
  719. if (IS_ERR(gpmc_l3_clk)) {
  720. printk(KERN_ERR "Could not get GPMC clock %s\n", ck);
  721. BUG();
  722. }
  723. gpmc_base = ioremap(l, SZ_4K);
  724. if (!gpmc_base) {
  725. clk_put(gpmc_l3_clk);
  726. printk(KERN_ERR "Could not get GPMC register memory\n");
  727. BUG();
  728. }
  729. clk_enable(gpmc_l3_clk);
  730. l = gpmc_read_reg(GPMC_REVISION);
  731. printk(KERN_INFO "GPMC revision %d.%d\n", (l >> 4) & 0x0f, l & 0x0f);
  732. /* Set smart idle mode and automatic L3 clock gating */
  733. l = gpmc_read_reg(GPMC_SYSCONFIG);
  734. l &= 0x03 << 3;
  735. l |= (0x02 << 3) | (1 << 0);
  736. gpmc_write_reg(GPMC_SYSCONFIG, l);
  737. gpmc_mem_init();
  738. ret = gpmc_setup_irq(gpmc_irq);
  739. if (ret)
  740. pr_err("gpmc: irq-%d could not claim: err %d\n",
  741. gpmc_irq, ret);
  742. return ret;
  743. }
  744. postcore_initcall(gpmc_init);
  745. static irqreturn_t gpmc_handle_irq(int irq, void *dev)
  746. {
  747. int i;
  748. u32 regval;
  749. regval = gpmc_read_reg(GPMC_IRQSTATUS);
  750. if (!regval)
  751. return IRQ_NONE;
  752. for (i = 0; i < GPMC_NR_IRQ; i++)
  753. if (regval & gpmc_client_irq[i].bitmask)
  754. generic_handle_irq(gpmc_client_irq[i].irq);
  755. gpmc_write_reg(GPMC_IRQSTATUS, regval);
  756. return IRQ_HANDLED;
  757. }
  758. #ifdef CONFIG_ARCH_OMAP3
  759. static struct omap3_gpmc_regs gpmc_context;
  760. void omap3_gpmc_save_context(void)
  761. {
  762. int i;
  763. gpmc_context.sysconfig = gpmc_read_reg(GPMC_SYSCONFIG);
  764. gpmc_context.irqenable = gpmc_read_reg(GPMC_IRQENABLE);
  765. gpmc_context.timeout_ctrl = gpmc_read_reg(GPMC_TIMEOUT_CONTROL);
  766. gpmc_context.config = gpmc_read_reg(GPMC_CONFIG);
  767. gpmc_context.prefetch_config1 = gpmc_read_reg(GPMC_PREFETCH_CONFIG1);
  768. gpmc_context.prefetch_config2 = gpmc_read_reg(GPMC_PREFETCH_CONFIG2);
  769. gpmc_context.prefetch_control = gpmc_read_reg(GPMC_PREFETCH_CONTROL);
  770. for (i = 0; i < GPMC_CS_NUM; i++) {
  771. gpmc_context.cs_context[i].is_valid = gpmc_cs_mem_enabled(i);
  772. if (gpmc_context.cs_context[i].is_valid) {
  773. gpmc_context.cs_context[i].config1 =
  774. gpmc_cs_read_reg(i, GPMC_CS_CONFIG1);
  775. gpmc_context.cs_context[i].config2 =
  776. gpmc_cs_read_reg(i, GPMC_CS_CONFIG2);
  777. gpmc_context.cs_context[i].config3 =
  778. gpmc_cs_read_reg(i, GPMC_CS_CONFIG3);
  779. gpmc_context.cs_context[i].config4 =
  780. gpmc_cs_read_reg(i, GPMC_CS_CONFIG4);
  781. gpmc_context.cs_context[i].config5 =
  782. gpmc_cs_read_reg(i, GPMC_CS_CONFIG5);
  783. gpmc_context.cs_context[i].config6 =
  784. gpmc_cs_read_reg(i, GPMC_CS_CONFIG6);
  785. gpmc_context.cs_context[i].config7 =
  786. gpmc_cs_read_reg(i, GPMC_CS_CONFIG7);
  787. }
  788. }
  789. }
  790. void omap3_gpmc_restore_context(void)
  791. {
  792. int i;
  793. gpmc_write_reg(GPMC_SYSCONFIG, gpmc_context.sysconfig);
  794. gpmc_write_reg(GPMC_IRQENABLE, gpmc_context.irqenable);
  795. gpmc_write_reg(GPMC_TIMEOUT_CONTROL, gpmc_context.timeout_ctrl);
  796. gpmc_write_reg(GPMC_CONFIG, gpmc_context.config);
  797. gpmc_write_reg(GPMC_PREFETCH_CONFIG1, gpmc_context.prefetch_config1);
  798. gpmc_write_reg(GPMC_PREFETCH_CONFIG2, gpmc_context.prefetch_config2);
  799. gpmc_write_reg(GPMC_PREFETCH_CONTROL, gpmc_context.prefetch_control);
  800. for (i = 0; i < GPMC_CS_NUM; i++) {
  801. if (gpmc_context.cs_context[i].is_valid) {
  802. gpmc_cs_write_reg(i, GPMC_CS_CONFIG1,
  803. gpmc_context.cs_context[i].config1);
  804. gpmc_cs_write_reg(i, GPMC_CS_CONFIG2,
  805. gpmc_context.cs_context[i].config2);
  806. gpmc_cs_write_reg(i, GPMC_CS_CONFIG3,
  807. gpmc_context.cs_context[i].config3);
  808. gpmc_cs_write_reg(i, GPMC_CS_CONFIG4,
  809. gpmc_context.cs_context[i].config4);
  810. gpmc_cs_write_reg(i, GPMC_CS_CONFIG5,
  811. gpmc_context.cs_context[i].config5);
  812. gpmc_cs_write_reg(i, GPMC_CS_CONFIG6,
  813. gpmc_context.cs_context[i].config6);
  814. gpmc_cs_write_reg(i, GPMC_CS_CONFIG7,
  815. gpmc_context.cs_context[i].config7);
  816. }
  817. }
  818. }
  819. #endif /* CONFIG_ARCH_OMAP3 */
  820. /**
  821. * gpmc_enable_hwecc - enable hardware ecc functionality
  822. * @cs: chip select number
  823. * @mode: read/write mode
  824. * @dev_width: device bus width(1 for x16, 0 for x8)
  825. * @ecc_size: bytes for which ECC will be generated
  826. */
  827. int gpmc_enable_hwecc(int cs, int mode, int dev_width, int ecc_size)
  828. {
  829. unsigned int val;
  830. /* check if ecc module is in used */
  831. if (gpmc_ecc_used != -EINVAL)
  832. return -EINVAL;
  833. gpmc_ecc_used = cs;
  834. /* clear ecc and enable bits */
  835. gpmc_write_reg(GPMC_ECC_CONTROL,
  836. GPMC_ECC_CTRL_ECCCLEAR |
  837. GPMC_ECC_CTRL_ECCREG1);
  838. /* program ecc and result sizes */
  839. val = ((((ecc_size >> 1) - 1) << 22) | (0x0000000F));
  840. gpmc_write_reg(GPMC_ECC_SIZE_CONFIG, val);
  841. switch (mode) {
  842. case GPMC_ECC_READ:
  843. case GPMC_ECC_WRITE:
  844. gpmc_write_reg(GPMC_ECC_CONTROL,
  845. GPMC_ECC_CTRL_ECCCLEAR |
  846. GPMC_ECC_CTRL_ECCREG1);
  847. break;
  848. case GPMC_ECC_READSYN:
  849. gpmc_write_reg(GPMC_ECC_CONTROL,
  850. GPMC_ECC_CTRL_ECCCLEAR |
  851. GPMC_ECC_CTRL_ECCDISABLE);
  852. break;
  853. default:
  854. printk(KERN_INFO "Error: Unrecognized Mode[%d]!\n", mode);
  855. break;
  856. }
  857. /* (ECC 16 or 8 bit col) | ( CS ) | ECC Enable */
  858. val = (dev_width << 7) | (cs << 1) | (0x1);
  859. gpmc_write_reg(GPMC_ECC_CONFIG, val);
  860. return 0;
  861. }
  862. EXPORT_SYMBOL_GPL(gpmc_enable_hwecc);
  863. /**
  864. * gpmc_calculate_ecc - generate non-inverted ecc bytes
  865. * @cs: chip select number
  866. * @dat: data pointer over which ecc is computed
  867. * @ecc_code: ecc code buffer
  868. *
  869. * Using non-inverted ECC is considered ugly since writing a blank
  870. * page (padding) will clear the ECC bytes. This is not a problem as long
  871. * no one is trying to write data on the seemingly unused page. Reading
  872. * an erased page will produce an ECC mismatch between generated and read
  873. * ECC bytes that has to be dealt with separately.
  874. */
  875. int gpmc_calculate_ecc(int cs, const u_char *dat, u_char *ecc_code)
  876. {
  877. unsigned int val = 0x0;
  878. if (gpmc_ecc_used != cs)
  879. return -EINVAL;
  880. /* read ecc result */
  881. val = gpmc_read_reg(GPMC_ECC1_RESULT);
  882. *ecc_code++ = val; /* P128e, ..., P1e */
  883. *ecc_code++ = val >> 16; /* P128o, ..., P1o */
  884. /* P2048o, P1024o, P512o, P256o, P2048e, P1024e, P512e, P256e */
  885. *ecc_code++ = ((val >> 8) & 0x0f) | ((val >> 20) & 0xf0);
  886. gpmc_ecc_used = -EINVAL;
  887. return 0;
  888. }
  889. EXPORT_SYMBOL_GPL(gpmc_calculate_ecc);
  890. #ifdef CONFIG_ARCH_OMAP3
  891. /**
  892. * gpmc_init_hwecc_bch - initialize hardware BCH ecc functionality
  893. * @cs: chip select number
  894. * @nsectors: how many 512-byte sectors to process
  895. * @nerrors: how many errors to correct per sector (4 or 8)
  896. *
  897. * This function must be executed before any call to gpmc_enable_hwecc_bch.
  898. */
  899. int gpmc_init_hwecc_bch(int cs, int nsectors, int nerrors)
  900. {
  901. /* check if ecc module is in use */
  902. if (gpmc_ecc_used != -EINVAL)
  903. return -EINVAL;
  904. /* support only OMAP3 class */
  905. if (!cpu_is_omap34xx()) {
  906. printk(KERN_ERR "BCH ecc is not supported on this CPU\n");
  907. return -EINVAL;
  908. }
  909. /*
  910. * For now, assume 4-bit mode is only supported on OMAP3630 ES1.x, x>=1.
  911. * Other chips may be added if confirmed to work.
  912. */
  913. if ((nerrors == 4) &&
  914. (!cpu_is_omap3630() || (GET_OMAP_REVISION() == 0))) {
  915. printk(KERN_ERR "BCH 4-bit mode is not supported on this CPU\n");
  916. return -EINVAL;
  917. }
  918. /* sanity check */
  919. if (nsectors > 8) {
  920. printk(KERN_ERR "BCH cannot process %d sectors (max is 8)\n",
  921. nsectors);
  922. return -EINVAL;
  923. }
  924. return 0;
  925. }
  926. EXPORT_SYMBOL_GPL(gpmc_init_hwecc_bch);
  927. /**
  928. * gpmc_enable_hwecc_bch - enable hardware BCH ecc functionality
  929. * @cs: chip select number
  930. * @mode: read/write mode
  931. * @dev_width: device bus width(1 for x16, 0 for x8)
  932. * @nsectors: how many 512-byte sectors to process
  933. * @nerrors: how many errors to correct per sector (4 or 8)
  934. */
  935. int gpmc_enable_hwecc_bch(int cs, int mode, int dev_width, int nsectors,
  936. int nerrors)
  937. {
  938. unsigned int val;
  939. /* check if ecc module is in use */
  940. if (gpmc_ecc_used != -EINVAL)
  941. return -EINVAL;
  942. gpmc_ecc_used = cs;
  943. /* clear ecc and enable bits */
  944. gpmc_write_reg(GPMC_ECC_CONTROL, 0x1);
  945. /*
  946. * When using BCH, sector size is hardcoded to 512 bytes.
  947. * Here we are using wrapping mode 6 both for reading and writing, with:
  948. * size0 = 0 (no additional protected byte in spare area)
  949. * size1 = 32 (skip 32 nibbles = 16 bytes per sector in spare area)
  950. */
  951. gpmc_write_reg(GPMC_ECC_SIZE_CONFIG, (32 << 22) | (0 << 12));
  952. /* BCH configuration */
  953. val = ((1 << 16) | /* enable BCH */
  954. (((nerrors == 8) ? 1 : 0) << 12) | /* 8 or 4 bits */
  955. (0x06 << 8) | /* wrap mode = 6 */
  956. (dev_width << 7) | /* bus width */
  957. (((nsectors-1) & 0x7) << 4) | /* number of sectors */
  958. (cs << 1) | /* ECC CS */
  959. (0x1)); /* enable ECC */
  960. gpmc_write_reg(GPMC_ECC_CONFIG, val);
  961. gpmc_write_reg(GPMC_ECC_CONTROL, 0x101);
  962. return 0;
  963. }
  964. EXPORT_SYMBOL_GPL(gpmc_enable_hwecc_bch);
  965. /**
  966. * gpmc_calculate_ecc_bch4 - Generate 7 ecc bytes per sector of 512 data bytes
  967. * @cs: chip select number
  968. * @dat: The pointer to data on which ecc is computed
  969. * @ecc: The ecc output buffer
  970. */
  971. int gpmc_calculate_ecc_bch4(int cs, const u_char *dat, u_char *ecc)
  972. {
  973. int i;
  974. unsigned long nsectors, reg, val1, val2;
  975. if (gpmc_ecc_used != cs)
  976. return -EINVAL;
  977. nsectors = ((gpmc_read_reg(GPMC_ECC_CONFIG) >> 4) & 0x7) + 1;
  978. for (i = 0; i < nsectors; i++) {
  979. reg = GPMC_ECC_BCH_RESULT_0 + 16*i;
  980. /* Read hw-computed remainder */
  981. val1 = gpmc_read_reg(reg + 0);
  982. val2 = gpmc_read_reg(reg + 4);
  983. /*
  984. * Add constant polynomial to remainder, in order to get an ecc
  985. * sequence of 0xFFs for a buffer filled with 0xFFs; and
  986. * left-justify the resulting polynomial.
  987. */
  988. *ecc++ = 0x28 ^ ((val2 >> 12) & 0xFF);
  989. *ecc++ = 0x13 ^ ((val2 >> 4) & 0xFF);
  990. *ecc++ = 0xcc ^ (((val2 & 0xF) << 4)|((val1 >> 28) & 0xF));
  991. *ecc++ = 0x39 ^ ((val1 >> 20) & 0xFF);
  992. *ecc++ = 0x96 ^ ((val1 >> 12) & 0xFF);
  993. *ecc++ = 0xac ^ ((val1 >> 4) & 0xFF);
  994. *ecc++ = 0x7f ^ ((val1 & 0xF) << 4);
  995. }
  996. gpmc_ecc_used = -EINVAL;
  997. return 0;
  998. }
  999. EXPORT_SYMBOL_GPL(gpmc_calculate_ecc_bch4);
  1000. /**
  1001. * gpmc_calculate_ecc_bch8 - Generate 13 ecc bytes per block of 512 data bytes
  1002. * @cs: chip select number
  1003. * @dat: The pointer to data on which ecc is computed
  1004. * @ecc: The ecc output buffer
  1005. */
  1006. int gpmc_calculate_ecc_bch8(int cs, const u_char *dat, u_char *ecc)
  1007. {
  1008. int i;
  1009. unsigned long nsectors, reg, val1, val2, val3, val4;
  1010. if (gpmc_ecc_used != cs)
  1011. return -EINVAL;
  1012. nsectors = ((gpmc_read_reg(GPMC_ECC_CONFIG) >> 4) & 0x7) + 1;
  1013. for (i = 0; i < nsectors; i++) {
  1014. reg = GPMC_ECC_BCH_RESULT_0 + 16*i;
  1015. /* Read hw-computed remainder */
  1016. val1 = gpmc_read_reg(reg + 0);
  1017. val2 = gpmc_read_reg(reg + 4);
  1018. val3 = gpmc_read_reg(reg + 8);
  1019. val4 = gpmc_read_reg(reg + 12);
  1020. /*
  1021. * Add constant polynomial to remainder, in order to get an ecc
  1022. * sequence of 0xFFs for a buffer filled with 0xFFs.
  1023. */
  1024. *ecc++ = 0xef ^ (val4 & 0xFF);
  1025. *ecc++ = 0x51 ^ ((val3 >> 24) & 0xFF);
  1026. *ecc++ = 0x2e ^ ((val3 >> 16) & 0xFF);
  1027. *ecc++ = 0x09 ^ ((val3 >> 8) & 0xFF);
  1028. *ecc++ = 0xed ^ (val3 & 0xFF);
  1029. *ecc++ = 0x93 ^ ((val2 >> 24) & 0xFF);
  1030. *ecc++ = 0x9a ^ ((val2 >> 16) & 0xFF);
  1031. *ecc++ = 0xc2 ^ ((val2 >> 8) & 0xFF);
  1032. *ecc++ = 0x97 ^ (val2 & 0xFF);
  1033. *ecc++ = 0x79 ^ ((val1 >> 24) & 0xFF);
  1034. *ecc++ = 0xe5 ^ ((val1 >> 16) & 0xFF);
  1035. *ecc++ = 0x24 ^ ((val1 >> 8) & 0xFF);
  1036. *ecc++ = 0xb5 ^ (val1 & 0xFF);
  1037. }
  1038. gpmc_ecc_used = -EINVAL;
  1039. return 0;
  1040. }
  1041. EXPORT_SYMBOL_GPL(gpmc_calculate_ecc_bch8);
  1042. #endif /* CONFIG_ARCH_OMAP3 */