gpmc.c 31 KB

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